网站开发根目录建在哪,建筑行业网站运营方案,常州网警,一套网页ui设计多少钱Data Definition Language 1 库的操作1.1 create 创建1.2 alter 修改1.3 drop 删除 2 表的操作2.1 表的创建2.2 表的修改2.2.1 修改表名2.2.2 修改列名2.2.3 修改列的类型和约束2.2.4 添加列2.2.5 删除列 2.3 表的删除2.4 表的复制 3 练习 1 库的操作
1.1 create 创建
create… Data Definition Language 1 库的操作1.1 create 创建1.2 alter 修改1.3 drop 删除 2 表的操作2.1 表的创建2.2 表的修改2.2.1 修改表名2.2.2 修改列名2.2.3 修改列的类型和约束2.2.4 添加列2.2.5 删除列 2.3 表的删除2.4 表的复制 3 练习 1 库的操作
1.1 create 创建
create database books;为了增加容错性我们应该写成
create database if not exists books;
# 如果不存在books这个库即创建1.2 alter 修改
如果想对库进行修改首先要停止服务直接修改文件夹名 一般来说不建议这个操作容易丢失数据
更改库的字符集
alter database books character set gbk;1.3 drop 删除
库的删除不能重复执行同1.1一样此时加上if exists
drop database if exists books;2 表的操作
2.1 表的创建
在books数据库中创建一个book表
create table book(id int, #编号bName varchar(20), #书名price double, #价格authorId int, #作者编号publishDate datetime #出版日期
);在books数据库中创建一个author表
create table author(id int,au_name varchar(20),au_city varchar(10)
);2.2 表的修改
2.2.1 修改表名
把author表修改为book_authors表
alter table author
rename to book_authors;2.2.2 修改列名
语法
alter table 表名
change column 列名 新列名 列的类型把publishdate改成pubDate
alter table book
change column publishdate pubDate datetime;2.2.3 修改列的类型和约束
把pubDate的类型改成timestamp
alter table book
modify column pubDate timestamp;2.2.4 添加列
在author表中添加新列annual年薪
alter table author
add column annual double;2.2.5 删除列
把刚刚添加的删了
alter table author
drop column annual;2.3 表的删除
drop table 表名;例如删除book_author表
drop table book_author;2.4 表的复制
只会复制表的结构但是不包含值
create table copy
like author;复制表的结构值
create table copy2
select *
from author;复制部分数据
create table copy3
select id,au_name
from author
where au_city 中国;3 练习 #1.
create table dept1(id int(7),name varchar(25)
);#2.
create table dept2
select department_id,department_name
from myemplyees.departments;#3.
create table emp5(id int(7),First_name varchar(25),Last_name varchar(25),Dept_id int(7)
);#4.
alter table emp5
modify column Last_name varchar(50);#5.
create table employees2
like myemplyees.employees;#6.
drop table if exists emp5;#7.
alter table employees2
rename to emp5;#8.
alter table dept
add column test_column int;alter table emp5
add column test_column int;#9.
alter table emp5
drop column dept_id;