DDL语句

DDL 是数据定义语言的缩写,简单来说,就是对数据库内部的对象进行创建、删除、修改的操作语言。它和 DML 语言的最大区别是 DML 只是对表内部数据的操作,而不涉及到表的定义、结构的修改,更不会涉及到其他对象。DDL 语句更多的被数据库管理员(DBA)所使用,一般的开发人员很少使用

1.创建数据库 test1

create database test1

2.选择数据库test1

use test1

3.删除数据库test1

drop database test1

4.创建表

CREATE TABLE `my_friend` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(20) not null default '' comment '用户user_id',
`friend_id` varchar(20) not null default '' COMMENT '好友user_id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='好友表';

5.查看my_friend表定义
desc my_friend 或 show create table my_friend G

6.删除表 table1
drop table table1

7.修改表
a.修改表类型 alter table 表名 modify 字段名 varchar(20);
b.增加表字段 alter table 表名 add column 字段名 int(2);
c.删除表字段 alter table 表名 drop column 字段名;
d.字段改名 alter table 表名 change age age1 int(4);将age改名为age1
e.修改字段排列顺序
alter table `表名` add `birth` datetime default null after `id_card`---添加`birth`在`id_card`之后
alter table 表名 modify age int(4) first;修改字段age,将它放在最前面
f.表改名 alter table epm rename emp1;把emp改名为emp1
原文地址:https://www.cnblogs.com/xyc211/p/7976341.html