ngày 09-08-2023
-- Tạo bảng KhoaCREATE TABLE Khoa (MaKhoa VARCHAR(10) PRIMARY KEY,TenKhoa VARCHAR(255) NOT NULL);-- Tạo bảng SinhVienCREATE TABLE SinhVien (MaSinhVien INT PRIMARY KEY,Ten VARCHAR(255) NOT NULL,NgaySinh DATE,DiaChi VARCHAR(255),MaKhoa VARCHAR(10),FOREIGN KEY (MaKhoa) REFERENCES Khoa(MaKhoa));
-- Thêm dữ liệu cho bảng KhoaINSERT INTO Khoa (MaKhoa, TenKhoa) VALUES('K1', 'Khoa Toan'),('K2', 'Khoa Ly'),('K3', 'Khoa Hoa');
INSERT INTO SinhVien (MaSinhVien, Ten, NgaySinh, DiaChi, MaKhoa) VALUES(1, 'Nguyen Van A', '2000-11-15', '123 Hai Ba Trung, TP.HCM', 'K1'),(2, 'Le Thi B', '2001-05-20', '456 Le Duan, TP.HCM', 'K1'),(3, 'Tran Van C', '2002-01-10', '789 Tran Hung Dao, Ha Noi', 'K1'),(4, 'Pham Thi D', '2001-09-05', '101 Ba Trieu, Da Nang', 'K1'),(5, 'Vo Van E', '2002-04-17', '202 Nguyen Du, TP.HCM', 'K1'),(6, 'Bui Thi F', '2000-12-25', '303 Ly Thuong Kiet, TP.HCM', 'K1'),(7, 'Hoang Van G', '2001-07-13', '404 Tran Phu, Ha Noi', 'K2'),(8, 'Nguyen Thi H', '2002-02-19', '505 Tran Binh Trong, TP.HCM', 'K2'),(9, 'Le Van I', '2001-06-30', '606 Hoang Van Thu, TP.HCM', 'K2'),(10, 'Tran Thi J', '2000-03-08', '707 Nguyen Trai, Da Nang', 'K2'),(11, 'Pham Van K', '2001-10-24', '808 Tran Cao Van, TP.HCM', 'K2'),(12, 'Vo Thi L', '2002-08-12', '909 Le Lai, TP.HCM', 'K2'),(13, 'Bui Van M', '2001-11-11', '110 Nguyen Chi Thanh, Ha Noi', 'K3'),(14, 'Hoang Thi N', '2000-05-21', '111 Hang Bai, Ha Noi', 'K3'),(15, 'Nguyen Van O', '2001-03-15', '212 Hang Ma, Ha Noi', 'K3'),(16, 'Le Thi P', '2002-07-18', '313 Hang Bong, Ha Noi', 'K3'),(17, 'Tran Van Q', '2000-12-03', '414 Hang Dao, Ha Noi', 'K3'),(18, 'Pham Thi R', '2001-09-09', '515 Pho Hue, Ha Noi', 'K3'),(19, 'Vo Van S', '2002-06-27', '616 Tran Nhan Tong, Ha Noi', 'K3'),(20, 'Bui Van T', '2001-02-02', '717 Le Thanh Tong, Ha Noi', 'K3');
SELECT *FROM SinhVien;
SELECT Ten,NgaySinhFROM SinhVien
SELECT *FROM SinhVienWHERE Ten= "Nguyen Van A"
Trong đó
SELECT ∗FROM SinhVienORDER BY Ten ASC
SELECT ∗FROM SinhVienORDER BY NgaySinh DESC
SELECT ∗FROM SinhVienWHERE NgaySinh>′01−01−2000′ORDER BY Ten ASC
SELECT SinhVien.*, Khoa.TenKhoaFROM SinhVienJOIN Khoa ON SinhVien.MaKhoa = Khoa.MaKhoa;
SELECT Khoa.TenKhoa,COUNT(SinhVien.MaSinhVien) as SoSinhVienFROM SinhVienJOIN Khoa ON SinhVien.MaKhoa = Khoa.MaKhoaGROUP BY Khoa.TenKhoa;
SELECT Khoa.TenKhoa,COUNT(SinhVien.MaSinhVien) as SoSinhVienFROM SinhVienJOIN Khoa ON SinhVien.MaKhoa = Khoa.MaKhoaGROUP BY Khoa.TenKhoaHAVING COUNT(SinhVien.MaSinhVien) < 8;
SELECT * FROM SinhVien;SELECT * FROM Khoa;SELECT Ten, NgaySinh FROM SinhVien;SELECT * FROM SinhVien WHERE Ten = 'Nguyen Van A';SELECT * FROM SinhVien WHERE NgaySinh > '2000-01-01';SELECT SinhVien.Ten, Khoa.TenKhoaFROM SinhVienJOIN Khoa ON SinhVien.MaKhoa = Khoa.MaKhoa;SELECT * FROM SinhVien ORDER BY Ten ASC;SELECT * FROM SinhVien ORDER BY NgaySinh DESC;SELECT Khoa.TenKhoa, COUNT(SinhVien.MaSinhVien) as SoSinhVienFROM SinhVienJOIN Khoa ON SinhVien.MaKhoa = Khoa.MaKhoaGROUP BY Khoa.TenKhoa;SELECT Khoa.TenKhoa, COUNT(SinhVien.MaSinhVien) as SoSinhVienFROM SinhVienJOIN Khoa ON SinhVien.MaKhoa = Khoa.MaKhoaGROUP BY Khoa.TenKhoaHAVING COUNT(SinhVien.MaSinhVien) < 2;SELECT SinhVien.Ten AS HoTen, Khoa.TenKhoa AS KhoaHocFROM SinhVienJOIN Khoa ON SinhVien.MaKhoa = Khoa.MaKhoa;SELECT * FROM SinhVien WHERE Ten = 'Nguyen Van A' OR Ten = 'Le Thi B';SELECT * FROM SinhVien WHERE MaKhoa = 'K1' AND NgaySinh > '2000-01-01';SELECT COUNT(*) AS SoKhoa FROM Khoa;