码迷,mamicode.com
首页 > 数据库 > 详细

mysql基操

时间:2018-06-14 20:48:28      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:关联   add   key   har   drop   zha   bool   run   for   

创建数据表:
  create table tt1(
    id int,
    name varchar(20),
    age int,sex boolean
    );
  insert into tt1 values(1,"zhang",25,0);
  insert into tt1 values(2,"wang",25,1);
  insert into tt1(id,name,age,sex) values(3,"li",28,1);
  insert into tt1(id,name,sex,age) values(4,"sun",0,22);

修改数据表
  修改表名:
    alter table 表名 rename to 新表名
    eg:alter table tt1 rename to info;

  修改字段名:
    alter table 表名 change 旧字段名 新字段名 新数据类型;
    eg:alter table info change id number int(11);

  修改字段数据类型:
    alter table 表名 modify 需要修改数据类型的字段名称 新数据类型;
    eg:alter table info modify sex char(2);

添加和删除字段
  添加:
    alter table 表名 add 新字段名 新数据类型;
    eg:alter table info add class int(10);

  删除:
    alter table 表名 drop 字段名;
    eg:alter table info drop class;


增补(删除)约束:

  约束名:约束类型_表名_字段名
  增加主键:
    alter table 表名 add constraint 约束名 primary key(字段名);
    eg:alter table info add constraint pk_number primary key(number);
  删除主键:
    alter table 表名 drop primary key [主键名];
    eg:alter table info drop primary key;

  外键:
    alter table 表名 add constraint 约束名 foreign key(字段名) references 引用表表名(引用的字段名);

  检查:
    alter table 表名 add constraint 约束名 check(约束条件);

  默认:
    alter table 表名 add 要修改的字段名 set default 默认值;

  自增:
    alter table 表名 modify column 字段名 类型 auto_increment;
    eg:alter table info modify column number int auto_increment;

删除数据表:
  无外键关联:
    drop table 表名;

  有外键关联:
  先解除关联:
    alter table 从表名称 drop foreign key 外键名;
  再:
    drop table 表名;

插入数据:
  所有列都插入值:
    insert into 表名 values (值1,值2,值3...);
    特点:列值同数,列名同序

  特定列插入:
    insert into 表名 (字段名1,字段名2,字段名3...)values(值1,值2,值3...);

  一次性插入多条数据:
    insert into 表名 (字段名1,字段名2,字段名3...)values(值1,值2,值3...),(值1,值2,值3...),(值1,值2,值3...);

修改数据:
  全部:
    update 表名 set 需要修改数据名;

  特定:
    update 表名 set 需要修改数据名 where 条件;
    eg:update info set name = ‘章‘ where number = 1;

  删除数据表数据:
    delete:delete from 表名[where 条件];
    eg:delete from info;
    truncate:truncate 表名;(清空数据表所有数据)
    eg:truncate info;

mysql基操

标签:关联   add   key   har   drop   zha   bool   run   for   

原文地址:https://www.cnblogs.com/moyuchen99/p/9184272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!