-- COPYRIGHT @ 2018 . ALL RIGHT RESERVD
-- CODE BY TRANMINHTAM - DTH175190
-- TẠO DATA
CREATE DATABASE QUANLYSV
ON
(
NAME='filedata',
FILENAME='Z:\data.mdf',
SIZE= 4,
MAXSIZE= 100,
FILEGROWTH= 2
)
LOG ON
(
NAME= 'filelog',
FILENAME='Z:\datalog.ldf',
SIZE= 4,
MAXSIZE= 100,
FILEGROWTH= 2
)
USE QUANLYSV
-- Tạo Table
CREATE TABLE SINHVIEN
(
TEN NVARCHAR(40) NOT NULL,
MASV CHAR(15) PRIMARY KEY CHECK(MASV LIKE '[a-z][0-9][0-9][0-9]'),
NAM TINYINT,
KHOA NVARCHAR(40) DEFAULT('CNTT')
)
CREATE TABLE MHOC
(
TEN_MH NVARCHAR(40) NOT NULL ,
MAMH CHAR(15) CHECK(MAMH LIKE '[A-Z][A-Z][A-Z][0-9][0-9][0-9]') PRIMARY KEY,
SOTINCHI TINYINT CHECK(SOTINCHI >=1 AND SOTINCHI <= 10),
KHOA CHAR(10)
)
CREATE TABLE HOCPHAN
(
MAHP CHAR(15) PRIMARY KEY,
MAMH CHAR(15),
HOCKY TINYINT,
NAMHOC CHAR(10),
GV NVARCHAR(20)
)
CREATE TABLE DKIEN
(
MAMH CHAR(15) PRIMARY KEY,
MAMH_TRUOC CHAR(15)
)
CREATE TABLE KETQUA
(
MAHP CHAR(15),
MASV CHAR(15),
DIEM DECIMAL(3,1) CHECK(DIEM >= 0 AND DIEM <= 10)
PRIMARY KEY (MASV , MAHP)
)
ALTER TABLE HOCPHAN ADD CONSTRAINT FK_HOCPHAN_MHOC FOREIGN KEY (MAMH) REFERENCES MHOC(MAMH)
--ALTER TABLE KETQUA ADD CONSTRAINT FK_KETQUA_SINHVIEN FOREIGN KEY (MASV) REFERENCES SINHVIEM(MASV)
ALTER TABLE KETQUA ADD CONSTRAINT FK1_KETQUA_SINHVIEN FOREIGN KEY (MASV) REFERENCES SINHVIEN(MASV)
ALTER TABLE KETQUA ADD CONSTRAINT FK_KETQUA_SINHVIEN FOREIGN KEY (MAHP) REFERENCES HOCPHAN(MAHP)
ALTER TABLE DKIEN ADD CONSTRAINT FK_DKIEN_MHOC FOREIGN KEY (MAMH) REFERENCES MHOC(MAMH)
ALTER TABLE DKIEN ADD CONSTRAINT FK1_DKIEN_MHOC FOREIGN KEY (MAMH_TRUOC) REFERENCES MHOC(MAMH)
SELECT *FROM SINHVIEN
INSERT INTO SINHVIEN VALUES (N'LÝ THANH QUI','A123',1,'CNTT')
INSERT INTO SINHVIEN VALUES (N'LÊ NGỌC THẠO','B123',5,'CNTT')
INSERT INTO SINHVIEN VALUES (N'PHONG THẦN','C123',2,'SP')
INSERT INTO MHOC VALUES (N'LẬP TRÌNH C','ABC123',1,'CNTT')
INSERT INTO MHOC VALUES (N'CHĂN NUÔI','AFF123',1,'NN')
INSERT INTO MHOC VALUES (N'TOÁN','ACC123',1,'SP')
INSERT INTO DKIEN VALUES ('ABC123','ABC123')
INSERT INTO DKIEN VALUES ('AFF123','AFF123')
INSERT INTO DKIEN VALUES ('ACC123','ACC123')
SELECT *FROM HOCPHAN
INSERT INTO HOCPHAN VALUES ('D12','ABC123',1,'2017','lÊ VĂN SUÔL')
INSERT INTO HOCPHAN VALUES ('C11','AFF123',2,'2018','NGUYỄN LÊ BẢO TRÂN')
INSERT INTO HOCPHAN VALUES ('H33','ACC123',3,'2019','TRẦN THÚY VÂN')
INSERT INTO KETQUA VALUES ('D12','A123',6.4)
No Comment to " SQL "