-- ********** create database ********** ---- ********** Begin ********** --createdatabase school
-- ********** End ********** --
go
use school
go
-- ********** create table ********** ---- ********** Begin ********** --createtable teacher(ID intnotnull,Name varchar(20)notnull,sex char(2)notnull,Phone varchar(20)null)-- ********** End ********** --
go
SET NOCOUNT ON-- ********** insert ********** ---- ********** Begin ********** --insertinto teacher values(1,"Lucy",'F',null)-- ********** End ********** --
go
数据的删除
-- ********** create database ********** ---- ********** Begin ********** --createdatabase website
-- ********** End ********** --
go
use website
go
-- ********** create table ********** ---- ********** Begin ********** --createtable shopping(
ID intidentity(1,1)notnull,
Name varchar(20)notnull,
address varchar(30)notnull)-- ********** End ********** --
go
SET NOCOUNT ONinsertinto shopping (Name, address)values('eBay','www.ebay.com')
go
SET NOCOUNT ON-- ********** insert ********** ---- ********** Begin ********** --insertinto shopping (Name, address)values('amazon','www.amazon.com')-- ********** End ********** --
go
SET NOCOUNT ON-- ********** delete ********** ---- ********** Begin ********** --deletefrom shopping where ID =1-- ********** End ********** --
go
数据的更改
-- ********** create database ********** ---- ********** Begin ********** --createdatabase Books
-- ********** End ********** --
go
use Books
go
-- ********** create table ********** ---- ********** Begin ********** --createtable prices(
ID intidentity(1,1)notnull,
Name varchar(20)notnull,
price varchar(30)notnull)-- ********** End ********** --
go
SET NOCOUNT ON-- ********** insert ********** ---- ********** Begin ********** --insertinto prices (Name,price)values('Harry Potter','$128')-- ********** End ********** --
go
SET NOCOUNT ONinsertinto prices (Name, price)values('Walden','$5')
go
SET NOCOUNT ON-- ********** update ********** ---- ********** Begin ********** --update prices
set price ='$6'where Name ='Walden'-- ********** End ********** --
go
AVG() 函数的使用
USE Mall
GO
SET NOCOUNT ON------ return two columns that the price bigger than average price -------- ********** Begin ********** --select prod_name,prod_price
from Products
where prod_price>(selectavg(prod_price)from Products
)-- ********** End ********** --
GO
COUNT() 函数的使用
USE Mall
GO
SET NOCOUNT ON------ return the number of product which price bigger than 10 ------- ********** Begin ********** --selectcount(prod_price)from Products where prod_price >10-- ********** End ********** --
GO
MAX() 函数和 MIN() 函数的使用
USE Mall
GO
SET NOCOUNT ON------ return the price of the least expensive item -------- ********** Begin ********** --select prod_name,prod_price from Products where prod_price =(selectmin(prod_price)from Products
)-- ********** End ********** --
GO
SUM() 函数的使用
USE Mall
GO
SET NOCOUNT ON------ return the amount of all products -------- ********** Begin ********** --selectsum(prod_price * quantity)as amount
from Products
-- ********** End ********** --
GO
带 WHERE 子句的多表查询
USE Mall
GO
SET NOCOUNT ON--********** Begin **********--select*from Products p,Vendors v where p.vend_id = v.vend_id
--********** End **********--
GO
内连接查询
USE Mall
GO
SET NOCOUNT ON--********** Begin **********--select p.*,v.vend_name,v.vend_phone
from Products p innerjoin Vendors v
on p.vend_id = v.vend_id
--********** End **********--
GO