create database QUAN_LY_PHIM
go
use QUAN_LY_PHIM
go
-- Tạo các bảng dữ liệu
-- Tại bảng theloai
create table theloai(
IDtheloai int NOT NULL,
MoTa nvarchar (100) NULL,
primary key (IDtheloai)
)
-- Tạo bảng tacgia
create table tacgia(
IDtacgia int NOT NULL,
IDphimtieubieu int NOT NULL,
Hoten nvarchar (50) NULL,
Ngaysinh datetime NULL,
primary key(IDtacgia)
)
-- Tạo bảng phim
create table phim(
IDphim int NOT NULL,
IDtheloai int NOT NULL,
NoiDung nvarchar (100) NULL,
TacGia int NOT NULL,
primary key (IDphim)
)
-- Tạo các ràng buộc khóa ngoại
ALTER TABLE phim ADD CONSTRAINT FK_phim_theloai FOREIGN KEY(IDtheloai)
REFERENCES theloai (IDtheloai)
ALTER TABLE tacgia ADD CONSTRAINT FK_tacgia_phim FOREIGN KEY(IDphimtieubieu)
REFERENCES phim (IDphim)
ALTER TABLE phim ADD CONSTRAINT FK_phim_tacgia FOREIGN KEY(TacGia)
REFERENCES tacgia (IDtacgia)
-- Nhập liệu cho bảng theloai
insert into theloai (IDtheloai,MoTa)
values(1, N'Phim tài liệu')
insert into theloai (IDtheloai,MoTa)
values(2, N'Phim tình cảm')
insert into theloai (IDtheloai,MoTa)
values(3, N'Phim chiến tranh')
-- Nhập liệu cho bảng phim
insert into phim (IDphim,IDtheloai, NoiDung, TacGia)
values(1,1, N'Chất độc da cam',1)
insert into phim (IDphim,IDtheloai, NoiDung, TacGia)
values(2,2, N'Gió và hoa',2)
insert into phim (IDphim,IDtheloai, NoiDung, TacGia)
values(3,3, N'Cuộc chiến 10000 ngày',3)
-- Nhập liệu cho bảng tacgia
insert into tacgia (IDtacgia,IDphimtieubieu, Hoten, Ngaysinh)
values(1,1, N'Phạm Trưởng','01/01/1967')
insert into tacgia (IDtacgia,IDphimtieubieu, Hoten, Ngaysinh)
values(2,2, N'Hoàng Phương','02/02/1950')
insert into tacgia (IDtacgia,IDphimtieubieu, Hoten, Ngaysinh)
values(3,3, N'Văn Cao','05/05/1960')
Không hiểu sao chỉ nhập liệu được cho bảng thể loại, còn 2 bảng còn lại toàn NULL và báo lỗi xung đột ràng buộc khóa ngoại tại của IDtacgia. Mong các bạn tìm giúp chỗ sai?
Làm sao để truy vấn xuất thông tin tác giả có nhiều phim nhất?
Bookmarks