第25节--数据定义语言DDL 之 表的管理

表的管理

1. 表的创建

create table if not exists 表名(

列名 列的类型(长度)约束,

列名 列的类型(长度)约束

);

列的类型长度和约束可以省略

案例1. 创建表book

create table if not exists book(

id int, #书的编号

bname varchar(20),#20代表书名的最大长度,20个字符,一个字母,汉字均代表一个字符

price double,#价格

authorid int,#作者

publishdate datetime   #出版时间

);

案例2. 创建表author

create table if not exists author(

id int,

au_name varchar(20),

nation varchar(20)

);

2. 表的修改

Alter table 表名 add/drop/modify/change column 列名 {列类型,约束};

①修改列名

alter table 表名 change column 原列名 新列名 列类型;

alter table book change column(可以省略)publishdate pubdate datetime;

②修改列的类型或约束

alter table 表名 modify column 原列名 表类型(新);

alter table book modify column pubdate timestamp;

③添加列

Alter table 表名 add column 新列名 列类型;

alter table author add column annual double;

④删除列

alter table author drop column annual;

⑤ 修改表名

alter table 原表名 rename to 新表名;

alter table author rename to book_author;

 

3. 表的删除

drop table if not exists 表名;

4.#表的复制

表中插入值

insert into author values

(1, '树上村树',‘日本’)

(2, '莫言' ,‘中国’)

(3, '冯唐' ,‘中国’)

(4, '金庸' ,‘中国’);

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;(或where 0;){用不成立的筛选条件,所以仅仅复制字段名}

Jasminelee
原文地址:https://www.cnblogs.com/Jasmine6-Lee/p/12714437.html