SQLB2
--Tao CSDL
CREATE DATABASE QLBanHang
ON
PRIMARY (
NAME = QLBanHang_data,
FILENAME = 'E:\Quan Ly Sinh Vien\QLBanHang.mdf',
SIZE = 3,
MAXSIZE = 100,
FILEGROWTH = 10 )
LOG ON (
NAME = QLBanHang_log,
FILENAME = 'E:\Quan Ly Sinh Vien\QLBanHang.ldf',
SIZE = 3,
MAXSIZE = 100,
FILEGROWTH = 10 )
-- Su dung CSDL
use QLBanHang
--- a. Tao cac bang va cac rang buoc giua cac bang-------
--Tao bang KHACH HANG--
CREATE TABLE KhachHang (
MaKhachHang nchar(6) primary key not null,
TenCongTy nvarchar(30) not null,
TenGiaoDich nvarchar(30) not null,
DiaChi nvarchar(30) not null,
Email varchar (30),
DienThoai nvarchar(20),
Fax int )
-- Tao bang LOAIHANG--
CREATE TABLE LoaiHang (
MaLoaiHang nchar(6) primary key not null,
TenLoaiHang nvarchar(30) not null,
MoTa nvarchar (30) )
-- Tao bang HANG HOA--
CREATE TABLE HangHoa(
MaHang nchar(10) primary key not null,
TenHang nvarchar(30) not null,
MaCongTy nvarchar(15) not null,
MaLoaiHang nchar(6),
SoLuong int,
DVT nvarchar (15),
GiaHang nvarchar (15) )
-- Tao bang PHIEU XUAT--
CREATE TABLE PhieuXuat (
SoPhieuXuat nchar(6) primary key not null,
MaKhachHang nchar(6),
MaNhanVien nchar(6),
NgayDatHang smalldatetime,
NgayGiaoHang smalldatetime,
NgayChuyenHang smalldatetime,
NoiGiaoHang nvarchar (30) )
-- Tao PHIEU NHAP--
CREATE TABLE PhieuNhap(
SoPhieuNhap nchar(6) primary key not null,
MaNCC nchar(10) not null,
MaNhanVien nchar(6),
NgayNhap smalldatetime )
-- Tao bang NHAN VIEN--
CREATE TABLE NhanVIen (
MaNhanVien nchar(6) primary key not null,
HoDem nvarchar(30) not null,
Ten nvarchar(15) not null,
NgaySinh smalldatetime,
NgayLamViec smalldatetime,
DiaChi nvarchar(30) ,
DienThoai nvarchar(20) ,
HeSoLuong decimal(4,2),
PhuCap decimal(4,2) )
-- tao bang NHA CUNG CAP--
CREATE TABLE NhaCungCap(
MaNCC nchar(10) primary key not null,
TenNCC nvarchar(30) not null,
TenGiaoDich nvarchar(30) not null,
DiaChi nvarchar(30) not null,
Email varchar (30),
DienThoai nvarchar(20),
Fax int )
-- Tao bang NCC_PN --
CREATE TABLE NCC_PN(
SoPhieuNhap nchar(6),
MaHang nchar(10),
SoLuong int,
GiaNhap nvarchar (15),
Constraint pk_NCC_PN primary key(SoPhieuNhap,MaHang))
-- Tao bang KH_PX--
CREATE TABLE KH_PX(
SoPhieuXuat nchar(6),
MaHang nchar(10),
GiaBan nvarchar (15),
SoLuong int,
MucGiamGia nvarchar (15),
Constraint pk_KH_PX primary key(SoPhieuXuat,MaHang))
-- Tao cac rang buoc---
ALTER TABLE PhieuXuat
ADD Constraint pk_PhieuXuat
Foreign key (MaKhachHang)
References KhachHang(MaKhachHang)
On delete cascade
On update cascade
-----------------------------
ALTER TABLE KH_PX
ADD Constraint pk_KHPX_PhieuXuat
Foreign key ( SoPhieuXuat)
References PhieuXuat (SoPhieuXuat)
On delete cascade
On update cascade
-------------------------------
ALTER TABLE PhieuXuat
ADD Constraint Pk_PhieuXuat_NhanVien
Foreign key ( MaNhanVien )
References NhanVien ( MaNhanVien )
On delete cascade
On update Cascade
------------------------------------
ALTER TABLE KH_PX
ADD Constraint pk_KH_PX_HangHoa
Foreign key ( MaHang)
References HangHoa ( MaHang )
On delete cascade
On update Cascade
--------------------------------
ALTER TABLE HangHoa
ADD Constraint pk_HangHoa
Foreign key ( MaLoaiHang)
References LoaiHang ( MaLoaiHang )
On delete cascade
On update Cascade
--------------------------
ALTER TABLE PhieuNhap
ADD Constraint pk_PN_NhanVien
Foreign key ( MaNhanVien)
References NhanVien( MaNhanVien)
On delete cascade
On update Cascade
------------------------------
ALTER TABLE PhieuNhap
ADD Constraint pk_PN_NhaCungCap
Foreign key ( MaNCC)
References NhaCungCap ( MaNCC )
On delete cascade
On update Cascade
----------------------------------
ALTER TABLE NCC_PN
ADD Constraint pk_PhieuNhap_NCC_PN
Foreign key ( SoPhieuNhap)
References PhieuNhap ( SoPhieuNhap)
On delete cascade
On update Cascade
--------------------------------
ALTER TABLE NCC_PN
ADD Constraint pk_NCC_PN_HangHoa
Foreign key ( MaHang)
References HangHoa ( MaHang )
On delete cascade
On update Cascade
Bạn đang đọc truyện trên: AzTruyen.Top