国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
SqlServer數(shù)據(jù)庫語句大全(四)

4.數(shù)據(jù)定義DATA DEFINE LANGUAGE
4.1表Table
4.2列Column
4.3序列Indentity
4.4約束Constraints
4.5索引Index
4.6視圖view
4.7權(quán)限Privilege
/**********************************************************/
4.1表Table
4.1.1.建立表
語法:
create table <表名>(
<列名> <數(shù)據(jù)類型> [長度] <,>
<列名...>
)

代碼:
//建立公司部門表
create table tb_basic_dept(
id int not null,
name varchar(20) ,
chair varchar(20)
)

4.1.2.刪除表
語法:
drop table <表名>
代碼:
//刪除部門表
drop table tb_basic_dept
-----------------------------------------------------------
4.2列Column
4.2.1.列添加
語法:
alter table <表名> add
<列名> <數(shù)據(jù)類型> [長度] <,>
<列名...>

代碼:
alter table tb_basic_dept add
remark varchar(50)
4.2.2.列刪除
語法:alter table <表名> drop column <列名>
代碼:
alter table tb_basic_dept drop column remark

4.2.3.列修改
語法:alter table <表名> alter column
<列名> <數(shù)據(jù)類型> [長度] [null | not null]

代碼:
//修改工資列為dec(8,2)
alter table tb_hr_gz alter column gz dec(8,2) null
-----------------------------------------------------------
4.3序列Identity
//特別要求
IDENTITY字段數(shù)據(jù)類型只能是(int, bigint, smallint, tinyint, decimal, or numeric(x,0))
IDENTITY字段必須是not null約束

4.3.1Identity
語法:
IDENTITY(<data_type> [, <seed>, <increment>]) AS column_name,

代碼:
//使用Identity
CREATE TABLE MyTable (
key_col int NOT NULL IDENTITY (1,1),
abc char(1) NOT NULL
)
INSERT INTO MyTable VALUES ('a')
INSERT INTO MyTable VALUES ('b')
INSERT INTO MyTable VALUES ('c')

-----------------------------------------------------------
4.4約束Constraints
4.4.1缺省約束(default)
4.4.2非空約束(not null)
4.4.3規(guī)則約束(rule)
4.4.4檢查約束(check)
4.4.5唯一約束(unique)
4.4.6主鍵約束(primary key)
4.4.7外鍵約束(foreign key)
4.4.8商業(yè)規(guī)則(business rule)

以下面兩個(gè)表為例進(jìn)行演示
create table tb_hr_bm(
bm varchar(20) not null ,
remark varchar(100) default ''
)
create table tb_hr_gz(
id int not null,
name varchar(30) not null,
hrid char(18) null,
workage int null ,
bm varchar(20) null,
gz real null,
remark varchar(100) null
)
hrid=身份證號(hào)碼
workage=工作年數(shù)
gz=工資金額
-----------------------------------------------------------
4.4.1缺省約束(default)
語法:CREATE DEFAULT default_name AS expression
代碼:CREATE DEFAULT zip_default AS 94710
-----------------------------------------------------------
4.4.2非空約束(not null)
//表的主鍵和其它必填字段必須為not null.
語法:create table (column-name datatype not null... )
代碼:create table tb_hr_gz(id int not null,...)
-----------------------------------------------------------
4.4.3規(guī)則約束(rule)
語法:CREATE RULE rulename AS condition
代碼:
//郵編號(hào)碼6位100000-999999
//建立一個(gè)自定義zip類型
CREATE TYPE zip FROM CHAR(6) NOT NULL
//建立一個(gè)規(guī)則約束
CREATE RULE zip_rule AS @number >100000 and @number < 999999
//綁定規(guī)則約束到zip類型
EXEC sp_bindrule zip_rule, 'zip'
//應(yīng)用自定義zip類型
2> CREATE TABLE address(
city CHAR(25) NOT NULL,
zip_code ZIP,
street CHAR(30) NULL
)

-----------------------------------------------------------

4.4.4檢查約束(建立/刪除)
//檢查約束建立
語法:
alter table name
add constraint <檢查約束名> check<取值范圍表達(dá)式>

代碼:
//工資添加取值范圍0 ~ 1000000
方法1:
create table tb_hr_gz(
gz real default 0.0 check(gz >=0 and gz <=1000000),
...
)
方法2:
alter table tb_hr_gz
add constraint tb_hr_gz_ck check(gz >=0 and gz <=1000000)

//檢查約束刪除
語法:
alter table name drop constraint <檢查約束名>
代碼:
//刪除工資的檢查約束
alter table tb_hr_gz drop constraint tb_hr_gz_ck
-----------------------------------------------------------
4.4.5唯一約束
4.4.5.1.唯一約束添加
語法:
alter table name add constraint <唯一約束名> unique<列名>
代碼:
//列如身份證號(hào)碼是唯一的!
alter table tb_hr_gz Add constraint tb_hr_gz_uk unique(hrid)

4.4.5.2.唯一約束刪除
語法:
alter table name drop constraint <唯一約束名>
代碼:
alter table tb_hr_gz drop constraint tb_hr_gz_uk

-----------------------------------------------------------
4.4.6主鍵約束
4.4.6.1主鍵約束添加
語法:
alter table table_name
add constraint <主鍵名稱> Primary Key <列名>
代碼:
create table tb_hr_bm(
bm varchar(20) not null ,
remark varchar(100) default ''
)
alter table tb_hr_bm
add constraint tb_hr_bm_pk Primary Key (bm)

4.4.6.2主鍵約束刪除
語法:
alter table table_name
drop constraint <主鍵名稱>
代碼:
alter table table_name
drop constraint tb_hr_bm_pk

-----------------------------------------------------------
4.4.7外鍵約束
4.4.7.1外鍵約束添加
語法:
alter table <表名>
add constraint <外鍵名>
foreign key(列名)
references <參考表名><列名>
<ON UPDATE|ON DELETE(RESTRICT|CASCADE|SET NULL|SET DEFAULT)>

//補(bǔ)充說明
常用選項(xiàng)是下面3項(xiàng):
ON UPDATE SET NULL //級(jí)聯(lián)更新
ON DELETE CASCADE //級(jí)聯(lián)刪除
ON DELETE SET NULL //級(jí)聯(lián)置空

ON UPDATE(RESTRICT|CASCADE|SET NULL|SET DEFAULT) 表示父表更新后,子表的行為
ON DELETE(RESTRICT|CASCADE|SET NULL|SET DEFAULT) 表示父表刪除后,子表的行為
RESTRICT 限制功能:父表一行記錄不能更新/刪除,當(dāng)子表有一條記錄以上時(shí)
CASCADE 級(jí)聯(lián)功能:父表一行記錄記錄更新/刪除刪除,子表對(duì)應(yīng)所有的記錄自動(dòng)更新/刪除
SET NULL 置空功能:父表一行記錄記錄更新/刪除刪除,子表對(duì)應(yīng)所有的記錄自動(dòng)為空
SET DEFAULT 默認(rèn)值功能:父表一行記錄記錄更新/刪除刪除,子表對(duì)應(yīng)所有的記錄自動(dòng)寫入默認(rèn)值


代碼:
建立外鍵的主要代碼
alter table tb_hr_personl_info
add constraint tb_hr_personl_info__bm_fk
foreign key(bm)
references tb_hr_bm (bm)
on update cascade
on delete cascade


//建立參考表部門
create table tb_hr_bm
(
bm varchar(20) not null ,
remark varchar(100) default ''
)
alter table tb_hr_bm
add constraint tb_hr_bm_pk Primary Key (bm)
//建立個(gè)人信息表
use hr
create table tb_hr_personl_info
(
userid int not null ,
username varchar(20) null,
bm varchar(20) null
)
/*為此表添加主鍵約束*/
alter table tb_hr_personl_info
add constraint tb_hr_personl_info_pk Primary Key (userid)
/*為個(gè)人信息表添加外鍵約束*/
alter table tb_hr_personl_info
add constraint tb_hr_personl_info__bm_fk
foreign key(bm)
references tb_hr_bm (bm)
on update cascade
on delete cascade

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
SQL MSSQL 常用代碼大全
SQL基礎(chǔ)
Access、MSSQL、MYSQL數(shù)據(jù)庫之間有什么區(qū)別
Sql Server 和 Access 操作數(shù)據(jù)庫結(jié)構(gòu)Sql語句
Oracle數(shù)據(jù)庫數(shù)據(jù)對(duì)象分析(中)--(1)
《SQL Server》之 表的創(chuàng)建和管理
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服