--建立數據庫表以及插入數據 15:40:34
USE master go
if exists(select * from sysdatabases where name='EmployeeSys') drop database EmployeeSys go
CREATE DATABASE EmployeeSys ON ( NAME = ' EmployeeSys_data', --主數據文件的邏輯名
FILENAME = 'D:\EmployeeSys_data.mdf' , --主數據文件的物理名
SIZE = 10 MB, --主數據文件初始大小
FILEGROWTH = 20 % ) LOG ON ( NAME = 'EmployeeSys_log', FILENAME = 'D:\EmployeeSys_log.ldf' , SIZE = 3MB, MAXSIZE = 20MB, FILEGROWTH = 1MB ) GO
USE EmployeeSys go
if exists(select * from sysobjects where name='Employee') drop table Employee go
create table Employee ( EmpId int identity(1,1) primary key not null, EmpName varchar(50) not null, Age int not null, Sex char(2) not null, AddTime datetime not null, Wage money not null, DepId int not null, State int not null ) if exists(select * from sysobjects where name='Department') drop table Department go
create table Department ( DepId int identity(1,1) primary key not null, DepName varchar(50) not null ) if exists(select * from sysobjects where name='Users') drop table Users go
create table Users ( Uid int identity(1,1) primary key not null, UName varchar(50) not null, UPwd varchar(50) not null ) ALTER TABLE Employee --默認約束
ADD CONSTRAINT DF_State DEFAULT ('未刪除') FOR State ALTER TABLE Employee --添加外鍵約束
ADD CONSTRAINT FK_Department FOREIGN KEY(DepId) REFERENCES Department(DepId) insert into Department values('人事部'); insert into Department values('產品開發部'); insert into Department values('開發部'); insert into Department values('經理部'); insert into Department values('空運部'); insert into Employee values('張三',23,'男','2011-9-1',5000,1,0); insert into Employee values('李四',29,'男','2010-6-1',7000,2,1); insert into Employee values('王五',15,'男','2012-5-1',6000,3,0); insert into Employee values('李麗',25,'女','2000-3-1',2000,4,1); insert into Employee values('李寧',18,'女','2011-10-1',4000,5,1); insert into Users values('admin','admin'); select * from Employee; select * from Department; select * from Users;
--數據庫表的基本增刪改查操做
select * from Department; select * from Employee; select * from Users; select * from student; --問題:當 IDENTITY_INSERT 設置爲 OFF 時,不能爲表 'student' 中的標識列插入顯式值。 --解決:其中student爲表名。意思是容許將顯示值插入到標識列中。
set identity_insert student on; --增
insert into Department(DepName) values(''); --插入單行數據 insert into 表名(列名) values (列值)
select EmpId,EmpName into student from Employee; --直接拿現有表數據建立一個新表並填充 select 新建表列名 into 新建表名 from 原表名
insert into student(EmpId,EmpName) select Uid,UName from Users; --將現有表數據添加到一個已有表 insert into 已有的新表(列名) select 原表列名 from 原表名
insert student(EmpId,EmpName) select '11','tom' union select '12','like'; --使用union關鍵字合併數據進行插入多行 insert 表名(列名) select 列值 union select 列值
--刪
drop table student; --刪除該表
truncate table student; --注意:刪除表數據,但表的結構、列、約束、索引等不會被刪除;不能用於有外建約束引用的表 truncate table <表名>
delete from Department where DepId=8; --刪除<知足條件的>行 delete from 表名 where 刪除條件
--改
update Department set DepName='空姐部' where DepId=5; --根據條件修改表數據 update <表名> set <列名=更新值> [where <更新條件>]
--查
select * from Employee where EmpName='李四'; --精確查詢
select * from Employee where EmpName like '%李%' order by Age asc; --使用like進行模糊查詢 desc降序 asc升序
select * from Department where DepName is null; --查詢null行
select * from Department where DepName is not null; --查詢非null的行
select * from Employee where EmpId between 1 and 3; --使用between在某個範圍內進行查詢 1-3條數據
select * from Employee where Age in('23','15'); --in查詢 查詢表Employee中age爲23和15的數據
select EmpId as ID,EmpName as 姓名 from Employee; --在查詢中使用AS更改列名
select EmpId,'1' as ID2 from Employee; --在查詢中使用常量 查詢表Employee,顯示EmpId列,並添加地址列爲ID2,其列值都爲'1'
select sex as 性別,AVG(Age) as 平均年齡 from Employee group by sex; --使用group by進行分組查詢 在表Employee中查詢,按sex字段分組
select sex as 性別,AVG(Age) as 平均年齡 from Employee group by Sex having COUNT(Age)>1; --使用having子句進行分組篩選 顯示分組後count(Age)>1的行,因爲where只能在沒有分組時使用,分組後只能使用having來限制條件。
select top 3 * from Employee; --查詢前3行的全部數據
select top 3 percent * from Employee; --查詢該表3%的數據,percent爲關鍵字
select d.DepName,e.EmpName from Department d,Employee e where d.DepId=e.DepId; --多表鏈接查詢
select e.EmpName,d.DepName from Employee e inner join Department d on e.DepId=d.DepId;--內鏈接 若是表中有至少一個匹配,則返回行 select * from 表1 inner join 表2 on 表1.id=表2.id
select * from Employee e left join Department d on e.DepId=d.DepId; --左鏈接 即便右表中沒有匹配,也從左表返回全部的行 left join
select * from Employee e right join Department d on e.DepId=d.DepId; --右鏈接 即便左表中沒有匹配,也從右表返回全部的行 right join
select * from Employee e full join Department d on e.DepId=d.DepId; --徹底鏈接 只要其中一個表中存在匹配,則返回行 full join
select * from Department where DepId in(select DepId from Employee where Sex='男'); --子查詢
select top 2 * from Employee where EmpId not in (select top 2 EmpId from Employee order by Age asc)order by EmpId--分頁 查詢根據Age排序後三、4條數據
select EmpName from student group by EmpName having COUNT(EmpName)>1;--查詢EmpName字段記錄重複1條以上的數據 --查詢EmpName字段記錄重複1條以上的數據,而且刪除EmpId最大的那個數據 (Min刪除EmpId最大的數據,Max刪除EmpId最小的數據)
delete from student where EmpName in(select EmpName from student group by EmpName having COUNT(EmpName)>1) and EmpId not in(select Min(EmpId) from student group by EmpName having COUNT(EmpName)>1) --內外鏈接定義 --SQL內連接:將2張表按照on的條件,返回公共部分 --SQL外鏈接: 包含左連接和右鏈接
--INNER JOIN:若是表中有至少一個匹配,則返回行 --LEFT JOIN:即便右表中沒有匹配,也從左表返回全部的行 --RIGHT JOIN:即便左表中沒有匹配,也從右表返回全部的行 --FULL JOIN:只要其中一個表中存在匹配,則返回行
--存儲過程、遊標以及臨時表等操做
select * from Department; select * from Employee; select * from Users; select * from student; --存儲過程 --=============================================================================================== --簡單賦值
declare @a int
set @a=5
print @a
--使用select語句賦值
declare @b nvarchar(50) select @b='張三'
print @b
declare @b1 nvarchar(50) select @b1=EmpName from student where EmpId=18
print @b1
--使用update語句賦值
declare @b2 nvarchar(50) update student set @b2=EmpName where EmpId=18
print @b2
--=============================================================================================== --表、臨時表、表變量 --刪除臨時表
drop table #DB_U;; drop table #DB_U2; drop table #DB_U3; --建立臨時表
create table #DB_U( [id][int]not null, [name][nvarchar](5)not null ); insert into #DB_U(id,name) values(1,'tom');--向臨時表插入信息
select * into #DB_U2 from #DB_U where id<8;--從#DB_U表查詢數據填充到新生成的#DB_U2表
select * from #DB_U2 where id<3 union select * from #DB_U;--兩臨時表聯合查詢
insert into #DB_U select * from student;--將查詢表的數據插入到臨時表中
alter table #DB_U add [myid] int not null identity(1,1);--添加一個新列myid,自增加字段
alter table #DB_U add[myid1] uniqueidentifier not null default(newid());--添加一個新列myid1,默認全球惟一標識
--給查詢結果集增長自增加列
select IDENTITY(int,1,1) as id into #DB_U3 from student;--無主鍵時
select(select SUM(1) from student where EmpId<=a.id) as myid from #DB_U a order by myid;--有主鍵時 --=============================================================================================== --定義表變量
declare @t table( id int not null, msg nvarchar(50) null ) insert into @t values(1,'1'); insert into @t values(2,'2'); select * from @t
select * from #DB_U; select * from #DB_U2; select * from #DB_U3; --=============================================================================================== --循環 --while循環
declare @a int
declare @sum int
set @a=1
set @sum =0
while @a<=100
begin
set @sum +=@a
set @a+=1
end
print @sum
--=============================================================================================== --條件語句 --if,else條件分支
if(1+1=2) begin
print '對'
end
else
begin
print '錯'
end
--when then條件分支
declare @today int
declare @week nvarchar(3) set @today=3
set @week= case
when @today=1 then '星期一'
when @today=2 then '星期二'
when @today=3 then '星期三'
when @today=4 then '星期四'
when @today=5 then '星期五'
when @today=6 then '星期六'
when @today=7 then '星期日'
else '值錯誤'
end
print @week
--=============================================================================================== --遊標
declare @EmpId int
declare @EmpName varchar (50) --定義一個遊標
declare user_cur cursor for select EmpId,EmpName from student --打開遊標
open user_cur while @@fetch_status=0
begin
--讀取遊標
fetch next from user_cur into @EmpId,@EmpName
print @EmpId
end
--關閉遊標
close user_cur --摧毀遊標
deallocate user_cur --=============================================================================================== --觸發器 --建立觸發器 --在student上建立<strong>INSERT觸發器</strong>stu_insert, --要求在student表中插入記錄時(要求每次只能插入一條記錄),這個觸發器都將更新Users表中的UName列。並測試觸發器stu_insert。
create trigger stu_insert on student for insert
as
update Users set UName=UName+1
where Uid=(select Uid from inserted) --插入日誌表
insert into student values ('嘻嘻嘻') --刪除觸發器
drop trigger stu_insert --=============================================================================================== --存儲過程 --建立帶output參數的存儲過程
CREATE PROCEDURE PR_Sum @a int, @b int, @sum int output AS
BEGIN
set @sum =@a+@b
END
--建立Return返回值存儲過程
CREATE PROCEDURE PR_Sum2 @a int , @b int
AS
BEGIN
Return @a+@b
END
--執行存儲過程獲取output型返回值
declare @mysum int
execute PR_Sum 21,22,@mysum output print @mysum
--執行存儲過程獲取Return型返回值
declare @mysum2 int
execute @mysum2= PR_Sum2 1,2
print @mysum2
--=============================================================================================== --自定義函數 --新建標量值函數
create function FUNC_Sum1( @a int, @b int ) returns int
as
begin
return @a+@b
end
--調用標量值函數
declare @s int
set @s=dbo.FUNC_Sum1(100,50) print @s
--刪除標量值函數
drop function FUNC_Sum1 -------------------------------------------- --新建內聯表值函數
create function FUNC_UserTab_1( @myId int ) returns table
as
return ( select * from student where EmpId<@myId) --調用表值函數
select * from dbo.FUNC_UserTab_1(15) -------------------------------------------- --新建多語句表值函數
create function FUNC_UserTab_2( @myId int ) returns @t table( [EmpId] [int] NOT NULL , [EmpName] [nvarchar](8) NOT NULL ) as
begin
insert into @t select * from student where EmpId<@myId
return
end
--調用多語句表值函數
select * from dbo.FUNC_UserTab_2(15)--更改字段類型長度ALTER TABLE 表名 ALTER COLUMN 列名 VARCHAR(255)