Tải bản đầy đủ (.pdf) (6 trang)

Answersheet.pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (107.68 KB, 6 trang )

ANSWER SHEET
Student code:

1513804

Student name:

NGUYỄN XUÂN TRỰC

1.
- Your create table statement is:
CREATE TABLE fudgemart_agents (
agent_id

INT

IDENTITY(1,1),

agent_name

VARCHAR(50),

agent_address

VARCHAR(50),

agent_country

VARCHAR(2),

agent_created_date



DATETIME

CHECK (agent_created_date <= GETDATE()),

BIT

DEFAULT 1

agent_manager_id INT,
agent_status

NOT NULL,

CONSTRAINT PK_agents_id PRIMARY KEY(agent_id),
CONSTRAINT FK_agents_employees FOREIGN KEY(agent_manager_id) REFERENCES
fudgemart_employees(employee_id)
);

- Your create index statement is:
CREATE INDEX idx_country ON fudgemart_agents(agent_country);

2. Database diagram (with the additional table fudgemart_agents) is printed as below


3. Select satement 3.a
SELECT
FROM
WHERE


*
fudgemart_employees
employee_department = 'Electronics' AND YEAR(GETDATE())-YEAR(employee_birthdate)>30;

Select statement 3.b
SELECT
FROM
WHERE

*
fudgemart_products
product_retail_price = 2*product_wholesale_price;

4. Create view statement 4a
CREATE VIEW products_vendor_mikey AS
SELECT
*
FROM
fudgemart_vendors
INNER JOIN
fudgemart_products
WHERE
vendor_name='Mikey'
AND product_is_active=1;

ON

vendor_id=product_vendor_id

Create view statement 4b

CREATE VIEW payrolltimes AS
SELECT
fet.timesheet_employee_id, fet.timesheet_payrolldate
FROM
fudgemart_employee_timesheets fet, (
SELECT
fet.timesheet_employee_id, COUNT(fet.timesheet_employee_id) AS C
FROM
fudgemart_employee_timesheets fet
WHERE
fet.timesheet_hours < 40
GROUP BY
fet.timesheet_employee_id) AS A
WHERE
fet.timesheet_employee_id = A.timesheet_employee_id;


5.
5a. Create stored procedure statement
CREATE PROCEDURE p_fudgemart_add_new_vendor
@vendor_id
INT,
@vendor_name
VARCHAR(50),
@vendor_phone
VARCHAR(20),
@vendor_website
VARCHAR(1000)
AS
BEGIN

SET NOCOUNT ON;
IF EXISTS (SELECT * FROM fudgemart_vendors WHERE vendor_id=@vendor_id)
BEGIN
PRINT 'The vendor id has already existed in the database';
RETURN 0
END
ELSE IF EXISTS (SELECT * FROM fudgemart_vendors WHERE vendor_name=@vendor_name)
BEGIN
PRINT 'The vendor name has already existed in the database';
RETURN 0
END
ELSE
INSERT INTO fudgemart_vendors (vendor_id, vendor_name, vendor_phone, vendor_website)
VALUES(@vendor_id, @vendor_name, @vendor_phone, @vendor_website)
RETURN @@ROWCOUNT
END


5b. Statements for excuting the procedure to insert data
INSERT INTO fudgemart_vendors VALUES (21, 'Vinmart', '555-2222', 'www.vinmart.com');
INSERT INTO fudgemart_vendors VALUES (22, 'Mikerosoft', '555-8888', 'www.microsoft.com');

5c. Create stored procedure statement
CREATE PROCEDURE p_fudgemart_update_price_product
@product_id
INT,
@diff_price
MONEY,
@mode
BIT

AS
BEGIN
SET NOCOUNT ON;
IF (@product_id <= 0)
BEGIN
PRINT 'the diff price must be greater than 0'
RETURN 0
END
ELSE
BEGIN
IF (@mode = 1)
UPDATE fudgemart_products SET product_retail_price=product_wholesale_price+@diff_price;
ELSE IF (@mode = 0)
UPDATE fudgemart_products SET product_retail_price=product_wholesale_price-@diff_price;
END
END


5d. Create trigger statements
CREATE TRIGGER trg_insert_agents
ON fudgemart_agents
FOR INSERT
AS
BEGIN
DECLARE @country VARCHAR(2)
DECLARE @agent_created_date DATETIME
SET @country = (SELECT agent_country FROM inserted)
SET @agent_created_date = (SELECT agent_created_date FROM inserted)
IF (@country = NULL)
SET @country = 'VN'

ELSE
BEGIN
IF EXISTS (SELECT * FROM inserted WHERE inserted.agent_country = @country)
RETURN
END
IF (@agent_created_date = NULL)
SET @agent_created_date = GETDATE()
INSERT INTO fudgemart_agents(agent_id, agent_name, agent_address, agent_country, agent_created_date,
agent_manager_id, agent_status)
SELECT agent_id, agent_name, agent_address, agent_country, agent_created_date, agent_manager_id,
agent_status
FROM inserted
END

6.
6a. Create function statement
….


6b. Select statement which calls the function
….

6c. Create function statement
….



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×