Tạo rằng buộc khóa cho các bảng trong SQL Server
Tạo rằng buộc khóa cho các bảng trong SQL Server
Rằng buộc khóa chính và khóa phụ giữa các bảng trong Sql Server
CREATE TABLE tblSach
(
MaSach NVARCHAR(10) NOT NULL,
TenSach NVARCHAR(50)NOT NULL,
MaLV NVARCHAR(10)NOT NULL,
MaNXB NVARCHAR(10)NOT NULL,
SoLuong INT NOT NULL,
GiaNhap FLOAT NOT NULL,
GiaBan FLOAT NOT NULL
Primary key(MaSach)
)
CREATE TABLE tblTacGia
(
MaTG NVARCHAR(10),
TenTG NVARCHAR(50)NOT NULL
Primary key(MaTG)
)
CREATE TABLE tblTacGia_Sach
(
MaSach NVARCHAR(10)NOT NULL,
MaTG NVARCHAR(10)NOT NULL
Primary key(MaSach,MaTG)
)
Alter Table tblTacGia_Sach
add constraint FK_TacGiaSach_Sach foreign key(MaSach) references tblSach(MaSach)
alter table tblTacGia_Sach
Add constraint FK_TacGiaSach_TacGia foreign Key(MaTG) references tblTacGia(MaTG)
Code cho bài tập quản lý bán hàng, Anh em nhớ chia ra các phần nhe:
use master
go
create database QLBH on (
name=N'QLBH_Dat',
filename=N'c:\sql\QLBH_Dat.mdf',
size=5MB,
maxsize=100MB,
filegrowth=5MB
)
log on(
name=N'QLBH_Log',
filename=N'c:\sql\QLBH_Log.ldf',
size=5MB,
maxsize=100MB,
filegrowth=5MB
)
go
use QLBH
create table DMKhach(
MaKhach nvarchar(50) not null primary key,
TenKhach nvarchar(50) not null ,
DiaChi nvarchar(50) not null,
DienThoai nvarchar(50)
)
go
create table DMHang(
MaHang nvarchar(50) not null primary key,
TenHang nvarchar(50) not null ,
DVT nvarchar(50) not null
)
go
use QLBH
create table HoaDonBan(
SoHD nvarchar(50) not null primary key,
MaKhach nvarchar(50) not null,
NgayHD datetime,
DienGiai nvarchar(100),
)
use QLBH
create table ChiTietHoaDon(
SoHD nvarchar(50) not null primary key,
MaHang nvarchar(50) not null,
SoLuong int not null,
DonGia int not null ,
Constraint DB_MaHang foreign key (MaHang) References DMHang(MaHang) ,
Alter table HoaDonBan Add Constraint DB_MaKhach foreign key (MaKhach) References DMKhach(MaKhach)
use QLBH
Insert into DMHang(MaHang,TenHang,DVT) Values('MH01','TH01','VND')
Insert into DMHang(MaHang,TenHang,DVT) Values('MH02','TH02','USD')
(
MaSach NVARCHAR(10) NOT NULL,
TenSach NVARCHAR(50)NOT NULL,
MaLV NVARCHAR(10)NOT NULL,
MaNXB NVARCHAR(10)NOT NULL,
SoLuong INT NOT NULL,
GiaNhap FLOAT NOT NULL,
GiaBan FLOAT NOT NULL
Primary key(MaSach)
)
CREATE TABLE tblTacGia
(
MaTG NVARCHAR(10),
TenTG NVARCHAR(50)NOT NULL
Primary key(MaTG)
)
CREATE TABLE tblTacGia_Sach
(
MaSach NVARCHAR(10)NOT NULL,
MaTG NVARCHAR(10)NOT NULL
Primary key(MaSach,MaTG)
)
Alter Table tblTacGia_Sach
add constraint FK_TacGiaSach_Sach foreign key(MaSach) references tblSach(MaSach)
alter table tblTacGia_Sach
Add constraint FK_TacGiaSach_TacGia foreign Key(MaTG) references tblTacGia(MaTG)
Code cho bài tập quản lý bán hàng, Anh em nhớ chia ra các phần nhe:
use master
go
create database QLBH on (
name=N'QLBH_Dat',
filename=N'c:\sql\QLBH_Dat.mdf',
size=5MB,
maxsize=100MB,
filegrowth=5MB
)
log on(
name=N'QLBH_Log',
filename=N'c:\sql\QLBH_Log.ldf',
size=5MB,
maxsize=100MB,
filegrowth=5MB
)
go
use QLBH
create table DMKhach(
MaKhach nvarchar(50) not null primary key,
TenKhach nvarchar(50) not null ,
DiaChi nvarchar(50) not null,
DienThoai nvarchar(50)
)
go
create table DMHang(
MaHang nvarchar(50) not null primary key,
TenHang nvarchar(50) not null ,
DVT nvarchar(50) not null
)
go
use QLBH
create table HoaDonBan(
SoHD nvarchar(50) not null primary key,
MaKhach nvarchar(50) not null,
NgayHD datetime,
DienGiai nvarchar(100),
)
use QLBH
create table ChiTietHoaDon(
SoHD nvarchar(50) not null primary key,
MaHang nvarchar(50) not null,
SoLuong int not null,
DonGia int not null ,
Constraint DB_MaHang foreign key (MaHang) References DMHang(MaHang) ,
Alter table HoaDonBan Add Constraint DB_MaKhach foreign key (MaKhach) References DMKhach(MaKhach)
use QLBH
Insert into DMHang(MaHang,TenHang,DVT) Values('MH01','TH01','VND')
Insert into DMHang(MaHang,TenHang,DVT) Values('MH02','TH02','USD')
Post a Comment