0% found this document useful (0 votes)
98 views3 pages

Database System CUIT201 PDF

This document discusses database concepts and provides SQL statements to create and manipulate tables in a sample database for a university tuck shop. [1] It creates a database called "dbcuttuckshop" and a table called "tproduct" with fields like product_id, name, expiry_date, barcode, quantity and reorder level. [2] It defines database keys like primary keys that uniquely identify records, foreign keys that link to other tables, and alternate keys that can also uniquely identify records. [3] It adds a check constraint to ensure quantity in stock is between 0-100 and provides example queries to update records, find products below reorder level or past expiry, and select products

Uploaded by

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

Database System CUIT201 PDF

This document discusses database concepts and provides SQL statements to create and manipulate tables in a sample database for a university tuck shop. [1] It creates a database called "dbcuttuckshop" and a table called "tproduct" with fields like product_id, name, expiry_date, barcode, quantity and reorder level. [2] It defines database keys like primary keys that uniquely identify records, foreign keys that link to other tables, and alternate keys that can also uniquely identify records. [3] It adds a check constraint to ensure quantity in stock is between 0-100 and provides example queries to update records, find products below reorder level or past expiry, and select products

Uploaded by

blessing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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;

You might also like