Lab 5 DBS - 082807
Lab 5 DBS - 082807
1. Keys in a Database......................................................................................................................................................3
1.1. KEYS....................................................................................................................................................................3
1.2. TYPES of KEYS..................................................................................................................................................3
1.2.1. Candidate Keys................................................................................................................................................3
1.2.2. Super Keys.......................................................................................................................................................3
1.2.3. Primary Keys...................................................................................................................................................3
1.2.4. Alternate Keys.................................................................................................................................................3
1.2.5. Foreign Keys....................................................................................................................................................3
2. Constraints................................................................................................................................................................... 3
2.1. Definition and Explanation.................................................................................................................................3
2.2. Types of Constraints............................................................................................................................................3
2.2.1. NOT NULL......................................................................................................................................................4
2.2.2. UNIQUE...........................................................................................................................................................4
2.2.3. CHECK............................................................................................................................................................4
2.2.4. DEFAULT........................................................................................................................................................4
2.2.5. INDEX..............................................................................................................................................................5
2.3. Dropping the Constraints...................................................................................................................................5
3. Working in DBS using KEYS and CONSTRAINTS................................................................................................5
3.1. DDL Statements:.................................................................................................................................................5
3.2. DDL Statements:.................................................................................................................................................6
3.2.1. Creating and Using Database “umer_abid_lab_task_06”........................................................................6
3.2.2. Creating Table PUBLISHERS...................................................................................................................6
3.2.3. Altering Table PUBLISHERS....................................................................................................................6
3.2.4. Creating New Table Books..........................................................................................................................6
3.2.5. Altering New Table Books..........................................................................................................................7
2. Constraints
2.1. Definition and Explanation
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and
reliability of the data in the table. If there is any violation between the constraint and the data action, the
action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and table
level constraints apply to the whole table. Constraints can be specified when the table is created with
the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement.
2.1.2. UNIQUE
This constraint ensures that the set of attributes have unique values. No two tuples can have same
value in the specified attributes.
Syntax:
1. <Attribute> <datatype> UNIQUE
2. ALTER TABLE Table_Name ADD CONSTRAINT cons_name UNIQUE(Attribute);
Example:
1. P_Name varchar (50) UNIQUE
2. ALTER TABLE BOOKS ADD CONSTRAINT UQ_Book_Title UNIQUE(Book_Title);
2.1.3. CHECK
This constraint ascertains that the value inserted in an attribute must satisfy a given expression.
Syntax:
1. <Attribute> <datatype> CHECK (<expression>)
2. ALTER TABLE Table_Name ADD CONSTRAINT cons_name CHECK
(Expression);
Example:
1. Price Numeric (4) CHECK (Price > 20)
2. ALTER TABLE PUBLISHERS ADD CONSTRAINT chk_PID_greater_than_0
CHECK (P_ID>0);
2.1.4. DEFAULT
This constraint consists of a set of default values for a column when no value is specified.
2.1.5. INDEX
This constraint is used to create and retrieve data from the database very quickly.