sql腳本 表結構設置 點擊查看代碼 if exists(select * from sys.objects where name='Department' and type='U') drop table Department create table Department ( --id iden ...
- 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
--