學習要點:
SQL之-建庫、建表、建約束、關系SQL基本語句大全.txt舉得起放得下叫舉重,舉得起放不下叫負重。頭要有勇氣,抬頭要有底氣。學習要加,驕傲要減,機會要乘,懶惰要除。人生三難題:思,相思,單相思。
SQL之-建庫、建表、建約束、關系、部分T-sql語句
---創建庫 創建庫之前 先進行 查看數據庫中是否 已存在 次數據庫 有便刪除 --- 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:/技能抽查試題第二模塊(數據庫)/試題——1/任務一/ConstructionDB_date.mdf',size=3mb,maxsize=10mb,filegrowth=5% --增長速度為)log on(name='ConstructionDB_log',filename='E:/技能抽查試題第二模塊(數據庫)/試題——1/任務一/ConstructionDB_date.ldf',size=2mb,maxsize=5mb,filegrowth=1mb)--使用T-SQL語句創建表use ConstructionDBgo---查詢 庫中是否存在 此表 存在則刪除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 --如果不存在該表,則進行創建--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, --時限URL varchar(64) not null, --二級菜單鏈接 備注 varchar(256) not null, )---流程類別表create table T_flow_type(Flow_type_id char(3) not null, --流程類別號 Flow_type_name varchar(64) not null, --流程類別名稱 In_method_id char(3) not null, --招標方式代號 In_choice_id char(3) not null, --項目選項代號 備注 varchar(256) not null, )---標段情況表create table T_sub_project(Project_id varchar(32) not null, ---工程編號 Sub_pro_id char(2) not null, -- 標段編號 Flow_type_id char(3) not null, --流程類別號 Sub_pro_name varchar(64) not null,--標段名稱(招標項目名稱) Usb_no varchar(64) not null, --密碼鎖號In_method_id char(3) not null, --招標方式代號 In_scope_id char(3) not null, --招標范圍代號 In_choice_id char(3) not null, --項目選項代號 Proj_type_id char(3) not null, --項目性質代號 Engi_type_id char(1) not null, --工程性質代號Pack_type char(1) not null, ---發包方式 Grade_type_idv char(1) not null,--評分類別號Flag_done char(1) not null,--完成標志 Flag_forcebreak char(1) not null,--強制中斷標志 備注 varchar(256) not null,)--創建一個數據庫名為‘sql_test'create database sql_testgo --打開數據庫 sql_testuse sql_testgo--建立學生表create table 學生(學生編號 char(4) primary key, 學生名字 varchar(50)not null)go--修改學生表alter table 學生 add 班級編號 char(4) null --添加班級編號字段-- (注意如果添加的字段不為空的話,是不能被添加的)go--建立班級表create table 班級(班級編號 char(4) primary key ,班級名稱 varchar(50)not null)go--建立課程表create table 課程(課程編號 char(4) primary key ,課程名稱 varchar(50) not null,開課日期 datetime )go--修改課程表alter table 課程add 課程代號 varchar(10) null --添加課程代號字段goalter table 課程drop column 開課日期 --刪除開課日期字段goalter table 課程alter column 課程名稱 varchar(20) not null --修改課程名稱字段go--建立一個product_test_one 表,與下個表類似,只不過在constraint前面有個‘逗號'不影響執行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--建立一個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--建立一個student表,使其中的 name 字段具有唯一性create table student (id char(8), name char(10) --表字段constraint pk_id primary key (id), --添加一個主鍵約束 constraint uk_name unique (name) --添加一個唯一性約束)go--建立一個student4表,同上 (注意:constraint 與constraint 之間一定要有逗號,否則出錯!)create table student4 (id char(8), name char(10) --表字段constraint pk_id4 primary key (id), constraint uk_name4 unique (name))go-- 刪除表student4drop table student4go--建立一個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(邏輯條件表達式)--創建一個‘員工‘表,使其輸入的性別字段(sex)只能接受‘m'或則‘f',而不能接受其他數據--并且為phone字段創建檢查約束,限制只能輸入類似0108564712之類的數據,而不能隨意輸入其他數據create table 員工(id char(5),name char(20),sex char(2),phone intconstraint pk_zid primary key (id), --此間一定要有‘逗號'分隔 ,定義主鍵約束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 約束表達式 [for 字段名]-- 創建一個表‘默認約束',為字段sex創建默認約束create table 默認約束(id char(5) primary key ,sex varchar(2) constraint con_sex default ‘m‘ )go--修改‘默認約束'表alter table 默認約束add name varchar(10)null constraint con_name default ‘你好寶貝‘ --增加一個字段為‘name',默認值為‘你好寶貝'go--往班級表里添加8條記錄insert into 班級 values(‘bj01‘,‘一班‘)insert into 班級 values(‘bj02‘,‘二班‘)insert into 班級 values(‘bj03‘,‘三班‘)insert into 班級 values(‘bj04‘,‘四班‘)insert into 班級 values(‘bj05‘,‘五班‘)insert into 班級 values(‘bj06‘,‘六班‘)insert into 班級 values(‘bj07‘,‘七班‘)insert into 班級 values(‘bj08‘,‘八班‘)go--顯示班級所以記錄select * from 班級go--刪除班級表里班級編號大于bj06的記錄delete from 班級 where 班級編號>‘bj06‘go--顯示班級所以記錄select * from 班級go--向學生表里添加記錄insert into 學生 values(‘xs01‘,‘one‘,‘bj01‘)insert into 學生 values(‘xs02‘,‘two‘,‘bj01‘)insert into 學生 values(‘xs03‘,‘three‘,‘bj01‘)insert into 學生 values(‘xs04‘,‘four‘,‘bj02‘)insert into 學生 values(‘xs05‘,‘five‘,‘bj03‘)insert into 學生 values(‘xs06‘,‘six‘,‘bj02‘)insert into 學生 values(‘xs07‘,‘seven‘,‘bj04‘)insert into 學生 values(‘xs08‘,‘eight‘,‘bj03‘)insert into 學生 values(‘xs09‘,‘nine‘,‘bj04‘)insert into 學生 values(‘xs10‘,‘ten‘,‘bj05‘)insert into 學生 values(‘xs11‘,‘eleven‘,‘bj06‘)insert into 學生 values(‘xs12‘,‘twleve‘,‘bj06‘)go--顯示學生所有的記錄select * from 學生go--連接查詢select * from 學生,班級 where 學生.班級編號=班級.班級編號go--以下效果同上一條相同--選擇的連接查詢select 學生.學生編號,班級.班級編號, 學生.學生名字,班級.班級名稱 from 學生,班級 where 學生.班級編號=班級.班級編號go--以下效果同上一條相同--查詢一班的學生select* from 學生 where 班級編號 in(select 班級編號 from 班級 where 班級編號=‘bj01‘)go--與上面一條查詢語句一樣功能select a.學生編號,a.學生名字,a.班級編號 from 學生 as a ,班級 as b where a.班級編號=b.班級編號 and b.班級編號=‘bj01‘go--統計一班學生人數select count(學生編號)as 學生統計 from 學生 where 班級編號 in(select 班級編號 from 班級 where 班級編號=‘bj01‘)go--group的用法和count()函數的用法--統計一班學生人數,并顯示學生的名字和所在班級select count(學生編號)as 學生統計, 學生名字,班級編號 from 學生 where 班級編號 in(select 班級編號 from 班級 where 班級編號=‘bj01‘)group by 班級編號,學生名字go
以上所述是小編給大家介紹的SqlServer編寫數據庫表的操作方式(建庫、建表、修改語句),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答