- sql腳本 ---表結構設定
點擊查看代碼
if exists(select * from sys.objects where name='Department' and type='U')
drop table Department
create table Department
(
--id identity(x,y) 初始x 自增y,primary key 主鍵
DepartmentId int primary key identity(1,1),
--名稱
DepartmentName nvarchar(50) not null,
--描述
DepartmentRemark text
)
--字串
--char(x) 占用x個位元組 ‘ab’=>x個位元組
--varchar(x) 最多占用x個位元組 ‘ab’=>2個(x>2)
--text 長文本
--char text varchar 前面加n;存盤unicode 對中文友好
--例子
--varchar(100) 100個字母或者50個漢字
--nvarchar(100) 100個字母或者100個漢字
--Rank是關鍵字了,所以加上[]
create table [Rank]
(
--id identity(x,y) 初始x 自增y,primary key 主鍵
RankId int primary key identity(1,1),
--名稱
RankName nvarchar(50) not null,
--描述
RankRemark text
)
if exists(select * from sys.objects where name='People' and type='U')
drop table People
create table People(
--id identity(x,y) 初始x 自增y,primary key 主鍵
PeopleId int primary key identity(1,1),
--部門 references 參考外鍵 參考在部門表的id范圍之內
DepartmentId int references Department(DepartmentId)not null ,
--職員
RankId int references [Rank](RankId)not null,
--名稱
PeopleName nvarchar(50) not null,
--性別 加上約束(check) 默認
PeopleSex nvarchar(1)default('男') check(PeopleSex='男' or PeopleSex='女')not null,
--老版本date 新版本有time 加上samll就是表示現在時間 不能表示未來
PeopleBirth smalldatetime not null,
--小數的使用 float 可能有誤差 decimal(x,y) 長度x,小數y位
PeopleSalary decimal(12,2) check(PeopleSalary>=1000 and PeopleSalary<=100000)not null,
--電話 約束unique 唯一 電話是唯一的
PeoplePhone varchar(20)unique not null,
--地址
PeopleAddress varchar(300),
--添加時間,獲取當前時間默認值,有一個函式 getdate()
PeopleAddTime smalldatetime default(getdate()) not null
)
--修改表結構---
--(1)CRUD列
--alter table 表名 add 新列名 資料型別
--添加列 員工表添加郵箱列
alter table People add Mail varchar(200)
--alter table 表名 drop column 列名
--洗掉列 員工表洗掉郵箱列
alter table People drop column Mail
--alter table 表名 alter column 列名 資料型別
--修改地址為 200
alter table People alter column PeopleAddress varchar(200)
--維護約束---(洗掉 添加)
--洗掉約束
--alter table 表名 drop constraint 約束名
--alter table People drop constraint xxx
--添加約束
--alter table 表名 add constraint 約束名 check(運算式)
--alter table People add constraint psadad check(PeopleSex='男' or PeopleSex='女')
--添加主鍵 唯一 默認 外鍵
--alter table 表名 add constraint 約束名 primary key (列名)
insert into Department(DepartmentName,DepartmentRemark)
values('市場部','..........')
insert into Department(DepartmentName,DepartmentRemark)
values('軟體部','..........')
insert into Department(DepartmentName,DepartmentRemark)
values('企劃部','..........')
insert into Department(DepartmentName,DepartmentRemark)
values('銷售部','..........')
--最好一一對應 如果列順序改變就回值出問題
--一次性插入多行資料
- 表內容 crud
點擊查看代碼
use Mytest
select *from Department
select * from [Rank]
select * from People
--資料crud
--修改
--update 表名 set 列名='xxx' where 條件
--update 表名 set 列名='xxx', 列名='yyy' where 條件
update Department set DepartmentName='經理部' where DepartmentId=8
update People set PeopleSalary=PeopleSalary+1000
update People set PeopleSalary=15000 where RankId=3 and PeopleSalary<=15000
update People set PeopleSalary=PeopleSalary*2,PeopleAddress='西京' where PeopleName='關羽7'
--delete drop truncate區別
--drop table xxx 洗掉表物件xxx
--delete from xxx 洗掉表xxx的資料, 即表物件和結構都存在
--truncate table xxx 洗掉資料(清空資料)即表物件和結構都存在
--truncate 清空所有資料,不能有資料, delete 可以洗掉所有資料也可以帶條件洗掉符合條件的資料
--自動編號 1,2,3,4,5
--truncate 編號為:1,2,3,4,5 (重頭開始)
--delete 編號將為6,7,8,9,10(繼續)
--查詢 as可省略 ,distinct()去重,
select PeopleName 姓名 from People
select distinct(PeopleAddress) from People
select PeopleSalary*1.2 加薪后,PeopleSalary 加薪前 from People
--按照 名字長度的前5個查詢
select top 5 PeopleName from People order by len(PeopleName)
--按照 名字長度的前25%查詢
select top 25 percent PeopleName from People order by len(PeopleName)
--按照 名字長度的后25%查詢
select top 25 percent PeopleName from People order by len(PeopleName) desc
--查詢為null的值的單位
select *from People where PeopleAddress is null;
--查詢非空的值 和‘ ’區別 null就是沒填 ‘ ’insert 有欄位但是空白
select *from People where PeopleAddress is not null;
--查詢時間的 1988到2000的
select * from People where PeopleBirth>'1988-8-7' and PeopleBirth<'2000-3-1'
select * from People where PeopleBirth between '1988-8-8'and '2000-3-1'
select * from People where year(PeopleBirth) between 1988 and 2000
--查詢年紀在多少歲的
select PeopleName,year(GETDATE())-YEAR(PeopleBirth) 年齡 from People
--查詢年紀在35歲以下的
select PeopleName,year(GETDATE())-YEAR(PeopleBirth) 年齡 from People where year(GETDATE())-YEAR(PeopleBirth)<35 and PeopleSalary >5000
select* from People where (MONTH(PeopleBirth)=11 and DAY(PeopleBirth)<=22) or (MONTH(PeopleBirth)=12 and DAY(PeopleBirth)<=31)
--子查詢
select *from People where PeopleAddress=
(select PeopleAddress from People where PeopleName='關羽')
--龍 8
--鼠 4
--?? 3
select 2000%12
select *,
case
when YEAR(PeopleBirth)%12=4 then '鼠'
when YEAR(PeopleBirth)%12=5 then '牛'
when YEAR(PeopleBirth)%12=6 then '虎'
when YEAR(PeopleBirth)%12=7 then '兔'
when YEAR(PeopleBirth)%12=8 then '龍'
when YEAR(PeopleBirth)%12=9 then '蛇'
when YEAR(PeopleBirth)%12=10 then '馬'
when YEAR(PeopleBirth)%12=11 then '羊'
when YEAR(PeopleBirth)%12=0 then '猴'
when YEAR(PeopleBirth)%12=1 then '雞'
when YEAR(PeopleBirth)%12=2 then '狗'
when YEAR(PeopleBirth)%12=3 then '豬'
end 生肖
from People
--優化
select *,
case YEAR(PeopleBirth)%12
when 4 then '鼠'
when 5 then '牛'
when 6 then '虎'
when 7 then '兔'
when 8 then '龍'
when 9 then '蛇'
when 10 then '馬'
when 11 then '羊'
when 0 then '猴'
when 1 then '雞'
when 2 then '狗'
when 3 then '豬'
end 生肖
from People
select *from People where PeopleAddress not like '%南%'
--查詢名字2個字的 第一個字為劉
select * from People where SUBSTRING(PeopleName,1,1)='劉' and LEN(PeopleName)=2
select * from People where SUBSTRING(PeopleName,2,1)='備' and LEN(PeopleName)=2
select *from People where PeoplePhone like '1516%'
--開頭4位是1516 第五位為1或者2 最后一位為9
select *from People where PeoplePhone like '1516[1,2]%9'
--開頭4位是1516 第五位為1到8 最后一位為不為4到8
select *from People where PeoplePhone like '1516[1-8]%[^4-8]'
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/548436.html
標籤:SQL Server
上一篇:從數倉發展史淺析數倉未來技術趨勢
