1.创建表
格式:create table tablename (列1 数据类型 not null auto_incement,
列2 数据类型 not null );
如:创建一个学生表student,学号 sid,学生姓名 sname
create table student(sid int not null auto_increment,
sname char(10) not null,
primary key (sid)
);
2.给列指定NULL值
当给不是主键列指定null值,如 create table test0310 (tid int primary key auto_increment,
tname char(10) null );
当insert时,不填写该列的值,可以插入成功,但是会有警告信息。
3.创建主键
1.create table test (tid int primary key default 20170310)
2.create table test (tid int default 20170310 ,primary key (tid));
3.altert table test add primary key (tid);
4.auto_increment
1.MySQL 自动对该列增量,当delete from 表中数据时,增量的值并不会从新开始,而是你最后的increment 值加1;但是若用truncate table 删除数据时,该值从新计算。
5.更改表
1增加列
alter table tablename ADD 新列 数据类型 参数;
2.修改列属性(数据类型,参数)
alter table tablename modify 列名 新类型 新参数;
3.修改列名(列属性)
alter table tablename change 旧列名 新列名 新类型 新参数;
4.删除列
alter table tablename drop 列名;
note:若是table里只有一个列,使用此方法删除,会报1090错误。
6.设置外键
alter table t1 add constraint 外键名 foreign key (列) references t2 (列);