约束
1.主键:primary key
2.默认值:default
3.数据自动增加1:auto_increment
4.数据不能为空:not null
5.数据唯一:unique
6.外键:foreign key(两个以上的表建立关联时用)
操作数据表
1.查看数据表:show tables(查看一个库里所有的表);select * from 表的名称(查看表的内容)
2.创建数据表:create table 表的名字 (字段1 数据类型 约束条件,字段2 数据类型 约束条件){例如:create table students(id int unsigned not null auto_increment primary key);(创建了一个名为students的表)}
3.查看表的创建语句:show create table
4.查看表结构:desc 表的名称
5.修改表名字:rename table 表名字 to 新的表名字{例如:rename table students to classes;(把名为students的表名字改成classes,只有名称发生改变,其他不变)}
6.增加数据表字段:alter table 表的名字 add 字段名 约束条件{例如:alter table students add id int unsigned not null auto_incresment primary key ;(给表students中增加一个名为id的栏目,该栏目中数值为正整数,非空,自动增加1,是主键)}
7.修改数据表字段:①改变字段名字及其其他属性:alter table 表的名字 change 字段1名字 字段2名字 数据类型 约束条件{例如:alter table students change age height ;(把表students中age改为height)}
②修改原字段的数据类型和约束条件:alter table 表的名字 modify 字段名 数据类型 约束
③将某字段移动到首位:alter table 表的名字 modify 需要移动的字段名 数据类型 约束 first
④将字段1移动到字段2后:alter table 表的名字 modify 字段1 数据类型 约束 after 字段2 数据类型 约束条件
8.删除数据表字段:alter table 表的名字 drop 字段名{例如:alter table students drop id;(删除表students中的id栏目)}
9.删除数据表:drop 表名