學(xué)習(xí)要點(diǎn):
SQL之-建庫(kù)、建表、建約束、關(guān)系SQL基本語(yǔ)句大全.txt舉得起放得下叫舉重,舉得起放不下叫負(fù)重。頭要有勇氣,抬頭要有底氣。學(xué)習(xí)要加,驕傲要減,機(jī)會(huì)要乘,懶惰要除。人生三難題:思,相思,單相思。
SQL之-建庫(kù)、建表、建約束、關(guān)系、部分T-sql語(yǔ)句
---創(chuàng)建庫(kù) 創(chuàng)建庫(kù)之前 先進(jìn)行 查看數(shù)據(jù)庫(kù)中是否 已存在 次數(shù)據(jù)庫(kù) 有便刪除 --- if exists(select * from sys.sysdatabases where name='ConstructionDB')begin use master drop database ConstructionDB end go create database ConstructionDB on()if exists(select * from sysobjects where name ='ConstructionDB') --查找命令drop DATABASE ConstructionDB --刪除 命令Create database ConstructionDBon(name='ConstructionDB_date',filename='E:/技能抽查試題第二模塊(數(shù)據(jù)庫(kù))/試題——1/任務(wù)一/ConstructionDB_date.mdf',size=3mb,maxsize=10mb,filegrowth=5% --增長(zhǎng)速度為)log on(name='ConstructionDB_log',filename='E:/技能抽查試題第二模塊(數(shù)據(jù)庫(kù))/試題——1/任務(wù)一/ConstructionDB_date.ldf',size=2mb,maxsize=5mb,filegrowth=1mb)--使用T-SQL語(yǔ)句創(chuàng)建表use ConstructionDBgo---查詢 庫(kù)中是否存在 此表 存在則刪除if exists(select * from sysobjects where name = 'T_flow_step_def') drop table T_flow_step_def--- 方法二IF OBJECT_ID (N'bas_CardType') IS NULLBEGIN --如果不存在該表,則進(jìn)行創(chuàng)建--drop table com_CodeRecord--流程步驟定義表 create table T_flow_step_def(Step_no int not null, --流程步驟ID Step_name varchar(30) not null, --流程步驟名稱 Step_des varchar(64) not null, --流程步驟描述Limit_time int not null, --時(shí)限URL varchar(64) not null, --二級(jí)菜單鏈接 備注 varchar(256) not null, )---流程類別表create table T_flow_type(Flow_type_id char(3) not null, --流程類別號(hào) Flow_type_name varchar(64) not null, --流程類別名稱 In_method_id char(3) not null, --招標(biāo)方式代號(hào) In_choice_id char(3) not null, --項(xiàng)目選項(xiàng)代號(hào) 備注 varchar(256) not null, )---標(biāo)段情況表create table T_sub_project(Project_id varchar(32) not null, ---工程編號(hào) Sub_pro_id char(2) not null, -- 標(biāo)段編號(hào) Flow_type_id char(3) not null, --流程類別號(hào) Sub_pro_name varchar(64) not null,--標(biāo)段名稱(招標(biāo)項(xiàng)目名稱) Usb_no varchar(64) not null, --密碼鎖號(hào)In_method_id char(3) not null, --招標(biāo)方式代號(hào) In_scope_id char(3) not null, --招標(biāo)范圍代號(hào) In_choice_id char(3) not null, --項(xiàng)目選項(xiàng)代號(hào) Proj_type_id char(3) not null, --項(xiàng)目性質(zhì)代號(hào) Engi_type_id char(1) not null, --工程性質(zhì)代號(hào)Pack_type char(1) not null, ---發(fā)包方式 Grade_type_idv char(1) not null,--評(píng)分類別號(hào)Flag_done char(1) not null,--完成標(biāo)志 Flag_forcebreak char(1) not null,--強(qiáng)制中斷標(biāo)志 備注 varchar(256) not null,)--創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)名為‘sql_test'create database sql_testgo --打開(kāi)數(shù)據(jù)庫(kù) sql_testuse sql_testgo--建立學(xué)生表create table 學(xué)生(學(xué)生編號(hào) char(4) primary key, 學(xué)生名字 varchar(50)not null)go--修改學(xué)生表alter table 學(xué)生 add 班級(jí)編號(hào) char(4) null --添加班級(jí)編號(hào)字段-- (注意如果添加的字段不為空的話,是不能被添加的)go--建立班級(jí)表create table 班級(jí)(班級(jí)編號(hào) char(4) primary key ,班級(jí)名稱 varchar(50)not null)go--建立課程表create table 課程(課程編號(hào) char(4) primary key ,課程名稱 varchar(50) not null,開(kāi)課日期 datetime )go--修改課程表alter table 課程add 課程代號(hào) varchar(10) null --添加課程代號(hào)字段goalter table 課程drop column 開(kāi)課日期 --刪除開(kāi)課日期字段goalter table 課程alter column 課程名稱 varchar(20) not null --修改課程名稱字段go--建立一個(gè)product_test_one 表,與下個(gè)表類似,只不過(guò)在constraint前面有個(gè)‘逗號(hào)'不影響執(zhí)行create table product_test_one(id char(10) not null, name varchar(20) null, price money default 20.5,quantity smallint null, constraint pk_id primary key clustered (id))go--建立一個(gè)product_test_two 表create table product_test_two(id char(10) not null, name varchar(20) null, price money default 20.5,quantity smallint null constraint pk_id2 primary key clustered (id))go--刪除表 pruduct_test_one表drop table product_test_onego--建立一個(gè)student表,使其中的 name 字段具有唯一性create table student (id char(8), name char(10) --表字段constraint pk_id primary key (id), --添加一個(gè)主鍵約束 constraint uk_name unique (name) --添加一個(gè)唯一性約束)go--建立一個(gè)student4表,同上 (注意:constraint 與constraint 之間一定要有逗號(hào),否則出錯(cuò)!)create table student4 (id char(8), name char(10) --表字段constraint pk_id4 primary key (id), constraint uk_name4 unique (name))go-- 刪除表student4drop table student4go--建立一個(gè)student3表,同上create table student3(id char(8), name char(10), --表字段constraint pk_id3 primary key (id) ,constraint uk_name3 unique (name))go--刪除表student3drop table student3go--constraint 約束名 check(邏輯條件表達(dá)式)--創(chuàng)建一個(gè)‘員工‘表,使其輸入的性別字段(sex)只能接受‘m'或則‘f',而不能接受其他數(shù)據(jù)--并且為phone字段創(chuàng)建檢查約束,限制只能輸入類似0108564712之類的數(shù)據(jù),而不能隨意輸入其他數(shù)據(jù)create table 員工(id char(5),name char(20),sex char(2),phone intconstraint pk_zid primary key (id), --此間一定要有‘逗號(hào)'分隔 ,定義主鍵約束constraint chk_sex check (sex in (‘f‘,‘m‘) ),constraint chk_phone check (phone like ‘(010) [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]‘))go--constraint 約束名 default 約束表達(dá)式 [for 字段名]-- 創(chuàng)建一個(gè)表‘默認(rèn)約束',為字段sex創(chuàng)建默認(rèn)約束create table 默認(rèn)約束(id char(5) primary key ,sex varchar(2) constraint con_sex default ‘m‘ )go--修改‘默認(rèn)約束'表alter table 默認(rèn)約束add name varchar(10)null constraint con_name default ‘你好寶貝‘ --增加一個(gè)字段為‘name',默認(rèn)值為‘你好寶貝'go--往班級(jí)表里添加8條記錄insert into 班級(jí) values(‘bj01‘,‘一班‘)insert into 班級(jí) values(‘bj02‘,‘二班‘)insert into 班級(jí) values(‘bj03‘,‘三班‘)insert into 班級(jí) values(‘bj04‘,‘四班‘)insert into 班級(jí) values(‘bj05‘,‘五班‘)insert into 班級(jí) values(‘bj06‘,‘六班‘)insert into 班級(jí) values(‘bj07‘,‘七班‘)insert into 班級(jí) values(‘bj08‘,‘八班‘)go--顯示班級(jí)所以記錄select * from 班級(jí)go--刪除班級(jí)表里班級(jí)編號(hào)大于bj06的記錄delete from 班級(jí) where 班級(jí)編號(hào)>‘bj06‘go--顯示班級(jí)所以記錄select * from 班級(jí)go--向?qū)W生表里添加記錄insert into 學(xué)生 values(‘xs01‘,‘one‘,‘bj01‘)insert into 學(xué)生 values(‘xs02‘,‘two‘,‘bj01‘)insert into 學(xué)生 values(‘xs03‘,‘three‘,‘bj01‘)insert into 學(xué)生 values(‘xs04‘,‘four‘,‘bj02‘)insert into 學(xué)生 values(‘xs05‘,‘five‘,‘bj03‘)insert into 學(xué)生 values(‘xs06‘,‘six‘,‘bj02‘)insert into 學(xué)生 values(‘xs07‘,‘seven‘,‘bj04‘)insert into 學(xué)生 values(‘xs08‘,‘eight‘,‘bj03‘)insert into 學(xué)生 values(‘xs09‘,‘nine‘,‘bj04‘)insert into 學(xué)生 values(‘xs10‘,‘ten‘,‘bj05‘)insert into 學(xué)生 values(‘xs11‘,‘eleven‘,‘bj06‘)insert into 學(xué)生 values(‘xs12‘,‘twleve‘,‘bj06‘)go--顯示學(xué)生所有的記錄select * from 學(xué)生go--連接查詢select * from 學(xué)生,班級(jí) where 學(xué)生.班級(jí)編號(hào)=班級(jí).班級(jí)編號(hào)go--以下效果同上一條相同--選擇的連接查詢select 學(xué)生.學(xué)生編號(hào),班級(jí).班級(jí)編號(hào), 學(xué)生.學(xué)生名字,班級(jí).班級(jí)名稱 from 學(xué)生,班級(jí) where 學(xué)生.班級(jí)編號(hào)=班級(jí).班級(jí)編號(hào)go--以下效果同上一條相同--查詢一班的學(xué)生select* from 學(xué)生 where 班級(jí)編號(hào) in(select 班級(jí)編號(hào) from 班級(jí) where 班級(jí)編號(hào)=‘bj01‘)go--與上面一條查詢語(yǔ)句一樣功能select a.學(xué)生編號(hào),a.學(xué)生名字,a.班級(jí)編號(hào) from 學(xué)生 as a ,班級(jí) as b where a.班級(jí)編號(hào)=b.班級(jí)編號(hào) and b.班級(jí)編號(hào)=‘bj01‘go--統(tǒng)計(jì)一班學(xué)生人數(shù)select count(學(xué)生編號(hào))as 學(xué)生統(tǒng)計(jì) from 學(xué)生 where 班級(jí)編號(hào) in(select 班級(jí)編號(hào) from 班級(jí) where 班級(jí)編號(hào)=‘bj01‘)go--group的用法和count()函數(shù)的用法--統(tǒng)計(jì)一班學(xué)生人數(shù),并顯示學(xué)生的名字和所在班級(jí)select count(學(xué)生編號(hào))as 學(xué)生統(tǒng)計(jì), 學(xué)生名字,班級(jí)編號(hào) from 學(xué)生 where 班級(jí)編號(hào) in(select 班級(jí)編號(hào) from 班級(jí) where 班級(jí)編號(hào)=‘bj01‘)group by 班級(jí)編號(hào),學(xué)生名字go
以上所述是小編給大家介紹的SqlServer編寫(xiě)數(shù)據(jù)庫(kù)表的操作方式(建庫(kù)、建表、修改語(yǔ)句),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選