Professional Documents
Culture Documents
To vi cc rng buc create table SinhVien ( MaSV int primary key, TenSV nvarchar(50) not null, QueQuan int references DiaPhuong(MaDP) )
update SinhVien set NhomTruong = 4 where (MaSV > 3 and MaSV < 8)
4) Truy vn (query)
H tr truy vn: distinct, top, as, identity Php ton tp hp: in, like, between Cc hm tng nhm: sum, max, min, avg
select: Hin mt s ct
select TenSV, DiemTB from SinhVien
select..where (select)
Hin tt c nhng ngi trong bng nhn vin c lng bng lng ln nht ca nhng ngi c trong cng ty: select TenNV, Luong from NhanVien where Luong = (select max(Luong) from NhanVien)
select..where (in)
Hin tt c nhng ngi trong bng nhn vin c lng ln nht hoc ln nh ca nhng ngi c trong cng ty:. select TenNV, Luong from NhanVien where Luong in (select top 2 Luong from NhanVien order by Luong) Cu lnh select trong s to ra mt tp hai gi tr (top 2) l lng ln nht v lng ln nh. V cu lnh select th nht s chn ra nhng ngi m lng nm trong tp ln nht v ln nh
select NhanVien.TenNV, DiaPhuong.TenDP from NhanVien right outer join DiaPhuong on NhanVien.QueQuan = DiaPhuong.MaDP
thuc v. V vy xem cc ct trong mt bng d liu, chng ta c th truy vn bng syscolumns declare @x int set @x = (select id from sysobjects where name='Employees') select * from syscolumns where id = @x Trong v d ny, ta khai bo bin @x kiu int, sau ly id ca bng Employees vo bin @x, ri ta ly tt c cc ct trong bng syscolumns m c id bng @x, cng c ngha l id ca bng Employees. Do vy ta s lit k c tt c cc ct trong bng Employees
S dng bng tm
Khai bo bng, cp nht bng, v hin bng declare @x table(MaSV int, TenSV nvarchar(20)) insert into @x values(1, N'Thng') insert into @x values(2, N'Yn') select * from @x Khai bo bng, ti bng khc t trong c s d liu ln bng va khai bo, v hin bng declare @x table(MaSV int, TenSV nvarchar(20)) insert into @x select EmployeeID, firstname from Northwind.dbo.Employees select * from @x
S dng bin tm
declare @t money set @t = (select max(UnitPrice) from Northwind.dbo.products) select * from Northwind.dbo.products where UnitPrice >= @t
Cc vn ca lu tr
+ S d tha (redundance) + S nht qun (consistence) + Tnh ton vn (integrity)
Cc khi nim c bn
+ C s d liu (database) + Bng (table) hoc quan h (relation) + Bn ghi (record) hoc dng (row) hoc b (tuple) + Trng (field) hoc ct (column) + Mi quan h (relationship) c biu hin bng bng (table) hoc kha (key)
Mi quan h gia cc
+ Ti sao phi lu mi quan h + Phng php lu cc mi quan h (dng kha, dng bng) + Kha chnh (primary key) v kha ngoi (foreign key) + Vn ton vn tham chiu (referential integrity)
Cc h tr vi ton vn
+ Rng buc kim tra (check constraint): phi tha mn iu kin no + Rng buc mc nh (default constraint): phi c mt gi tr mc nh + Rng buc duy nht (unique constraint): cc gi tr trong ct khng c trng lp + Rng buc kha ngoi (foreign key constraint): cc gi tr phi hp l vi ct kha chnh tng ng + Rng buc kha chnh (primary key constraint): phi duy nht v khng trng + Thuc tnh nh danh (identity property): c th c tng t ng + Thuc tnh khng trng (not null property): khng c php trng
Cc kiu d liu
Gi tr nh phn: bit
Cc s nguyn: bigint, int, smallint, tinyint Cc s thc xp x: float, real Gi tr s thc chnh xc: decimal, numberic Gi tr tin t: money, smallmoney Gi tr hi gian: datetime, smalldatetime Cc chui: char, varchar, text, nchar, nvarchar, ntext, Cc chui nh phn: binary, varbinary, image Cc kiu khc: cursor, table, variant, timestamp, uniqueidentifier