mysql DDL数据定义语言
DDL數據定義語言
本節涉及MySQL關鍵字:create、alter(rename,add,chang,modify,drop)、drop、delete、truncate等。
-- 創建表:
-- 數據類型:int,date,varchar(size),[?decimal(5,2),小數點后兩位]
格式:create table tableName
? ? ? ? ? ? ? ?(
? ? ? ? ? ? ? ? ? columnName datatype,
? ? ? ? ? ? ? ? ? columnName datatype,
? ? ? ? ? ? ? ? ? .......
);
拷貝表格:
格式 : ?create table tableName
? ? ? ? ? ?as
? ? ? ? ? ?subquery;或者在subquery后加(where 1=2),表示只保留格式
創建表實例: create table supermarket
? ? ? ? ? ? ? ? ? ? ? ? ?(
? ? ? ? ? ? ? ? ? ? ? ? ? ? market_id int,
? ? ? ? ? ? ? ? ? ? ? ? ? ? market_name varchar(50),
? ? ? ? ? ? ? ? ? ? ? ? ? ? address varchar(200),
? ? ? ? ? ? ? ? ? ? ? ? ? ? contact varchar(15),
? ? ? ? ? ? ? ? ? ? ? ? ? ? manager int
? ? ? ? ? ? ? ? ? ? ? ? ? );
拷貝表實例:create table supermarkets_copy
? ? ? ? ? ? ? ? ?as
? ? ? ? ? ? ? ? ?select *
? ? ? ? ? ? ? ? ?from supermarkets;
-- 修改表:alter
? ? ? ?-- 修改表名
? ? ? ? ? ? ?格式: ?alter table tableName
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?rename to new_tableName;
? ? ? ? ? ? ? ? 實例: ??alter table supermarket?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? rename to supermarkets;
? ? ? ?-- 添加列
? ? ? ? ? ? 格式 : alter table tableName
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?add columnName defined [ FIRST | AFTER column_name ];
? ? ? ? ? ? ? ? ? ? ? ?alter table tableName
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?add columnName defined [ FIRST | AFTER column_name ],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?add columnName defined [ FIRST | AFTER column_name ],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?...;
? ? ? ? ? ? ?? 實例: ?alter table supermarkets
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?add kind int;
?
? ? ? -- 修改列
? ? ? ??-- 修改列名
? ? ? ? ? ? 格式: ?alter table tableName
? ? ? ? ? ? ? ? ? ? ? ? ? ? change column old_name new_name defined;
? ? ? ? ? ? ? ?實例: ?alter table supermarkets
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?change kind category varchar(10);
? ? ?-- 修改列定義
? ? ? ? ? ? 格式: ?alter table tableName
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modify columnName new_defined [ FIRST | AFTER column_name ];
? ? ? ? ? ? ? ? ? ? ? alter table tableName
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modify columnName new_defined [ FIRST | AFTER column_name ],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modify columnName new_defined [ FIRST | AFTER column_name ],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ....;
? ? ? ? ? ? ? ? ? ?實例:?alter table supermarkets
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?modify category int;
? ?-- 刪除列
? ? ? ? ? ?格式; ? alter table tableName
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? drop column columnName;
? ? ? ? ? ? ? ? ???實例:?alter table supermarkets
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?drop column category;
-- 刪除表
? ? ? ? ? ?格式: drop table tableName;
? ? ? --刪除表的內容,保留格式: ? ? ? ? ?
? ? ? ? ? ?格式:delete from?tableName;? 后面可以加限制條件where1=2或1=1。 ? ??
-- truncate: 清空表(刪除后無法返回上一步)
? ? ? ? ? ? ? ????格式:truncate table tableName;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?select * from emp;
-- 創建表上的約束
-- 主鍵約束:標志行的唯一性,只能有一個,而且不能為空
-- 外鍵約束:建立兩張表的關聯關系
-- 唯一約束:標志其唯一性,但數量不限的,可以為空
-- 非空約束:必填項
-- 默認
如果創建表的時候添加約束,可直接去掉alter語句
創建表上的約束, (主鍵,外鍵,唯一)約束用add constraint,
? ? ? ? ? ? ? ? ? ? ? (非空,默認)約束用modify.
? ? ? ? ? ? ? 格式: alter table tableName
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?add constraint_defination;
? ? ? ? ? ? ? ? ? ? ?實例: alter table?supermarkets
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?drop primary key;
? ?主鍵約束:? primary key ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ?實例:alter table supermarkets
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? add constraint pk_market_id primary key(market_id);
? ?唯一約束:?unique??
? ? ? ? ? ? ? ? ? ? ?實例: alter table supermarkets
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? add constraint uq_market_name unique(market_name);
? ? ? ? ?
? ?外鍵約束:foreign key
? ? ? ? ? ? ? ? ? ? ?實例: ?alter table emp
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? add constraint fk_market_id foreign key (market_id)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? references supermarkets(market_id);
? ? ? 兩個外鍵的約束實例: ?add constraint fk_market_id_customer_id foreign key (market_id,customer_id)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? references supermarkets(market_id),customer(customer_id);
? ? ? ? ? ? ? ? ? ?delete from supermarkets where market_id = 1; -- 維護數據的完整性
? ? 非空約束:not null
? ? ? ? ? ? ? ? ? ? ?實例: alter table supermarkets
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?modify market_name varchar(50) not null,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?modify address varchar(200) not null;
? ? 默認值:
? ? ? ? ? ? ? ? ? ? 實例: ?alter table supermarkets
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?modify market_name varchar(50) not null,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?modify address varchar(200) not null default '蘇州店';
?
? ? ? ?
-- 視圖
? ? ?-- 創建或者更新視圖
? ? ? ? ? ? ?格式:-- create or replace view viewName
? ? ? ? ? ? ? ? ? ? ?-- as
? ? ? ? ? ? ? ? ? ? ?-- subquery;
? ? ? ? ? ? ? ? 實例: create view cust_view
? ? ? ? ? ? ? ? ? ? ? ? ?as
? ? ? ? ? ? ? ? ? ? ? ? ?select customer_id,customer_name,address,province,city,sale_employee_id,credit_limit
? ? ? ? ? ? ? ? ? ? ? ? ?from customer;
? ? ? ? ? ? ? ? ? ? ? ? ?select *
? ? ? ? ? ? ? ? ? ? ? ? ?from cust_view;
? ? ? ? ? ? ? ?實例: create or replace view purchase_view
? ? ? ? ? ? ? ? ? ? ? ? as
? ? ? ? ? ? ? ? ? ? ? ? select `order`.order_number,customer.customer_name,product.product_name,buy_number,format(order_detail.price,2) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? fmt_price,format(order_detail.total_money,2) fmt_total_money
? ? ? ? ? ? ? ? ? ? ? ? from `order`
? ? ? ? ? ? ? ? ? ? ? ? left join order_detail on `order`.order_id = order_detail.order_id
? ? ? ? ? ? ? ? ? ? ? ? left join customer on `order`.customer_id = customer.customer_id
? ? ? ? ? ? ? ? ? ? ? ? left join product on order_detail.product_id = product.product_id;
? ? ? ? ? ? ? ? ? ? ? ? select *
? ? ? ? ? ? ? ? ? ? ? ? from purchase_view
? ? ? ? ? ? ? ? ? ? ? ? where order_number = '321154103';
? ? ?-- 刪除視圖
? ? ? ? ? ? ?格式:-- drop view [if exists] view_name;
? ? ? ? ? ? ? ? ?實例:drop view if exists cust_view;
?
-- 現有一個商店的數據庫,記錄客戶及其購物情況,由下面三個表組成:
-- 商品goods(商品號goodsId,商品名goodsName,單價unitprice,商品類別category,供應商provider);
-- 客戶customer(客戶號customerId,姓名name,地址address,電郵email,性別sex,身份證cardId);
-- 購買purchase(客戶號customerId,商品號goodsId,購買數量nums);
-- 請用SQL語言完成下列功能:
-- 1. 建表,在定義中要求聲明:
-- (1). 每個表的主外鍵;
-- (2). 客戶的姓名不能為空值;
-- (3). 電郵不能夠重復;
-- (4). 客戶的性別默認是男;
create table goods
(
? ? ? goodsId int not null,
? ? ? goodsName varchar(50),
? ? ? unitprice decimal(5,2),
? ? ? category varchar(30),
? ? ? provider varchar(30),
? ? ? constraint pk_goodsId primary key(goodsId)
);
?
create table customer
(
? ? ? customerId int not null,
? ? ? name varchar(50),
? ? ? address varchar(200),
? ? ? email varchar(20),
? ? ? sex varchar(6),
? ? ? cardId varchar(20),
? ? ? constraint pk_customerId primary key(customerId)
);
?
create table purchase
(
? ? ? customerId int not null,
? ? ? goodsId int not null,
? ? ? nums int,
? ? ? constraint pk_customerId_goodsId primary key(goodsId,customerId),
? ? ? constraint fk_customerId foreign key(customerId)
? ? ? references customer(customerId),
? ? ? constraint fk_goodsId foreign key(goodsId)
? ? ? references goods(goodsId)
);
?
? ? alter table customer
? ? modify name varchar(50) not null;
?
-- 約束
-- 主鍵:primary key
-- 外鍵:foreign key
-- 唯一:unique
-- 非空:not null
?
alter table customer
add constraint uq_email unique(email);
?
alter table customer
modify sex varchar(6) default '男';
?
轉載于:https://www.cnblogs.com/wanglisong/p/6903162.html
總結
以上是生活随笔為你收集整理的mysql DDL数据定义语言的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: typedef struct 是什么意思
- 下一篇: pip install mysql-co