- 库的管理
1、库的创建
语法:create database [if not exists ] 库名 【character set 字符集名】;
2、库的修改
例:更新库的字符集
alter database 库名 character set gbk;
3、库的删除
drop database if exists 库名;
- 表的管理
1.表的创建
create table 表名(
列名 列的类型【(长度)约束】,
列名 列的类型【(长度)约束】,
...
列名 列的类型【(长度)约束】,
)
例:创建表book
create table book(
id int,
name varchar(20)
);
2.表的修改
alter table 表名 add|drop|modify|change column 列名 【列类型 约束】【first|after 字段名】;
①修改列名
alter table 表名 change column publishdate pubdate datetime;
②修改列的类型或约束
alter table 表名 modify column pubdate int;
例:将列last_name的长度增加到50
alter table 表名 modify column last_name varchar(50);
③添加新列
alter table 表名 add column age int;
④删除列
alter table 表名 drop column 列名;
⑤修改表名
alter table 表名 rename to 另一表名;
3.表的删除
drop table 【if exists】表名;
4.表的复制
①仅仅复制表的结构
create table copy like 表名;
②复制表的结构+数据
create table copy2
select * from 表名;
③只复制部分数据
create table copy3
select id, name
from 表名
where nation=’中国’;
④仅仅复制某些字段
create table copy4
select id, name
from 表名
where 0; (使查询不到任何数据)