Lab 01 Company Database - Tài liệu tham khảo | Đại học Hoa Sen

Lab 01 Company Database - Tài liệu tham khảo | Đại học Hoa Sen và thông tin bổ ích giúp sinh viên tham khảo, ôn luyện và phục vụ nhu cầu học tập của mình cụ thể là có định hướng, ôn tập, nắm vững kiến thức môn học và làm bài tốt trong những bài kiểm tra, bài tiểu luận, bài tập kết thúc học phần, từ đó học tập tốt và có kết quả cao cũng như có thể vận dụng tốt những kiến thức mình đã học.

Lab01_Sol.docx 1
MSSV:
Họ tên SV:
Notes: Filename: Lab01_HoTen_MSSV.docx
Lab 01
Company Database
1. Create Database Company
CREATE DATABASE Company ON PRIMARY
( NAME = 'Company',
FILENAME = 'E:\DATA\Company.mdf' ,
SIZE = 3072KB ,
MAXSIZE = UNLIMITED,
FILEGROWTH = 1024KB )
LOG ON
( NAME = 'Company_log',
FILENAME = 'E:\DATA\Company_log.ldf' ,
SIZE = 1024KB ,
MAXSIZE = 2048KB ,
FILEGROWTH = 10%)
Note: Save to file Company_DB.sql
2. Create tables – Primary Key – Foreign Key
Table Structure:
Lab01_Sol.docx 2
Department
Field Name
Data Type
Null
DName varchar(15) NOT NULL
DNumber
numeric(4, 0) NOT NULL
Mgrssn char(9) NULL
MgrStartdate datetime NULL
Employee
Field Name
Data Type
Null
FName varchar(15) NOT NULL
MInit varchar(1) NULL
varchar(15)
NOT NULL
SSN
char(9)
NOT NULL
BDate
datetime
NULL
Address
varchar(30)
NULL
Sex
char(1)
NULL
Salary numeric(10, 2) NULL
SuperSSN char(9) NULL
DNo numeric(4, 0) NULL
Dept_Location
Field Name
Data Type
Null
DNumber
numeric(4, 0) NOT NULL
DLocation
varchar(15) NOT NULL
Project
Field Name
Data Type
Null
PName varchar(15) NOT NULL
PNumber
numeric(4, 0) NOT NULL
PLocation varchar(15) NULL
DNum numeric(4, 0) NOT NULL
Works_On
Field Name
Data Type
Null
ESSN
char(9) NOT NULL
PNo
numeric(4, 0) NOT NULL
Hours numeric(4, 1) NULL
Dependent
Field Name
Data Type
Null
ESSN
char(9) NOT NULL
Dependent_Name
varchar(15) NOT NULL
Sex char(1) NULL
BDate
datetime
NULL
Relationship
varchar(8)
NULL
Note: Save to file Company_DB.sql
--Tables
Lab01_Sol.docx 3
[Paste SQL statements here]
CREATE DATABASE ON CompanyLoc PRIMARY
( NAME = 'CompanyLoc',
FILENAME = 'D:\IT201_CSDL\CompanyLoc.mdf' ,
SIZE = 3072KB ,
MAXSIZE = UNLIMITED,
FILEGROWTH 1024KB = )
LOG ON
( NAME = 'CompanyLoc_log',
FILENAME = 'D:\IT201_CSDL\CompanyLoc_log.ldf' ,
SIZE = 1024KB ,
MAXSIZE = 2048KB ,
FILEGROWTH 10= %);
Go
use CompanyLoc ;
go
-- Tao bang
--Department table
CREATE TABLE Department (
DName varchar(15) NOT NULL,
DNumber numeric(4 , 0) NOT NULL,
MgrSsn char(9) NULL,
MgrStartdate datetime NULL);
GO
-- Employee table
CREATE TABLE Employee (
FName varchar(15) NOT NULL,
MInit varchar(1) NULL,
LName varchar(15) NOT NULL,
SSN char(9 ) NOT NULL,
BDate datetime NULL,
Address varchar(30) NULL,
Sex char(1 ) NULL,
Salary 2 numeric(10, ) NULL,
SuperSSN char(9) NULL,
DNo 0 numeric(4, ) NULL);
GO
-- Dept_Location table
CREATE TABLE Dept_Location (
DNumber numeric(4,0) NOT NULL,
DLocation varchar(15) NOT NULL);
Go
-- Project table
CREATE TABLE Project (
PName varchar(15) NOT NULL,
PNumber 0 numeric(4, ) NOT NULL,
PLocation varchar(15) NULL,
DNum 0 numeric(4, ) NOT NULL);
Go
-- Works_On table
CREATE TABLE Works_On (
ESSN char(9) NOT NULL,
PNo 0 numeric(4, ) NOT NULL,
Hours numeric(4, 1 ) NULL);
Go
-- Dependent table
CREATE TABLE Dependent (
ESSN char(9) NOT NULL,
Lab01_Sol.docx 4
Dependent_Name varchar(15) NOT NULL,
Sex char(1) NULL,
BDate datetime NULL,
Relationship varchar(8) NULL);
Go
--Primary key
[Paste SQL statements here]
--PK
--Department PRIMARY KEY
ALTER TABLE Department
ADD Constraint pk_Dept PRIMARY KEY ( );DNumber
GO
-- Employee PRIMARY KEY
ALTER TABLE Employee
ADD Constraint pk_Emp PRIMARY KEY (SSN);
GO
-- Project Frimary Key
ALTER TABLE Project
ADD Constraint pk_Proj PRIMARY KEY(Pnumber);
Go
-- Dpet_loc
ALTER TABLE Dept_Location
ADD Constraint pk_DeptLoc PRIMARY DLocation KEY(DNumber, );
Go
--works_on
ALTER TABLE Works_On
ADD Constraint pk_WorksOn PRIMARY PNo KEY( ,ESSN );
Go
-- [dbo].[Dependent]
ALTER TABLE Dependent
ADD Constraint pk_Depe PRIMARY KEY(ESSN, Dependent_Name);
Go
--Foreign key
[Paste SQL statements here]
--FK
ALTER TABLE Department
ADD constraint fk_DeptMgrssn FOREIGN KEY(Mgrssn)
REFERENCES Employee(SSN);
GO
-- Employee FOREIGN KEY
ALTER TABLE Employee
ADD Constraint fk_EmpDNo FOREIGN KEY(DNo) REFERENCES
Department(DNumber);
GO
ALTER TABLE Employee
ADD Constraint fk_EmpSuperSSN FOREIGN KEY(SuperSSN)
REFERENCES Employee(SSN);
Go
Lab01_Sol.docx 5
-- Dept_Location Foreign Key
ALTER TABLE Dept_Location
ADD Constraint fk_DepLoDnumber FOREIGN KEY( )Dnumber
REFERENCES Department(Dnumber);
Go
-- Project Foreign Key
ALTER TABLE Project
ADD Constraint fk_ProDnum FOREIGN KEY( )Dnum
REFERENCES Department(Dnumber);
Go
-- Work_On Foreign Key
ALTER TABLE Works_On
ADD Constraint fk_WOnPno FOREIGN KEY(Pno)
REFERENCES Project(Pnumber);
Go
ALTER TABLE Works_On
ADD Constraint fk_WOnEssn FOREIGN KEY( )Essn
REFERENCES Employee(SSN);
Go
-- Dependent Foreign Key
ALTER TABLE Dependent
ADD Constraint fk_DeEssn FOREIGN KEY(Essn)
REFERENCES Employee(SSN);
Go
END
| 1/5

Preview text:

Lab01_Sol.docx 1 MSSV: Họ tên SV:
Notes: Filename: Lab01_HoTen_MSSV.docx Lab 01 Company Database
1. Create Database Company
CREATE DATABASE Company ON PRIMARY ( NAME = 'Company',
FILENAME = 'E:\DATA\Company.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) LOG ON ( NAME = 'Company_log',
FILENAME = 'E:\DATA\Company_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048KB , FILEGROWTH = 10%)
Note: Save to file Company_DB.sql
2. Create tables – Primary Key – Foreign Key Table Structure: Lab01_Sol.docx 2  Department Field Name Data Type Null DName varchar(15) NOT NULL DNumber numeric(4, 0) NOT NULL Mgrssn char(9) NULL MgrStartdate datetime NULL  Employee Field Name Data Type Null FName varchar(15) NOT NULL MInit varchar(1) NULL LName varchar(15) NOT NULL SSN char(9) NOT NULL BDate datetime NULL Address varchar(30) NULL Sex char(1) NULL Salary numeric(10, 2) NULL SuperSSN char(9) NULL DNo numeric(4, 0) NULL  Dept_Location Field Name Data Type Null DNumber numeric(4, 0) NOT NULL DLocation varchar(15) NOT NULL  Project Field Name Data Type Null PName varchar(15) NOT NULL PNumber numeric(4, 0) NOT NULL PLocation varchar(15) NULL DNum numeric(4, 0) NOT NULL  Works_On Field Name Data Type Null ESSN char(9) NOT NULL PNo numeric(4, 0) NOT NULL Hours numeric(4, 1) NULL  Dependent Field Name Data Type Null ESSN char(9) NOT NULL
Dependent_Name varchar(15) NOT NULL Sex char(1) NULL BDate datetime NULL Relationship varchar(8) NULL
Note: Save to file Company_DB.sql --Tables Lab01_Sol.docx 3 [Paste SQL statements here]
CREATE DATABASE CompanyLoc ON PRIMARY ( NAME = 'CompanyLoc',
FILENAME = 'D:\IT201_CSDL\CompanyLoc.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) LOG ON ( NAME = 'CompanyLoc_log',
FILENAME = 'D:\IT201_CSDL\CompanyLoc_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048KB , FILEGROWTH = 10%); Go use CompanyLoc; go -- Tao bang --Department table CREATE TABLE Department( DName varchar(15) NOT NULL,
DNumber numeric(4, 0) NOT NULL, MgrSsn char(9) NULL, MgrStartdate datetime NULL); GO -- Employee table CREATE TABLE Employee( FName varchar(15) NOT NULL, MInit varchar(1) NULL, LName varchar(15) NOT NULL, SSN char(9) NOT NULL, BDate datetime NULL, Address varchar(30) NULL, Sex char(1) NULL, Salary numeric(10, 2) NULL, SuperSSN char(9) NULL, DNo numeric(4, 0) NULL); GO -- Dept_Location table CREATE TABLE Dept_Location(
DNumber numeric(4,0) NOT NULL,
DLocation varchar(15) NOT NULL); Go -- Project table CREATE TABLE Project( PName varchar(15) NOT NULL, PNumber numeric(4, 0) NOT NULL, PLocation varchar(15) NULL, DNum numeric(4, 0) NOT NULL); Go -- Works_On table CREATE TABLE Works_On( ESSN char(9) NOT NULL, PNo numeric(4, 0) NOT NULL, Hours numeric(4, 1) NULL); Go -- Dependent table CREATE TABLE Dependent( ESSN char(9) NOT NULL, Lab01_Sol.docx 4 Dependent_Name varchar(15) NOT NULL, Sex char(1) NULL, BDate datetime NULL, Relationship varchar(8) NULL); Go --Primary key [Paste SQL statements here] --PK --Department PRIMARY KEY ALTER TABLE Department
ADD Constraint pk_Dept PRIMARY KEY (DNumber); GO -- Employee PRIMARY KEY ALTER TABLE Employee
ADD Constraint pk_Emp PRIMARY KEY (SSN); GO -- Project Frimary Key ALTER TABLE Project
ADD Constraint pk_Proj PRIMARY KEY(Pnumber); Go -- Dpet_loc ALTER TABLE Dept_Location
ADD Constraint pk_DeptLoc PRIMARY KEY(DNumber, DLocation); Go --works_on ALTER TABLE Works_On
ADD Constraint pk_WorksOn PRIMARY KEY(ESSN, PNo); Go -- [dbo].[Dependent] ALTER TABLE Dependent
ADD Constraint pk_Depe PRIMARY KEY(ESSN, Dependent_Name); Go --Foreign key [Paste SQL statements here] --FK ALTER TABLE Department
ADD constraint fk_DeptMgrssn FOREIGN KEY(Mgrssn) REFERENCES Employee(SSN); GO -- Employee FOREIGN KEY ALTER TABLE Employee
ADD Constraint fk_EmpDNo FOREIGN KEY(DNo) REFERENCES Department(DNumber); GO ALTER TABLE Employee
ADD Constraint fk_EmpSuperSSN FOREIGN KEY(SuperSSN) REFERENCES Employee(SSN); Go Lab01_Sol.docx 5 -- Dept_Location Foreign Key ALTER TABLE Dept_Location
ADD Constraint fk_DepLoDnumber FOREIGN KEY(Dnumber)
REFERENCES Department(Dnumber); Go -- Project Foreign Key ALTER TABLE Project
ADD Constraint fk_ProDnum FOREIGN KEY(Dnum)
REFERENCES Department(Dnumber); Go -- Work_On Foreign Key ALTER TABLE Works_On
ADD Constraint fk_WOnPno FOREIGN KEY(Pno) REFERENCES Project(Pnumber); Go ALTER TABLE Works_On
ADD Constraint fk_WOnEssn FOREIGN KEY(Essn) REFERENCES Employee(SSN); Go -- Dependent Foreign Key ALTER TABLE Dependent
ADD Constraint fk_DeEssn FOREIGN KEY(Essn) REFERENCES Employee(SSN); Go END