02-009 MySQL_基础_DDL语言

DDL (Data Definition Language)

数据定义语言:库、表的创建(create)、修改(alter)、删除(drop)

库的管理

1.创建:
create database 【if not exists】 库名;
2.修改:
别改了,不安全。
更改库的字符集:alter database 库名 character set gbk;
3.删除:
drop database 【if exists】库名;

表的管理

1.创建
create table 【if not exists】 表名(
列名 类型【(长度) 约束】,
列名 类型【(长度) 约束】,

列名 类型【(长度) 约束】

2.修改
列名、类型或者约束、添加列、删除列、修改表名
①修改列名

alter table book change publishDate pubDate datetime;

②类型或者约束

alter table book modify publDate timestamp;

③添加新列

alter table author add column annual double;

④删除列

alter table author drop column annual;

⑤修改表名

alter table author rename to book_author;

3.删除
drop table 表名;
查看当前库的所有表:show tables;

表的复制

1.仅仅复制表结构

create table test like author;

2.复制结构和数据

create table copy 
select * from author ;

3.复制部分结构和数据

扫描二维码关注公众号,回复: 8736132 查看本文章
create table copy 
select id,name  from author where id = 5;

4.仅仅复制部分结构无数据

create table copy 
select id,name  from author where  1= 5;
create table copy 
select id,name  from author where 0;

跨库可以用【库名.表名】

学习整理于MySQL 基础+高级篇.

发布了53 篇原创文章 · 获赞 0 · 访问量 397

猜你喜欢

转载自blog.csdn.net/weixin_40778497/article/details/103589632
今日推荐