Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Tài liệu 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CODE

TẠO DATABASE:
CREATE DATABASE WEBPHIM

TẠO BẢNG:
USE WEBPHIM

CREATE TABLE NHACUNGCAP

MACONGTY Nvarchar(10) NOT NULL,

CONSTRAINT pk_NHACUNGCAP PRIMARY KEY(MACONGTY),

TENCONGTY Nvarchar(40) NOT NULL,

TENGIAODICH Nvarchar(30),

DIACHI Nvarchar(50),

DIENTHOAI Nvarchar(15),

FAX Nvarchar(15),

EMAIL Nvarchar(30)

);

CREATE TABLE LOAIHANG

MALOAIHANG Nvarchar(10) NOT NULL,

CONSTRAINT pk_LOAIHANG PRIMARY KEY(MALOAIHANG),

TENLOAIHANG Nvarchar(40) NOT NULL

);

CREATE TABLE MATHANG

MAHANG Nvarchar(10) NOT NULL,

TENHANG Nvarchar(50) NOT NULL,

MACONGTY Nvarchar(10),

MALOAIHANG Nvarchar(10),
SOLUONG INT,

DONVITINH Nvarchar(10),

GIAHANG INT,

CONSTRAINT pk_MATHANG PRIMARY KEY(MAHANG),

CONSTRAINT fk_MATHANG_MACONGTY

FOREIGN KEY(MACONGTY)

REFERENCES NHACUNGCAP(MACONGTY),

CONSTRAINT fk_MATHANG_MALOAIHANG

FOREIGN KEY(MALOAIHANG)

REFERENCES LOAIHANG(MALOAIHANG)

);

CREATE TABLE NHANVIEN

MANHANVIEN Nvarchar(10) NOT NULL,

HO Nvarchar(20) NOT NULL,

TEN Nvarchar(10) NOT NULL,

NGAYSINH DateTime,

NGAYLAMVIEC DateTime,

DIACHI Nvarchar(50),

DIENTHOAI Nvarchar(15),

LUONGCOBAN INT,

PHUCAP INT,

CONSTRAINT pk_NHANVIEN PRIMARY KEY(MANHANVIEN)

);

CREATE TABLE KHACHHANG

MAKHACHHANG Nvarchar(10) NOT NULL,

TENCONGTY Nvarchar(40) NOT NULL,

TENGIAODICH Nvarchar(30),
DIACHI Nvarchar(50),

DIENTHOAI Nvarchar(15),

FAX Nvarchar(15),

EMAIL Nvarchar(30),

CONSTRAINT pk_KHACHHANG PRIMARY KEY(MAKHACHHANG)

);

CREATE TABLE DONDATHANG

SOHOADON INT NOT NULL,

MAKHACHHANG Nvarchar(10),

MANHANVIEN Nvarchar(10),

NGAYDATHANG DateTime,

NGAYGIAOHANG DateTime,

NGAYCHUYENHANG DateTime,

NOIGIAOHANG Nvarchar(50),

CONSTRAINT pk_DONDATHANG PRIMARY KEY(SOHOADON),

CONSTRAINT fk_DONDATHANG_MAKHACHHANG

FOREIGN KEY(MAKHACHHANG)

REFERENCES KHACHHANG(MAKHACHHANG),

CONSTRAINT fk_DONDATHANG_MANHANVIEN

FOREIGN KEY(MANHANVIEN)

REFERENCES NHANVIEN(MANHANVIEN)

);

CREATE TABLE CHITIETDATHANG

SOHOADON INT NOT NULL,

MAHANG Nvarchar(10) NOT NULL,

GIABAN INT,

SOLUONG SmallInt,
MUCGIAMGIA NumeriC(2,1),

CONSTRAINT pk_CHITIETDATHANG PRIMARY


KEY(SOHOADON,MAHANG),

CONSTRAINT fk_CHITIETDATHANG_SOHOADON

FOREIGN KEY(SOHOADON)

REFERENCES DONDATHANG(SOHOADON),

CONSTRAINT fk_CHITIETDATHANG_MAHANG

FOREIGN KEY(MAHANG)

REFERENCES MATHANG(MAHANG)

);

ALTER TABLE CHITIETDATHANG

ADD

CONSTRAINT df_CHITIETDATHANG_SOLUONG

DEFAULT(1) FOR SOLUONG;

ALTER TABLE CHITIETDATHANG

ADD

CONSTRAINT df_CHITIETDATHANG_MUCGIAMGIA

DEFAULT(0) FOR MUCGIAMGIA;

ALTER TABLE NHANVIEN

ADD CONSTRAINT Check_Tuoi

CHECK (DATEDIFF(YEAR, NGAYSINH, GETDATE()) BETWEEN 18 AND 61);

ALTER TABLE DONDATHANG

ADD CONSTRAINT Check_ngay

CHECK (NGAYGIAOHANG >= NGAYDATHANG AND NGAYCHUYENHANG >=


NGAYDATHANG);

You might also like