-- DDL:Data definition LANGUAGE 数据定义语言:create alter delete
/*
SQL数据定义功能有:
1.模式定义
2.表定义
3.视图和索引定义
*/
/*
SQL语句数据定义语句
模式:create `schema` drop schema
表 :create TABLE drop TABLE alter TABLE
视图:create view DROP VIEW
索引:create INDEX drop VIEW alter INDEX
*/
-- 一个数据库管理系统DBMS的实例instance中可以建立多个数据库。
-- 一个数据库可以建立多个模式。定义模式相当于定义了命名空间,再改模式中可以进一步定义模式下的数据库对象。
-- 一个模式下通常包括多个表,视图,索引等数据库对象
-- 1.模式定义(略) 3.视图和索引定义
-- 2.表的定义,删除,修改
-- 商品种类表
drop table if EXISTS kind;
-- 创建表:种类
create table kind(
`kindId` int PRIMARY key auto_increment, -- 列级约束
`kindName` VARCHAR(50) not null UNIQUE comment '种类名称'
);
-- 小种类
create table smallKind(
sid int auto_increment,
sname varchar(100) not null,
kindId int not null,
CONSTRAINT smallKind_sid_Pk PRIMARY key(sid), -- 表级约束,在列的最后维护关系
unique(sname),
CONSTRAINT smallKind_kindId_FK foreign key(kindId) REFERENCES kind(kindId) -- constraint 约束名 foreign key(当前字段) 引用kind表里的(kindId)。
)
drop table if EXISTS item;
-- 商品表
create table item(
id int auto_increment,
name VARCHAR(255) not null,
salePoint VARCHAR(255),
salePrice DOUBLE not null,
marketPrice NUMERIC(11,2) not null, -- 指定11位,小数点后两位的数
inPrice double,
description text,
createDate date,
updateDate datetime,
constraint item_id_pk primary key(id)
)
-- 增加列
alter table item add status int default '0' comment'0-新增,1-上架,2-下架';
alter table item add inPrice double;
alter table item add newLine varchar(30) UNIQUE; -- -- 增加列和约束,新增一个约束的列sid,外键无法添加
-- 修改列
alter table item MODIFY column sid long; -- 修改数据类型。
-- 更改列名[+新的数据类型]
alter table post change COLUMN annotationOld annotationNew varchar(255);
alter table item change smallKindId sid int; -- 列重命名和数据类型
alter table item add foreign key(sid) REFERENCES smallkind(sid); -- 给当前sid字段增加一个smallKind表.sid外键
alter table item rename to xxx; -- 表重命名
alter table xxx rename to item;
alter table item disable CONSTRAINT; -- 禁用约束
alter table item enable CONSTRAINT; -- 启用约束
alter table kind DEFAULT CHARACTER set utf8; -- 修改表的默认编码,对字段无效
alter table `kind` convert to character set utf8; -- 修改一张表的所有字段的编码格式
-- 更改已有数据库的编码格式
alter database sms character set utf8;
-- 删除列
alter table item drop inprice;
-- 删除表
drop table item;
mysql数据库note2:DDL数据定义语言
最新推荐文章于 2022-05-20 16:15:50 发布