1. 创建表
1.1. 创建表命令:
create table `表名`(
`列名1` 列的类型(长度) 约束,
`列名2` 列的类型(长度) 约束,
...
) engine=数据库引擎 auto_increment=自动递增起始值 default charset=字符集; 。
1.2. engine数据库引擎一般为InnoDB。
1.3. auto_increment自动递增起始值一般为1。
1.4. default charset默认字符集一般为utf8。
1.6. 表名和列名需要加`符号。
1.7. 创建一个学生表
-- 学生id
-- 姓名name
-- 姓别sex
-- 出生日期birthday
-- 住址address
-- 身高height
-- 体重weight
-- 说明introduce
create table `student` (
`id` bigint(20) not null auto_increment,
`name` varchar(16) default null,
`sex` char(2) default null,
`birthday` datetime default null,
`address` text,
`height` float(16,2) default null,
`weight` double(16,3) default null,
`introduce` longtext,
primary key (`id`)
) engine=InnoDB auto_increment=1 default charset=utf8;
2. 查看表
2.1. 查看所有表命令: show tables; 。
2.2. 查看表结构命令: desc `表名`; 。
2.3. 查看创建表过程命令: show create table `表名`; 。
3. 修改表
3.1. 添加列命令: alter table `表名` add `列名` 列的类型 列的约束; 。
3.2. 修改列命令: alter table `表名` modify `列名` 列的类型 [null/not null | auto_increment | comment | zerofill]; 。
3.3. 修改列名命令: alter table `表名` change `旧列名` `新列名` 列的类型; 。
3.4. 删除列命令: alter table `表名` drop `列名`; 。
3.5. 修改表名命令: rename table `旧表名` to `新表名`; 。
3.6. 修改表的字符集命令: alter table `表名` character set 字符集; 。
4. 删除表
4.1. 删除表命令: drop table `表名`; 。