1、创建一个course表格:
create table course(
-> cno varchar(20) primary key,
-> course_name varchar(50) not null,
-> cpno varchar(20),
-> course_credit decimal(4,1));【前一个4表示所有的位数,后面的2表示小数位的位数】
2、查看创建的表格:
show tables;
3、修改表结构
alter table +表名
【增加新列】add+列定义
【修改表中指定列的默认值,或者删除列的默认值】alter+列名
【修改指定列的数据类型】modify+列定义
【修改列的名称】change+列名+原列名
【删除列或者约束】drop+列名
E.g:
1)增加列;
alter table student
add ssex char(2) not null;
2)修改默认值/为表中字段设置默认值;
alter table student
alter ssex set default'男';
3)重命名、更改表明;
方法一:
Alter table test
Rename table to test2;
方法二:
alter table student
rename to table2;
4)更改已有数据类型;
alter table student
-> modify ssex enum('male','female')not NULL;
【译:更改ssex中的数据:男、女更改为枚举型】