数据定义语言
**库和表的管理:*创建、修改、删除
/
创建:create
修改:alter
删除:drop
*/
一、库的管理
①库的创建:
语法:
create database 库名;
创建books库:create books if not exists books;
②库的修改:
语法:
rename database books to 新库名;
更改库的字符集:
alter database books character set gbk;
③库的删除:
drop database if exists books;
二、表的管理:
①创建表:
create table 表名(
列名 类型【(长度)约束】,
列名 类型【(长度)约束】,
列名 类型【(长度)约束】,
…
列名 类型【(长度)约束】
)
create table if not exists book(
id int,#编号
bkname varchar(20)#书名
price double,#价格
author_id int,#作者编号
publishdate datetime#出版日期
)
create table author(
id int,
au_name varcahr(20),
nation varchar(10)
)
②表的修改:
/*
alter table 表名 add|drop|modify|change column 列名 类型 约束;
*/
修改列名:alter table book change column publishdate pubDate datetime;
类型或者约束:alter table book modify column pubDate timestamp;
添加列:alter table author add column annul double;
删除列:alter table author drop column annul;
修改表名:alter table author rename to book_author;
③表的删除
drop table if exists book_author;
show tables;
通用的写法:
drop database if exists 旧库名;
create database 新库名;
drop table if exists 旧表名;
create table 表名();
④表的复制
insert into author values
(1,‘村上春树’,‘日本’),
(2,‘莫言’,‘中国’),
(3,‘金庸’,‘中国’);
1、仅仅复制表的结构无数据
create table copy like author;
2、复制整个表
create table copy2
select * from author;
3、只复制部分数据
create table copy3
select id,au_name
from author
where nation=‘中国’;
4、仅仅复制某些字段
create table copy4
select id,au_name
from author
where 1=2;#条件不成立只复制字段过去,而没有数据