MySQL DDL-对库和表的操作

#DDL
/*
数据定义语言

库和表的管理

一、库的管理
创建、修改、删除

二、表的管理
创建、修改、删除

创建:create 
修改: alter
删除:drop
*/

一、库的管理
#1.库的创建
/*
语法:
create database 库名称;
*/

#案例:创建库Books
create database if not exists Books;


#2.库名称的修改
由于rename存在安全隐患,推荐直接在文件夹中更改
进入:C:\ProgramData\MySQL\MySQL Server 5.5\data

#更改库的字符集
alter database books character set gbk;


#3.库的删除
drop database if exists books; 


二、表的管理
#1.表的创建
/*
create table 表名(列名 列的类型 【长度 约束】,
		  列名 列的类型 【长度 约束】,
		。。。。
		  列名 列的类型 【长度 约束】)
*/
#案例:创建表book
use books;
create table book(id int,#编号
		bname varchar(20),#图书名字
		price double,#价格
		authorID int,#作者
		publishDate datetime #出版日期
);
desc book;

#创建作者表
create table author(id int,
		    au_name varchar(20),
		    nation varchar(10)
);
desc author;

#2.表的修改
/*
alter table 表名 add|drop|modify|change column 列名【列类型 约束】
*/

#(1)修改列名
#将publishDate修改为pubData
alter table book change column publishDate pubData datetime;
desc book;

#(2)修改列的类型或者约束
修改列的类型
alter table book modify column pubData timestamp;

#(3)添加新列
alter table author add column annual double;
desc author;

#(4)删除列
alter table author drop column annual;

#(5)修改表名
alter table author rename to author_book;



#3.表的删除(只适用于表和库)
drop table if exists author_book;


#4.表的复制

  

猜你喜欢

转载自www.cnblogs.com/ivyharding/p/11553218.html