0% found this document useful (0 votes)
14 views2 pages

Đề-5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Đề-5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Create database PE_DBI202

--Use the database


use PE_DBI202
--Create all tables

CREATE TABLE Pet_Owner (


owner_id INT PRIMARY KEY Identity(1,1),
owner_fname NVARCHAR(50) not null,
owner_lname NVARCHAR(50) not null,
owner_address NVARCHAR(100),
owner_phone NVARCHAR(100)
)

CREATE TABLE Pet_Type (


pet_typeId INT PRIMARY KEY identity(1,1),
pet_typeName VARCHAR(50) unique not null
);

CREATE TABLE Pet (


pet_id INT PRIMARY KEY identity(1,1),
pet_name NVARCHAR(100) not null,
pet_gender NVARCHAR(30) check (pet_gender LIKE N'Đực' or pet_gender like
N'Cái'),
pet_age INT,
owner_id INT,
pet_typeId INT,
Constraint FK_Pet_Owner FOREIGN KEY (owner_id) REFERENCES Pet_Owner(owner_id),
Constraint FK_Pet_Type FOREIGN KEY (pet_typeId) REFERENCES Pet_Type(pet_typeId)
);

CREATE TABLE Treatment (


treatment_id Varchar(3) PRIMARY KEY,
treatment_name NVARCHAR(100) not null
);
--Pet_visit table that contains all visit of a pet
--and each pet can have only one visit recorded per date.
CREATE TABLE Pet_Visit (
pet_id INT not null,
visitdate DATE not null,
treatment_id VARCHAR(3),
Constraint PK_PetVisit Primary key(pet_id, visitdate),
Constraint Pet_Visit_Pet FOREIGN KEY (pet_id) REFERENCES Pet(pet_id),
Constraint Pet_Visit_Treatment FOREIGN KEY (treatment_id) REFERENCES
Treatment(treatment_id) on update cascade
);

--Insert into database


Insert into Pet_Owner(owner_fname,owner_lname,owner_address,owner_phone)
values(N'Nguyễn Văn',N'Ánh', N'Đà Nẵng','0905111222'),
(N'Trần Thị', N'Khánh Hồng', N'TP Đà Nẵng', NULL),
(N'Nguyễn',N'Diệu My','HCM', '0909111222'),
(N'Trần Kim', N'Huy','HCM', '0905919001'),
(N'Trần', N'Quang Minh', 'Quảng Nam',NULL),
(N'Trần Lê', N'Thiện Duyên', 'Quảng Ngãi',NULL)

INSERT INTO Pet_Type (pet_typeName)


VALUES
(N'Chó'),
(N'Mèo'),
(N'Chim');

--Table Pet
INSERT INTO Pet (pet_name, pet_gender, pet_age, owner_id, pet_typeId)
VALUES
('Buddy', N'Đực', 5, 1, 1),
('Mittens', N'Cái', 3, 2, 2),
('Rex', N'Đực', 8, 3, 1),
('Whiskers',N'Đực', 2, 4, 2),
('Tweety',N'Cái', 1, 5, 2),
('Luccy',N'Cái', 1, 5, 2),
('Tikky',N'Cái', 1, 4, 1)

INSERT INTO Treatment (treatment_id, treatment_name)


VALUES
('VAC', N'Tiêm Vaccin'),
('CLN', N'Làm sạch răng'),
('BLD', N'Thử máu'),
('CHK', N'Thăm khám'),
('SUR', N'Phẫu thuật'),
('XRY', N'Chụp X-Ray')

INSERT INTO Pet_Visit (pet_id, visitdate, treatment_id)


VALUES
(1, '2023-01-15', 'VAC'),
(2, '2023-02-20', 'CLN'),
(3, '2023-03-10', 'VAC'),
(4, '2023-04-25', 'VAC'),
(5, '2023-05-30', 'VAC'),
(1, '2023-06-15', 'CHK'),
(2, '2023-07-20', 'SUR'),
(1, '2023-08-10', 'SUR'),
(4, '2023-09-25', 'VAC')

--Solution
-- Question 2:
--Code for create new table for storing pet medication information

--Insert data for the table

--Question 3: Store procedure

--Test the store procedure

--Question 4: Create the trigger

--Test the Trigger

--Question 5: View for count

--Test the View

You might also like