2021-03-09
数据定义语言库和表的管理
一、库的管理
创建、修改、删除
二、表的管理
创建、修改、删除
创建:create
修改:alter
删除:drop
一、库的管理
1.库的创建
(可以右键)
语法:create database 库名
例子:create database if not exists books
2.库的修改
alter database books character set gbk(更改库的字符集)
3.库的删除
drop database if exists books
二、表的管理
1.表的创建※
语法:
create table 表名(
列名 列的类型 【(长度)约束】,
列名 列的类型 【(长度)约束】,
列名 列的类型 【(长度)约束】,
列名 列的类型 【(长度)约束】,
)
案例:
create table book(
id int,
bname varchar(20),
price double,
authorid int,
publishdata datebase
);
2.表的修改
①修改列名
alter table book chance column publishdata pubData DataTime(修改的时候还得加类型,也可以顺便改个类型)
②修改列的类型或约束
alter table book modify column pubdate TIMESTAMP
③添加新列
alter table author add column annual DOUBLE
④删除列
alter table author drop column annual
⑤修改表名
alter table author rename to book_author
3.表的删除
drop table if exists bool_author
通用的写法
drop database if exists 旧库名
create database 新库名
drop table if exists 旧表名
create table 表名()
4.表的复制
create table copy like anthor(仅能复制表的结构)
create table copy2 select * from author(复制表的结构+数据)
create table copy3 select id,name
from ahthor
where country=‘中国‘ (仅复制部份数据)
create table copy4 (仅复制某些字段)
select id,au_name
from author
where 0;
原文:https://www.cnblogs.com/alwaysrun/p/14507770.html