Sunteți pe pagina 1din 3

CHINHOYI UNIVERSITY OF TECHNOLOGY

BLESSING GWANYANYA
C18132501F
DATABASE SYSTEMS (CUIT201)
Assignment 2
1.

a) CREATE DATABASE dbcuttuckshop;

b) i) CREATE TABLE tproduct(


product_id NUMERIC NOT NULL,
product_name CHARACTER VARYING(70) NOT NULL,
expiry_date DATE NOT NULL,
bar_code NUMERIC(17) NOT NULL,
quantity_in_stock INT NOT NULL,
Reorder_level INT NOT NULL,
PRIMARY KEY (product_id));

c) c) i) Primary Key – A primary key is a special relational database table column or


combination of columns designated to uniquely identify all table records. For example in
the product table tproducts our primary key is the product_id where every value is
different from another.

ii) Candidate Key – Candidate key is a column or set of columns in a table that can
uniquely identify any database record without referring to any other data. Cuttuckshop
database has productid and CategoryID as uniquely defined tables.

iii) Foreign Key – Foreign keys are the columns of a table that show the primary key of
other tables. CategoryID shows the primary key in the tcategory table.

iv) Alternate Key – ) An alternate keys contain one or more columns whose combined
values uniquely identify every row in a table. For example in the category table
(tcategory), our alternate key is category_name because it can be use to uniquely
identify a record in the table.

d) Domain integrity specifies that all columns in a relational database must be declared
upon a defined domain. The primary unit of data in the relational data model is the data
item. This type of data integrity warrants the following: the identity and purpose of a field is
clear and all of the tables in which it appears are properly identified; field definitions are
consistent throughout the database; the values of a field are consistent and valid; and the
types of modifications, comparisons and operators that can be applied to the values in the
field are clearly identified.

e) ALTER TABLE tproduct


ADD CONSTRAINT CHECK_CONST CHECK (0<quantity_in_stock<100);
f) i) UPDATE tproduct_category
SET category_id = 1
WHERE product_id = 900090

ii) SELECT * FROM tproduct WHERE Reorder_level > quantity_in_stock

iii) SELECT *FROM tproduct WHERE expiry_date < 2019/04/01

g) SELECT product_name,CURDATE(),expiry_date,
TIMESTAMPDIFF(YEAR,expiry_date,CURDATE()) AS age
FROM tproduct WHERE expiry_date is NOT NULL ORDER BY age;

h) SELECT *FROM tproduct_category WHERE category_id = 1;

i) SELECT product_name,CURDATE(),expiry_date,
TIMESTAMPDIFF(YEAR,expiry_date,CURDATE()) AS age
FROM tproduct WHERE expiry_date is NOT NULL ORDER BY age;

S-ar putea să vă placă și