SQL語句實例
1.創建一個新的數據庫
Create database newdatabase
1)新創建一個數據庫:
CREATE DATABASEnewdb
ON
(
NAME= newdb_dat, --文件的邏輯名
FILENAME= 'c: \newdb.mdf', --文件存放的物理位置
SIZE= 5MB, --文件的初始大小
MAXSIZE= 100MB, --文件最大存儲空間
FILEGROWTH= 5MB --文件增長大小
)
LOG ON
(
NAME= newdb_log,
FILENAME= 'c: \newdb.ldf',
SIZE= 10MB,
MAXSIZE= 100MB,
FILEGROWTH= 10MB
)
GO
2)創建一個新的表
Create table newtable
(
id int primary key not null, --設定主鍵不爲空
name nvarchar(10) not null, --設定不爲空
birthday smalldatetime
)
2.插入數據
Insert into table --在表中插入一條記錄
Values ('001','張三','1980-9-8')
3.修改數據
Update table --把原來姓名爲'李四'修改成了'張三'
Set name = '張三'
Where name= '李四'
Update student --將所有學生的年齡都增加1歲
Set age = age+1
4.刪除數據(注意:語句DELETE和DROP TABLE之間有一個非常重要的區別。DELETE刪除一個表中的一部分或全部的內容。而DROP TABLE則將表的內容和表的大綱全都一起刪除。這樣,在執行了DELETE語句之後,表依然存在於數據庫中,儘管表中或許已沒有行了,但在執行DROP TABLE語句後,表將不復存在)
Delete from student --刪除學生表中所有性別爲’女’的學員信息
Where sex = '女'
Delete from student --刪除學生表中學號爲’002’的學員信息
Where stid = ’002’
5.修改表
Alter table student
Add age char(20)null --在表中添加了age列
Drop column name --在表中刪除了name列
Alter column age smallint --在表中修改了age列的數據類型爲半字長整數
修改主鍵:alter table [lis] drop pk_lis
查看主鍵名:exec sp_pkeys'lis'
6.修改列名
EXEC sp_rename '要修改的表名.[要修改的字段名]','修改後的字段名','COLUMN'
7.刪除表
Drop table student --在數據庫中刪除了student表
簡單查詢
1.查詢學生表中的所有數據
Select * --‘*’代表所有全部列
from student
2.查詢學生表中的name列
Select name
from student
3.查詢學生表中的name列和stid列
Select stid, name --查詢多列時,用’,’隔開
from student
4.查詢學生表中姓名爲'張三'的學號
Select id
From student
Where name='張三' --查詢條件使用Where子句實現
5.從學生表中查詢出前5列的學員信息
Select TOP5 * --top5代表前5行數據
From student
6.查詢學生表中姓名不爲'張三'的學號
Select id
From student
Where name <> '張三' --where name != '張三'
7.查詢學生表中年齡大於24歲的學員姓名
Select name
From student
Where age > 24
8.查詢學生表中年齡在22至24歲之間的學員姓名
Select name
From student
Where age >= 22 and age <= 24--where agebetween 22 and 24
9.查詢學生表中學號爲'001'和'008'的學員姓名
Select name
From student
Where id=001 or id=008--where idin(001,008)
10.查詢學生表中年齡小於22歲,而大於24歲的學員姓名
Select name
From student
Where age < 22 or age > 24--where age not between 22 and 24
11.查詢學生表中班級編號爲’001’,而年齡大於24歲的學員姓名
Select name
From student
Where classid=’001’ and age > 24
12.查詢學生表中的所有的班級編號(不要重複)
Select distinct classid -- distinct關鍵字從select語句的結果中除去重複的行
From student
13.NOT操作的優先級最高,AND其次,OR最低
比較以下兩個語句的不同:
Selec t *
From student
Where age > 22 and age < 24
or classid = '001' and sex = '男'
Select *
From student
Where ((age>22 and age<24)
or classid='001') and sex='男'
14.查詢不屬於班級編號爲'S1T01'的所有學員姓名
Select name
From student
Where not classid='S1T01'
15.查詢學生表中班級編號爲空的學員姓名
Select name
From student
Where classid is null
16.查詢學生表中班級編號不爲空的學員姓名
Select name
From student
Where classid is not null
17.查詢學生總人數
Select count(*)
From student
18.計算學員的平均年齡
Select avg(age)
From student
19.查詢學員的最大年齡
Select max(age)
From student
20.查詢學生表中的所有數據,並以年齡降序排列
Select *
From student
Order by age desc--asc爲升序
21.計算每個地址中不同城市的數目
Select address as 城市, count(address)
From student
Group by address
22.HAVING子句定義了用於行組的條件判斷。HAVING子句對行組來說所具有的意義,與WHERE子句對於每一單獨的行所具有的意義是相同的。
1)計算每個班級中學員的最大年齡,並按班號從大到小排列,使用下面的語句:
SELECTclassid, MAX(age) as 最大年齡
FROM student
GROUPBY classid
ORDER BY classid desc
2)要返回平均年齡在22到24之間的班級編號,使用下面的查詢語句:
SELECTclassid as 班號,AVG(age)as 平均年齡
FROM student
GROUPBY classid
HAVINGAVG(age) between 22 and 24
23.LIKE操作符用於將列的值與某個特定的模式做比較。列的數據類型可以是任何字符或者日期型數據。
通配符 %:any 代表零個或多個任意字符 _:single代表某一個任意字符
1)查詢所有姓名以李開頭的學員的姓名和編號
Select id,name
From student
Where name like '李%'
2)查詢所有姓名中第二個是'國'的學員的姓名和編號
Select id,name
From student
Where name like '_國%'
3)查詢所在地名稱是以C到F的字符打頭的所有部門的詳細資料
Select *
From department
Where address like '[C-F]%'
4)查詢姓名開頭不是'李'開頭的學員的姓名和編號
Select stid,name
From student
Where name not like '李%'
5)查詢姓(firstname)的打頭字母不是J,K,L,M,N,O,並且名(lastname)的打頭字母不是E或者Z的所有學生的編號和名字
Select stid,name
From student
Where firstname like '[^J-O]%'
and lastname like '[^EZ]%'
24.子查詢
查詢班級名稱爲'S1T01'的學員姓名
Select name
From student
Where classid in/=
(
Select classid
From class
Where name='S1T01'
)
25.多表查詢
1)合併union
USE NORTHWIND
SELECT ContactName,city,postalcode
FROM customers
UNION
SELECT lastname + ' ' + firstname ,city,postalcode
FROM employees
2)內聯接
Select s.name,c.name
From student as s inner join class as c
On s.classid=c.classid
/* Select s.name,c.name
From student as s,class as c
Where s.classid=c.classid */
3)外聯接
a)左外聯接
Select s.name,c.name
From student as s left join class as c
On s.classid=c.classid
b)右外聯接
Select s.name,c.name
From student as s right join class as c
On s.classid=c.classid
c)完全聯接
Select s.name,c.name
From student as s full join class as c
On s.classid=c.classid
d)交叉聯接
Select s.name,c.name
From student as s cross join class as c
或者select s.name,c.name
fromstudent as s ,class as c
4)自聯接
查詢學生表中的所有學生的上級領導姓名
Select 學生表.name as 學生姓名, 領導表.name as 領導姓名
From student as 學生表 left join student as 領導表
On 學生表.leaderid=領導表.stid