0% found this document useful (0 votes)
49 views5 pages

SQL Database Table Creation Guide

The document contains SQL commands for managing a database named 'Store'. It includes the creation of six tables: Supplier, Product, Staff, Customer, Import_Invoice, and Sales_Invoice, along with their respective fields and foreign key relationships. Additionally, it includes commands to drop existing tables and insert sample data into each of the newly created tables.

Uploaded by

Bình Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views5 pages

SQL Database Table Creation Guide

The document contains SQL commands for managing a database named 'Store'. It includes the creation of six tables: Supplier, Product, Staff, Customer, Import_Invoice, and Sales_Invoice, along with their respective fields and foreign key relationships. Additionally, it includes commands to drop existing tables and insert sample data into each of the newly created tables.

Uploaded by

Bình Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

-- Use the database

USE Store;

-- Drop Tables if they already exist

IF OBJECT_ID(' Sales_Invoice ', 'U') IS NOT NULL DROP TABLE Hoadonban;

IF OBJECT_ID('Import_Invoice', 'U') IS NOT NULL DROP TABLE Import_Invoice;

IF OBJECT_ID('Customer', 'U') IS NOT NULL DROP TABLE Customer;

IF OBJECT_ID('Staff', 'U') IS NOT NULL DROP TABLE Staff;

IF OBJECT_ID('Product', 'U') IS NOT NULL DROP TABLE Product;

IF OBJECT_ID('Supplier', 'U') IS NOT NULL DROP TABLE Supplier;

-- Table 1: Supplier

CREATE TABLE Supplier (

ID_Supplier CHAR(10) PRIMARY KEY,

Name_Supplier VARCHAR(50),

Address NVARCHAR(50),

PHONE VARCHAR(15)

);

-- Table 2: Product

CREATE TABLE Product (

ID_Product CHAR(10) PRIMARY KEY,

Name_Product VARCHAR(50),

ID_Supplier CHAR(10),

Quantity INT,

Price INT,
FOREIGN KEY (ID_Supplier ) REFERENCES Supplier(ID_Supplier ) ON
DELETE CASCADE ON UPDATE CASCADE

);

-- Table 3: Staff

CREATE TABLE Staff (

ID_Staff CHAR(10) PRIMARY KEY,

Name_Staff VARCHAR(50),

Sex NVARCHAR(50),

Address NVARCHAR(50),

PHONE VARCHAR(15)

);

-- Table 4: Customer

CREATE TABLE Customer (

ID_Customer CHAR(10) PRIMARY KEY,

Name_Customer VARCHAR(50),

Sex NVARCHAR(50),

Address NVARCHAR(50),

PHONE VARCHAR(15)

);

-- Table 5: Import_Invoice

CREATE TABLE Import_Invoice (

ID_Invoice CHAR(10) PRIMARY KEY,

ID_Staff CHAR(10),

ID_Product CHAR(10),
ID_Supplier CHAR(10),

Quantity INT,

Date_of_Sale DATE,

Address VARCHAR(50),

PHONE VARCHAR(15),

Unit_Price FLOAT,

Total_Money FLOAT,

FOREIGN KEY (ID_Staff) REFERENCES Staff(ID_Staff) ON DELETE CASCADE


ON UPDATE CASCADE,

FOREIGN KEY (ID_Product) REFERENCES Product(ID_Product) ON DELETE


CASCADE ON UPDATE CASCADE,

FOREIGN KEY (ID_Supplier ) REFERENCES Supplier(ID_Supplier ) ON


DELETE NO ACTION ON UPDATE NO ACTION

);

-- Table 6: Hoadonban

CREATE TABLE Sales_Invoice(

ID_Sales_Invoice CHAR(10) PRIMARY KEY,

ID_Staff CHAR(10),

ID_Customer CHAR(10),

ID_Product CHAR(10),

Quantity INT,

Date_of_Sale DATE,

Address VARCHAR(50),

PHONE VARCHAR(15),

Unit_Price FLOAT,

Total_Money FLOAT,
FOREIGN KEY (ID_Staff) REFERENCES Staff(ID_Staff) ON DELETE CASCADE
ON UPDATE CASCADE,

FOREIGN KEY (ID_Customer) REFERENCES Customer(ID_Customer) ON


DELETE CASCADE ON UPDATE CASCADE,

FOREIGN KEY (ID_Product) REFERENCES Product(ID_Product) ON DELETE


CASCADE ON UPDATE CASCADE

);

-- Insert Data into Supplier

INSERT INTO Supplier VALUES ('NCC01', 'TRANTUYEN', 'HY', '1659939186');

INSERT INTO Supplier VALUES ('NCC02', 'TRINHTHUY', 'HY', '122345');

-- Insert Data into Product

INSERT INTO Product VALUES ('MT01', 'DELL', 'NCC01', 2, 1200);

INSERT INTO Product VALUES ('MT02', 'ASUS', 'NCC01', 2, 1200);

-- Insert Data into Staff

INSERT INTO Staff VALUES ('NV01', 'Nguyen Van A', 'Nam', 'HN',
'0987654321');

-- Insert Data into Customer

INSERT INTO Customer VALUES ('KH01', 'Le Thi B', 'Nu', 'HY', '0912345678');

-- Insert Data into Sales_Invoice

INSERT INTO Sales_Invoice VALUES ('HDB01', 'NV01', 'KH01', 'MT01', 2,


'2014-01-01', 'HY', '1659939285', 1200, 2400);

INSERT INTO Sales_Invoice VALUES ('HDB02', 'NV01', 'KH01', 'MT01', 1,


'2014-01-03', 'HY', '1356789', 1000, 1000);
-- Insert Data into Import_Invoice

INSERT INTO Import_Invoice VALUES ('HDN01', 'NV01', 'MT01', 'NCC01', 125,


'2014-01-10', 'HN', '1465676778', 1005, 125625);

INSERT INTO Import_Invoice VALUES ('HDN02', 'NV01', 'MT01', 'NCC01', 23,


'2014-01-01', 'HY', '2132456', 12000, 276000);

You might also like