与创建表相关的语句(DDL) (最好能手写出来)
CREATE TABLE table_name
(
field1 datatype 约束/主键约束 auto_increment,
field2 datatype 约束,
field3 datatype 约束
)[character set 字符集 collate 校对规则]
1.创建表
create table t_user(
id int,
name varchar(20),
sal double(4,3),
birthday datetime,
hiredate timestamp
);
varchar最好指定长度,整型一般不指定.
2.查看当前库中有哪些表
show tables;
3.查看表的结构
desc 表名; description
4.删除表
drop table 表名;
drop table t_user;
5.添加一列
alter table 表名 add 列名 类型;
alter table t_user add photo blob;
6.修改列的类型
alter table 表名 modify 列名 类型;
alter table t_user modify photo varchar(20);
7.修改列的名称
alter table 表名 change 旧列名 新列名 数据类型;
将 photo这一列 改名为 image
alter table t_user change photo image varchar(20);
8.删除某列
alter table 表名 drop 列名;
alter table t_user drop image;
9.修改表的名称
rename table 旧表名 to 新名;
rename table t_user to user;
10(用的极少)修改表的字符集. (如果创建表时不指定,默认使用数据库的字符集)
alter table 表名 character set 字符集 collate 校对集;
alter table t_user character set utf8 collate utf8_bin;