體驗(yàn)一下MySQL與SQL sever的不同吧蓖宦!
這是我總結(jié)的一些SQL sever的一些基本語(yǔ)句
數(shù)據(jù)庫(kù)操作:
create database 名
use 名
drop database 名
創(chuàng)建視圖及表格:連接查詢P41
create view 視圖名 as--將以下語(yǔ)句形成的select形成一個(gè)視圖款违,以下用on
select 學(xué)生.*,系.系編號(hào),成績(jī)--普通的查閱視圖應(yīng)將on改為where
into 新表明--將所形成的一個(gè)select建一個(gè)表
from 系 inner join 學(xué)生 inner join 選課 inner join 課程--inner隆夯,right,left,full
on 選課.課程編號(hào)=課程.課程編號(hào)
on 選課.學(xué)號(hào)=學(xué)生.學(xué)號(hào)
on 系.系編號(hào)=學(xué)生.系編號(hào)--順序要相對(duì)應(yīng)
查找內(nèi)容:P38P43
select * from 學(xué)生 as 另外名 where 性別=‘男’ order by 月費(fèi) ASC--正向desc逆向
select 系.系編號(hào)蹄衷,系.系名忧额,count(*) as 學(xué)生數(shù) from 學(xué)生 inner join 系--查詢學(xué)生數(shù)大于500的系的系編號(hào)和系名
on 學(xué)生.系編號(hào)=系.系編號(hào) group by 系.系編號(hào) having count(*)>500--having用來(lái)搜索分組group數(shù)據(jù),where用來(lái)搜索個(gè)別數(shù)據(jù)
select top 1 姓名愧口,年齡 from 學(xué)生 order by 年齡 desc --查詢年齡最大的學(xué)生的姓名與年齡
select top 10 percent with ties 姓名睦番,年齡 from 學(xué)生 order by 年齡--同上年齡前10%的同學(xué),with ties制定最后一個(gè)記錄相同者也返回
創(chuàng)建表格:
create table 表名(
學(xué)號(hào) tinyint constrain pk_學(xué)生 primary key耍属,
年級(jí) int as left(學(xué)號(hào)抡砂,4)+‘級(jí)’
性別 sex constrain ck_學(xué)生_性別 check(性別 in(‘男’,‘女’))恬涧,--建立check約束及約束名
余額 money as 單價(jià)*價(jià)格
費(fèi)用編號(hào) int constrain fk_學(xué)生_費(fèi)用編號(hào) foreign key references 費(fèi)用表(費(fèi)用編號(hào))注益,
費(fèi)用日期 datetime not null default ‘2018-12-12’,--非空否則填默認(rèn)值
備注text溯捆,
primary key(學(xué)號(hào)丑搔,費(fèi)用編號(hào))--定義雙主鍵)
二進(jìn)制binary,varbinary提揍,image啤月;字符char,varchar劳跃,text谎仲;Unicode數(shù)據(jù)nchar;整型bigint刨仑,int郑诺,smallint,tinyint杉武;精確性decimal辙诞,float貨幣型money,smallmoney轻抱;日期型datetime飞涂,smalldatetime;bit型P7
編輯表格內(nèi)容:
insert into 學(xué)生 values(’‘祈搜,’‘较店,null)或insert into 學(xué)生(屬性1,2,3) values(’‘,’‘容燕,’‘)
delete 學(xué)生 where 號(hào)碼=’123454‘
update 學(xué)生 set 月費(fèi)=月費(fèi)+50 where 號(hào)碼=‘534533’
更改表格屬性:
alter table 學(xué)生 add 身高 decimal(4梁呈,1) not null default 18.5
alter table 學(xué)生 drop column 性別
alter table 學(xué)生 alter column 月費(fèi) money
excute SP_rename ‘表.原名’,‘新名’缰趋,’類型‘--類型可以為database捧杉,object表陕见,column屬性,index索引
更改表格約束:
alter table 學(xué)生 add constrain? fk_學(xué)生_費(fèi)用編號(hào) foreign key (費(fèi)用編號(hào)) references 費(fèi)用表(費(fèi)用編號(hào))
alter table 學(xué)生 add constrain? def_學(xué)生_備注 default ’‘ for 備注
alter table 學(xué)生 add check(性別 in(’男‘味抖,’女‘))
alter table 學(xué)生 add primary key(學(xué)號(hào))--添加約束
alter table 學(xué)生 drop constrain pk_學(xué)生--刪除約束
定義變量并循環(huán):
declare @n int
set @n=1
while(@n<100)
begin
語(yǔ)句
endP33
創(chuàng)建索引:
create index unique 索引名 on 表(列)
drop index 表.索引名