教学思路SQL之预备课程学习 建库、建表、建约束、关系、部分T-sql语句
生活随笔
收集整理的這篇文章主要介紹了
教学思路SQL之预备课程学习 建库、建表、建约束、关系、部分T-sql语句
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
--建庫 if exists(select * from sys.sysdatabases where name='wf')
begin
?use master
?drop database wf
end
go create database wf
on
?(name=N'wf',filename=N'E:\MyCode\ETC收費站\ETC收費站\ETC_Data\wf.mdf',size=3mb,maxsize=unlimited,filegrowth=1)
log on
?(name=N'wf',filename=N'E:\MyCode\ETC收費站\ETC收費站\ETC_Data\wf_log.ldf',size=3mb,maxsize=unlimited,filegrowth=1) go use wf
go if exists(select * from sys.sysobjects where??name='wf' )
begin
?drop table?wf
?end
--建表、建約束、關系 use wf?
go?
create table tableok?
(?
col1 int,?
col2_notnull int not null,?
col3_default nchar(1) not null default('男'), --默認男
col4_default datetime not null default(getdate()), --默認得到系統時間
col5_check?? int??????not null check(col5_check>=18 and col5_check<=55), --添加約束,數據值在18到55之間
col6_check?? nchar(9) not null check(col6_check like 'msd0902[0-9][^6-9]'), --添加約束,數據值前7位必須是‘msd0902’,倒數第兩位可以是0-9中任意一個數字,最后一位不是6-9之間的數字。
cola_primary nchar(5) not null primary key, --建立主鍵
colb_unique int unique, --唯一約束
col7_Identity int???? not null identity(100,1), --自增長,從100開始,每列值增加1個
col8_identity numeric(5,0) not null identity(1,1) --自增長,從1開始,每列值增加1個,最大值是5位的整數
col9_guid uniqueidentifier not null default(newid()) --使用newid()函數,隨機獲取列值
)?
--alter?
--主外鍵/引用/關系??約束?
alter table 從表名 [with check]--啟用 with nocheck--禁用約束?
add constraint FK_主表名_從表名?
foreign key (從表中的字段名) references 主表名 (主表中的字段名)?
--其它非主外鍵約束?
alter table wf?
add constraint 約束名 約束類型 具體的約束說明?
alter table wf--修改聯合主鍵?
add constraint Pk_cola_primary primary key(cola_primary,col1)?
---1.insert 【into】 <表名>【列名】 values <值列表>?
--1)全部【列名】 可以省略?
insert stuinfo values('','','')?
create table outtable?
(?
??id int not null??primary key,?
[name] nvarchar(4) not null,?
adrress nvarchar(20) default '地址不詳'?
)?
truncate table outtable?
alter table outtable alter column id int??identity(4,1)?
insert outtable values (1,22,'')?
insert outtable values(2,32,'')?
insert outtable values(3,33,'')?
--2)部分?
insert stuinfo (stuno) values ('')?
--3)自動增長列,不能手動插入。將列名、列值省略?
---2多行插入?
---1)從一個現有表中取出所取字段插入到目標表中?
insert into stuinfo (stuno,stuname)??select stuno,stuname from stumarks?
insert into stuinfo(stuno) select stuno from stumarks?
---2)從現有的表中取出數據,創建一個新表,將數據插入到新表中?
select [列名] into <表名> from <源表名>?
select id as '編號',[name]??into newtable from outtable?
select cast(id as varchar(4))+cast([name] as nvarchar(4)) as '編號及姓名' ,id into newtable from outtable?
select * from newtable?
drop table newtable?
---3)union 現有的多個表中取數據放入現有的表3中?
insert into<表名3> [列名]?
select * from 表1 union?
select * from 表2?
--3.更新語句 update <表名> set <列名=更新值> [where <更新條件>]?
---注意:一般where不要省略,如不寫將修改整個表?
--4truncate刪除數據?
--比delete只能全部刪除數據,不能部分刪除,刪除的效率高,可以重置自增長列?
select * from stumarks?
select * from stuinfos?
--給考試成績各提5分,100分封頂。?
update stumarks set writtenexam=100?
where writtenexam>95?
update stumarks set labexam=100?
where labexam>95?
update stumarks?
set writtenexam=writtenexam+5?
where writtenexam+5<=100?
update stumarks?
set labexam=labexam+5?
where labexam+5<=100?
select examno,stuno,writtenexam+5 as 筆試,labexam+5??as 機試 from stumarks where writtenexam+5<=100 and labexam+5<=100?
create table t?
(?
id int ,?
name??nchar(4),?
dt datetime,?
age int,?
score int?
)?
insert t values (1,'a',getdate(),20,50)?
insert t values (2,'b',getdate(),21,60)?
insert t values (3,'c',getdate(),21,100)?
insert t values (4,'d',getdate(),23,80)?
select * from t?
select top 2 * from t?
select top 60 percent * from t?
select top 5 * from products?
select top 5 percent * from products?
--啟別名:兩種方式 As(可省略) 、=?
select productid as '編號' from products?
select productid??'編號' from products?
select '編號'=productid??from products???
select employeeid from employees?
--declare @a nvarchar(6)?
--set @a='員工編號為:'?
select N'員工編號為:'+cast(employeeid as nvarchar(2)) from employees?
select distinct country from suppliers order by country?
select * from t?
select * from products?
select * from products order by 4 asc,6 desc?
select * from products where unitprice>16 and productname like 'T%' or productid=16?
select * from suppliers where country in('japan','italy')?
(1)char、varchar、text和nchar、nvarchar、ntext
????????char和varchar的長度都在1到8000之間,它們的區別在于char是定長字符數據,而varchar是變長字符數據。所謂定長就是長度固定的,當輸入的數據長度沒有達到指定的長度時將自動以英文空格在其后面填充,使長度達到相應的長度;而變長字符數據則不會以空格填充。text存儲可變長度的非Unicode數據,最大長度為2^31-1(2,147,483,647)個字符。
?????? 后面三種數據類型和前面的相比,從名稱上看只是多了個字母"n",它表示存儲的是Unicode數據類型的字符。寫過程序的朋友對Unicode應該很了解。字符中,英文字符只需要一個字節存儲就足夠了,但漢字眾多,需要兩個字節存儲,英文與漢字同時存在時容易造成混亂,Unicode字符集就是為了解決字符集這種不兼容的問題而產生的,它所有的字符都用兩個字節表示,即英文字符也是用兩個字節表示。nchar、nvarchar的長度是在1到4000之間。和char、varchar比較:nchar、nvarchar則最多存儲4000個字符,不論是英文還是漢字;而char、varchar最多能存儲8000個英文,4000個漢字。可以看出使用nchar、nvarchar數據類型時不用擔心輸入的字符是英文還是漢字,較為方便,但在存儲英文時數量上有些損失。
(2)datetime和smalldatetime
datetime:從1753年1月1日到9999年12月31日的日期和時間數據,精確到百分之三秒。
smalldatetime:從1900年1月1日到2079年6月6日的日期和時間數據,精確到分鐘。
(3)bitint、int、smallint、tinyint和bit
bigint:從-2^63(-9223372036854775808)到2^63-1(9223372036854775807)的整型數據。
int:從-2^31(-2,147,483,648)到2^31-1(2,147,483,647)的整型數據。
smallint:從-2^15(-32,768)到2^15-1(32,767)的整數數據。
tinyint:從0到255的整數數據。
bit:1或0的整數數據。
(4)decimal和numeric
這兩種數據類型是等效的。都有兩個參數:p(精度)和s(小數位數)。p指定小數點左邊和右邊可以存儲的十進制數字的最大個數,p必須是從 1到38之間的值。s指定小數點右邊可以存儲的十進制數字的最大個數,s必須是從0到p之間的值,默認小數位數是0。
(5)float和real
float:從-1.79^308到1.79^308之間的浮點數字數據。
real:從-3.40^38到3.40^38之間的浮點數字數據。在SQL Server中,real的同義詞為float(24)。?
SELECT --從數據庫表中檢索數據行和列??
select col1,col2,col3.....from table
INSERT --向數據庫表添加新數據行??
??????insert into table(col1,col2....) values(value1,value2...)
??????insert into table(col1,col2....)??select col1,col2...from table
DELETE --從數據庫表中刪除數據行??
??????delete from table?
UPDATE --更新數據庫表中的數據??
??????update table set col1=value,......
--數據定義??
CREATE TABLE --創建一個數據庫表??
??create table tbame (col1 int,col2 char(20))
DROP TABLE --從數據庫中刪除表??
??drop table tbname
ALTER TABLE --修改數據庫表結構??
--增加列
??????alter table #a add col3 int
--刪除列
?????? alter table #a drop column col3
--增加列的默認值
alter?? table?? tbname?? add?? CONSTRAINT?? tf_name?? default?? 'a'?? for?? col2 CREATE VIEW --創建一個視圖???
???? create view view_name as
???????? select * from tbname where ....
DROP VIEW --從數據庫中刪除視圖????? drop view view_nameCREATE INDEX --為數據庫表創建一個索引???? --創建簡單的非聚集索引 CREATE INDEX index_name ????ON tbname (VendorID);???? --創建簡單的唯一非聚集索引
CREATE UNIQUE INDEX index_name
????ON tbname (VendorID); DROP INDEX --從數據庫中刪除索引??
???? drop index index_name on tbnameCREATE PROCEDURE --創建一個存儲過程?
?? create procedure name as
????begin
????......
????end
DROP PROCEDURE --從數據庫中刪除存儲過程??
?? drop procedure nameCREATE TRIGGER --創建一個觸發器??
??create trigger name on tbname?
?????? FOR INSERT,UPDATE???? AS?
?????? .......
DROP TRIGGER --從數據庫中刪除觸發器 ? 本文轉自葉子文文博客51CTO博客,原文鏈接http://blog.51cto.com/leafwf/185772如需轉載請自行聯系原作者
葉子文文
begin
?use master
?drop database wf
end
go create database wf
on
?(name=N'wf',filename=N'E:\MyCode\ETC收費站\ETC收費站\ETC_Data\wf.mdf',size=3mb,maxsize=unlimited,filegrowth=1)
log on
?(name=N'wf',filename=N'E:\MyCode\ETC收費站\ETC收費站\ETC_Data\wf_log.ldf',size=3mb,maxsize=unlimited,filegrowth=1) go use wf
go if exists(select * from sys.sysobjects where??name='wf' )
begin
?drop table?wf
?end
--建表、建約束、關系 use wf?
go?
create table tableok?
(?
col1 int,?
col2_notnull int not null,?
col3_default nchar(1) not null default('男'), --默認男
col4_default datetime not null default(getdate()), --默認得到系統時間
col5_check?? int??????not null check(col5_check>=18 and col5_check<=55), --添加約束,數據值在18到55之間
col6_check?? nchar(9) not null check(col6_check like 'msd0902[0-9][^6-9]'), --添加約束,數據值前7位必須是‘msd0902’,倒數第兩位可以是0-9中任意一個數字,最后一位不是6-9之間的數字。
cola_primary nchar(5) not null primary key, --建立主鍵
colb_unique int unique, --唯一約束
col7_Identity int???? not null identity(100,1), --自增長,從100開始,每列值增加1個
col8_identity numeric(5,0) not null identity(1,1) --自增長,從1開始,每列值增加1個,最大值是5位的整數
col9_guid uniqueidentifier not null default(newid()) --使用newid()函數,隨機獲取列值
)?
--alter?
--主外鍵/引用/關系??約束?
alter table 從表名 [with check]--啟用 with nocheck--禁用約束?
add constraint FK_主表名_從表名?
foreign key (從表中的字段名) references 主表名 (主表中的字段名)?
--其它非主外鍵約束?
alter table wf?
add constraint 約束名 約束類型 具體的約束說明?
alter table wf--修改聯合主鍵?
add constraint Pk_cola_primary primary key(cola_primary,col1)?
---1.insert 【into】 <表名>【列名】 values <值列表>?
--1)全部【列名】 可以省略?
insert stuinfo values('','','')?
create table outtable?
(?
??id int not null??primary key,?
[name] nvarchar(4) not null,?
adrress nvarchar(20) default '地址不詳'?
)?
truncate table outtable?
alter table outtable alter column id int??identity(4,1)?
insert outtable values (1,22,'')?
insert outtable values(2,32,'')?
insert outtable values(3,33,'')?
--2)部分?
insert stuinfo (stuno) values ('')?
--3)自動增長列,不能手動插入。將列名、列值省略?
---2多行插入?
---1)從一個現有表中取出所取字段插入到目標表中?
insert into stuinfo (stuno,stuname)??select stuno,stuname from stumarks?
insert into stuinfo(stuno) select stuno from stumarks?
---2)從現有的表中取出數據,創建一個新表,將數據插入到新表中?
select [列名] into <表名> from <源表名>?
select id as '編號',[name]??into newtable from outtable?
select cast(id as varchar(4))+cast([name] as nvarchar(4)) as '編號及姓名' ,id into newtable from outtable?
select * from newtable?
drop table newtable?
---3)union 現有的多個表中取數據放入現有的表3中?
insert into<表名3> [列名]?
select * from 表1 union?
select * from 表2?
--3.更新語句 update <表名> set <列名=更新值> [where <更新條件>]?
---注意:一般where不要省略,如不寫將修改整個表?
--4truncate刪除數據?
--比delete只能全部刪除數據,不能部分刪除,刪除的效率高,可以重置自增長列?
select * from stumarks?
select * from stuinfos?
--給考試成績各提5分,100分封頂。?
update stumarks set writtenexam=100?
where writtenexam>95?
update stumarks set labexam=100?
where labexam>95?
update stumarks?
set writtenexam=writtenexam+5?
where writtenexam+5<=100?
update stumarks?
set labexam=labexam+5?
where labexam+5<=100?
select examno,stuno,writtenexam+5 as 筆試,labexam+5??as 機試 from stumarks where writtenexam+5<=100 and labexam+5<=100?
create table t?
(?
id int ,?
name??nchar(4),?
dt datetime,?
age int,?
score int?
)?
insert t values (1,'a',getdate(),20,50)?
insert t values (2,'b',getdate(),21,60)?
insert t values (3,'c',getdate(),21,100)?
insert t values (4,'d',getdate(),23,80)?
select * from t?
select top 2 * from t?
select top 60 percent * from t?
select top 5 * from products?
select top 5 percent * from products?
--啟別名:兩種方式 As(可省略) 、=?
select productid as '編號' from products?
select productid??'編號' from products?
select '編號'=productid??from products???
select employeeid from employees?
--declare @a nvarchar(6)?
--set @a='員工編號為:'?
select N'員工編號為:'+cast(employeeid as nvarchar(2)) from employees?
select distinct country from suppliers order by country?
select * from t?
select * from products?
select * from products order by 4 asc,6 desc?
select * from products where unitprice>16 and productname like 'T%' or productid=16?
select * from suppliers where country in('japan','italy')?
(1)char、varchar、text和nchar、nvarchar、ntext
????????char和varchar的長度都在1到8000之間,它們的區別在于char是定長字符數據,而varchar是變長字符數據。所謂定長就是長度固定的,當輸入的數據長度沒有達到指定的長度時將自動以英文空格在其后面填充,使長度達到相應的長度;而變長字符數據則不會以空格填充。text存儲可變長度的非Unicode數據,最大長度為2^31-1(2,147,483,647)個字符。
?????? 后面三種數據類型和前面的相比,從名稱上看只是多了個字母"n",它表示存儲的是Unicode數據類型的字符。寫過程序的朋友對Unicode應該很了解。字符中,英文字符只需要一個字節存儲就足夠了,但漢字眾多,需要兩個字節存儲,英文與漢字同時存在時容易造成混亂,Unicode字符集就是為了解決字符集這種不兼容的問題而產生的,它所有的字符都用兩個字節表示,即英文字符也是用兩個字節表示。nchar、nvarchar的長度是在1到4000之間。和char、varchar比較:nchar、nvarchar則最多存儲4000個字符,不論是英文還是漢字;而char、varchar最多能存儲8000個英文,4000個漢字。可以看出使用nchar、nvarchar數據類型時不用擔心輸入的字符是英文還是漢字,較為方便,但在存儲英文時數量上有些損失。
(2)datetime和smalldatetime
datetime:從1753年1月1日到9999年12月31日的日期和時間數據,精確到百分之三秒。
smalldatetime:從1900年1月1日到2079年6月6日的日期和時間數據,精確到分鐘。
(3)bitint、int、smallint、tinyint和bit
bigint:從-2^63(-9223372036854775808)到2^63-1(9223372036854775807)的整型數據。
int:從-2^31(-2,147,483,648)到2^31-1(2,147,483,647)的整型數據。
smallint:從-2^15(-32,768)到2^15-1(32,767)的整數數據。
tinyint:從0到255的整數數據。
bit:1或0的整數數據。
(4)decimal和numeric
這兩種數據類型是等效的。都有兩個參數:p(精度)和s(小數位數)。p指定小數點左邊和右邊可以存儲的十進制數字的最大個數,p必須是從 1到38之間的值。s指定小數點右邊可以存儲的十進制數字的最大個數,s必須是從0到p之間的值,默認小數位數是0。
(5)float和real
float:從-1.79^308到1.79^308之間的浮點數字數據。
real:從-3.40^38到3.40^38之間的浮點數字數據。在SQL Server中,real的同義詞為float(24)。?
SELECT --從數據庫表中檢索數據行和列??
select col1,col2,col3.....from table
INSERT --向數據庫表添加新數據行??
??????insert into table(col1,col2....) values(value1,value2...)
??????insert into table(col1,col2....)??select col1,col2...from table
DELETE --從數據庫表中刪除數據行??
??????delete from table?
UPDATE --更新數據庫表中的數據??
??????update table set col1=value,......
--數據定義??
CREATE TABLE --創建一個數據庫表??
??create table tbame (col1 int,col2 char(20))
DROP TABLE --從數據庫中刪除表??
??drop table tbname
ALTER TABLE --修改數據庫表結構??
--增加列
??????alter table #a add col3 int
--刪除列
?????? alter table #a drop column col3
--增加列的默認值
alter?? table?? tbname?? add?? CONSTRAINT?? tf_name?? default?? 'a'?? for?? col2 CREATE VIEW --創建一個視圖???
???? create view view_name as
???????? select * from tbname where ....
DROP VIEW --從數據庫中刪除視圖????? drop view view_nameCREATE INDEX --為數據庫表創建一個索引???? --創建簡單的非聚集索引 CREATE INDEX index_name ????ON tbname (VendorID);???? --創建簡單的唯一非聚集索引
CREATE UNIQUE INDEX index_name
????ON tbname (VendorID); DROP INDEX --從數據庫中刪除索引??
???? drop index index_name on tbnameCREATE PROCEDURE --創建一個存儲過程?
?? create procedure name as
????begin
????......
????end
DROP PROCEDURE --從數據庫中刪除存儲過程??
?? drop procedure nameCREATE TRIGGER --創建一個觸發器??
??create trigger name on tbname?
?????? FOR INSERT,UPDATE???? AS?
?????? .......
DROP TRIGGER --從數據庫中刪除觸發器 ? 本文轉自葉子文文博客51CTO博客,原文鏈接http://blog.51cto.com/leafwf/185772如需轉載請自行聯系原作者
葉子文文
總結
以上是生活随笔為你收集整理的教学思路SQL之预备课程学习 建库、建表、建约束、关系、部分T-sql语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Day5---D4:合规和审计管理
- 下一篇: 分享《组播技术白皮书》