Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 3c

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Introduction

• SQL is a standard language for accessing and manipulating databases.


• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
• SQL became a standard of the American National Standards Institute (ANSI) in
1986, and of the International Organization for Standardization (ISO) in 1987
Create database, drop database, drop table

 CREATE DATABASE database_name


Ex:
USE Master
CREATE DATABASE QLDA
 DROP DATABASE database_name
Ex:
USE Master
DROP DATABASE QLDA
 DROP TABLE table_name
Ex:
USE QLDA
DROP TABLE SV
Create table

 CREATE TABLE table_name (


Column1 Datatype [Not null],
Column2 Datatype [Not null],
….
Primary Key (primary key) ,
Foreign Key (foreign key) References parent_table_name (primary key) )
Create table (example)

 Table: Khoa (MsKhoa, TenKhoa)


CREATE TABLE Khoa (
MsKhoa nchar(10),
TenKhoa nchar(80),
Primary Key (MsKhoa) )
 Table: BoMon(MsBM, TenBM, MsKhoa)
CREATE TABLE BoMon (
MsBM nchar(10),
TenBM nchar(80),
MsKhoa nchar(10),
Primary Key (MsBM),
Foreign Key (MsKhoa) References Khoa (MsKhoa) )
Create table (example)

 Table: SV(MsSV, HotenSV, GioiTinh, MsBM)


CREATE TABLE SV (
MsSV nchar(10),
HoTenSV nchar(80),
GioiTinh nchar(5),
MsBM nchar(10),
Primary Key (MsSV) )
Alter table

 ALTER TABLE table_name


Add column_name datatype,
Drop Column column_name,
Alter Column column_name datatype
Add Foreign Key (foreign key) References Parent_table_name (primary key)

Ex1:
ALTER TABLE Khoa
Add DiaChi nchar(80)
Ex2:
ALTER TABLE SV
Add Foreign Key (MsBM) References BoMon(MsBM)
Insert rows

❖ INSERT INTO table_name (column1, column2, column3, ...)


Values (value1, value2, value3, ...);
Ex:
INSERT INTO Khoa (MsKhoa, TenKhoa, DiaChi)
Values ('01', 'Khoa CNTT&TT', 'Khu 2')

INSERT INTO Khoa (MsKhoa, TenKhoa, DiaChi)


Values ('02', 'Khoa PTNT', 'Hoa An')

INSERT INTO BoMon(MsBM, TenBM, MsKhoa)


Values ('01', 'Bo mon CNTT', '01')

INSERT INTO BoMon(MsBM, TenBM, MsKhoa)


Values ('02', 'Bo mon CNPM', '01')
Insert rows

Ex:
INSERT INTO SV(MsSV, HoTenSV, GioiTinh, MsBM)
Values ('SV1', 'Nguyen Thi Lan', 'Nu', '02')

INSERT INTO SV(MsSV, HoTenSV, GioiTinh, MsBM)


Values ('SV2', 'Nguyen Van Diep', 'Nam', '02')

INSERT INTO SV(MsSV, HoTenSV, GioiTinh, MsBM)


Values ('SV3', 'Le Thi Hoa', 'Nu', '02')
Delete rows

❖ DELETE FROM table_name


Where conditions;

Ex:
DELETE FROM BoMon
Where TenBM='Bo mon CNTT'
Update rows

❖ UPDATE table_name
Set column1 = value1, column2 = value2, ...
Where condition;

Ex:
UPDATE Khoa
Set TenKhoa='Khoa Phat trien nong thon', DiaChi='Hoa An - Hau Giang'
Where TenKhoa='Khoa PTNT'
Select clause

 SELECT column1, column2, ... [*]


From table_name
Where conditions
Select clause (example)

 SELECT MsNV, HoTenNV, DiaChi


From NhanVien
Where HoTenNV Like 'Nguyen%'
 SELECT MsNV, HoTenNV, DiaChi, Luong
From NhanVien
Where Luong Between 2000 and 4000
 SELECT MsNV, HoTenNV, DiaChi, Luong
From NhanVien
Where Luong in (1500, 4000, 4500)
 Select MsNV, HoTenNV, DiaChi, Luong
From NhanVien
Where Luong in (1500, 4000, 4500) and HoTenNV Like 'Nguyen%'
Select clause - Inner join

 SELECT column_name(s)
From table1
Inner Join table2
On table1.column_name = table2.column_name

Ex:
SELECT *
From DonVi
Inner Join DuAn
On DonVi.MsDV = DuAn.MsDV
Select clause - Inner join

Ex:
SELECT DonVi.MsDV, TenDV, MsDA, TenDA, DiaDiem
From DonVi
Inner Join DuAn
On DonVi.MsDV = DuAn.MsDV

SELECT DonVi.MsDV, TenDV, MsDA, TenDA, DiaDiem


From DonVi
Inner Join DuAn
On DonVi.MsDV = DuAn.MsDV
Where DiaDiem = 'Ha Noi'
Select clause - Inner join

Ex:
SELECT DonVi.MsDV, TenDV, PhanCong.MsDA, TenDA, DiaDiem, SoGio
From DonVi
Inner Join DuAn
On DonVi.MsDV = DuAn.MsDV
Inner Join PhanCong
On DuAn.MSDA = PhanCong.MsDA
Where DiaDiem = 'Ha Noi'
Select clause - Inner join

Ex:
SELECT DonVi.MsDV, TenDV, PhanCong.MsDA, TenDA, DiaDiem,
NhanVien.MSNV , SoGio, HoTenNV
From DonVi
Inner Join DuAn
On DonVi.MsDV = DuAn.MsDV
Inner Join PhanCong
On DuAn.MSDA = PhanCong.MsDA
Inner Join NhanVien
On PhanCong.MSNV = NhanVien.MSNV
Where DiaDiem = 'Ha Noi'
Select clause - Outer join

 SELECT column_name(s)
From table1
Left Join table2
On table1.column_name = table2.column_name;

 SELECT column_name(s)
From table1
Right Join table2
On table1.column_name = table2.column_name;
Select clause - Outer join

 Cho biết thông tin của những đơn vị không quản lý dự án nào cả
SELECT *
FROM DONVI
ORDER BY MSDV

SELECT *
FROM DUAN
ORDER BY MSDV

SELECT *
FROM DONVI
INNER JOIN DUAN
ON DONVI.MSDV = DUAN.MSDV
ORDER BY DONVI.MSDV

SELECT *
FROM DONVI
LEFT JOIN DUAN
ON DONVI.MSDV = DUAN.MSDV
WHERE DUAN.MSDV IS NULL
ORDER BY DONVI.MSDV
Select clause - Outer join

 Cho biết thông tin của những đơn vị hiện tại chưa có nhân viên nào?
SELECT *
FROM DONVI
LEFT JOIN NhanVien
ON DONVI.MSDV = NhanVien.MSDV
WHERE NhanVien.MSDV IS NULL

INSERT INTO DonVi(MsDV, TenDV, DiaDiemDV)


Values (9, 'PMU M', 'Dong Thap')

SELECT *
FROM DONVI
LEFT JOIN NhanVien
ON DONVI.MSDV = NhanVien.MSDV
WHERE NhanVien.MSDV IS NULL
Select clause - Outer join

 Cho biết thông tin của những dự án chưa được phân công nhân viên thực hiện?
SELECT *
FROM DuAn
LEFT JOIN PhanCong
ON DuAn.MsDA = PhanCong.MsDA
WHERE PhanCong.MsDA Is Null
 Cho biết thông tin của những nhân viên chưa được phân công thực hiện dự án?
SELECT *
FROM NhanVien
LEFT JOIN PhanCong
ON NhanVien.MsNV = PhanCong.MsNV
WHERE PhanCong.MsNV Is Null
Select clause – Group by

 SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Select clause – Group by

 Thống kê tổng số nhân viên theo từng đơn vị. Thông tin hiển thị bao gồm:
MsDV, TenDV, Tổng số NV
SELECT DONVI.MSDV, TENDV, COUNT(*) AS Tong_So_NV
FROM DONVI
INNER JOIN NHANVIEN
ON DONVI.MSDV = NHANVIEN.MSDV
GROUP BY DONVI.MSDV, TENDV
Select clause – Group by

 Cho biết thông tin của những đơn vị có nhiều hơn 2 nhân viên. Thông tin hiển
thị bao gồm: MsDV, TenDV, Tổng số NV
SELECT DONVI.MSDV, TENDV, COUNT(*) AS Tong_So_NV
FROM DONVI
INNER JOIN NHANVIEN
ON DONVI.MSDV = NHANVIEN.MSDV
GROUP BY DONVI.MSDV, TENDV
HAVING COUNT(*) >2
Select clause – Group by

 Thống kê tổng lương nhân viên theo từng đơn vị. Thông tin hiển thị bao gồm:
MsDV, TenDV, Tổng lương
SELECT DONVI.MSDV, TENDV, SUM(LUONG) AS Tong_Luong
FROM DONVI
INNER JOIN NHANVIEN
ON DONVI.MSDV = NHANVIEN.MSDV
GROUP BY DONVI.MSDV, TENDV
Select clause – Group by

 Cho biết thông tin của những đơn vị có tổng lương nhân viên trên 10000. Thông
tin hiển thị bao gồm: MsDV, TenDV, Tổng lương
SELECT DONVI.MSDV, TENDV, SUM(LUONG)AS Tong_Luong
FROM DONVI
INNER JOIN NHANVIEN
ON DONVI.MSDV = NHANVIEN.MSDV
GROUP BY DONVI.MSDV, TENDV
HAVING SUM(LUONG)>10000

You might also like