0% found this document useful (0 votes)
15 views17 pages

Databaselab 6

Uploaded by

210316
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)
15 views17 pages

Databaselab 6

Uploaded by

210316
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/ 17

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER

ENGINEERING

LAB TITLE: SQL Constraints

Student Name: Muhammad Bilal, Agha Ammar Khan, Muneeb ur Rehman

Reg. No: 210316,210300,212106

LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)

Ability to Conduct
Experiment

Ability to assimilate the


results

Effective use of lab


equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: 30/10/2024 Signature:


LAB#06
TITLE: SQL Constraints

Objective:
Understand the different commands and constrains used in SQL.

The SQL UNION Operator:


The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of
columns. The columns must also have similar data types. Also, the columns in each SELECT
statement must be in the same order.

SQL UNION Syntax:

Note: The UNION operator selects only distinct values by default. To allow duplicate values,
use UNION ALL.

SQL UNION ALL Syntax:

PS: The column names in the result-set of a UNION are always equal to the column names in
the first SELECT statement in the UNION.
SQL UNION Example:
Look at the following tables:
"Employees_Norway":

"Employees_USA":

Now we want to list all the different employees in Norway and USA. We use the following
SELECT statement:
The result-set will look like this:

Note: This command cannot be used to list all employees in Norway and USA. In the
example above we have two employees with equal names, and only one of them will be
listed. The UNION command selects only distinct values.

SQL UNION ALL Example:


Now we want to list all employees in Norway and USA:

Result:

SQL Constraints:
Constraints are used to limit the type of data that can go into a table. Constraints can be
specified when a table is created (with the CREATE TABLE statement) or after the table is
created (with the ALTER TABLE statement).
We will focus on the following constraints:
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT

SQL NOT NULL Constraint:


By default, a table column can hold NULL values. The NOT NULL constraint enforces a
column to NOT accept NULL values. The NOT NULL constraint enforces a field to always
contain a value. This means that you cannot insert a new record, or update a record without
adding a value to this field. The following SQL enforces the "P_Id" column and the
"LastName" column to not accept NULL values:

SQL UNIQUE Constraint:


The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and
PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of
columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table. SQL UNIQUE Constraint on CREATE TABLE The following SQL creates a
UNIQUE constraint on the "P_Id" column when the "Persons" table is created:

MySQL: CREATE TABLE Persons


To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple
columns, use the following SQL syntax:

SQL UNIQUE Constraint on ALTER TABLE:


To create a UNIQUE constraint on the "P_Id" column when the table is already created, use
the following SQL:

To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple
columns, use the following SQL syntax:

SQL PRIMARY KEY Constraint:


The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary
keys must contain unique values. A primary key column cannot contain NULL values. Each
table should have a primary key, and each table can have only one primary key.
SQL PRIMARY KEY Constraint on CREATE TABLE:
The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Persons" table is
created:

MySQL:

SQL FOREIGN KEY Constraint:


A FOREIGN KEY in one table points to a PRIMARY KEY in another table. Let's illustrate the
foreign key with an example. Look at the following two tables: The "Persons" table:

The "Orders" table:


Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in the
"Persons" table.The "P_Id" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table. The
FOREIGN KEY constraint is used to prevent actions that would destroy link between tables.
The FOREIGN KEY constraint also prevents that invalid data is inserted into the foreign key
column, because it has to be one of the values contained in the table it points to.

SQL FOREIGN KEY Constraint on CREATE TABLE:


The following SQL creates a FOREIGN KEY on the "P_Id" column when the "Orders" table is
created:

MySQL:

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on
multiple columns, use the following SQL syntax:
SQL FOREIGN KEY Constraint on ALTER TABLE:
To create a FOREIGN KEY constraint on the "P_Id" column when the "Orders" table is already
created, use the following SQL:

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on
multiple columns, use the following SQL syntax:

To DROP a FOREIGN KEY Constraint:


To drop a FOREIGN KEY constraint, use the following SQL:

MySQL:

SQL CHECK Constraint:


The CHECK constraint is used to limit the value range that can be placed in a column. If you define a
CHECK constraint on a single column it allows only certain values for this column.

If you define a CHECK constraint on a table it can limit the values in certain columns based on values
in other columns in the row.

SQL CHECK Constraint on CREATE TABLE:


The following SQL creates a CHECK constraint on the "P_Id" column when the "Persons"
table is created. The CHECK constraint specifies that the column "P_Id" must only include
integers greater than 0.
My SQL:

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple
columns, use the following SQL syntax:

SQL CHECK Constraint on ALTER TABLE:


To create a CHECK constraint on the "P_Id" column when the table is already created, use
the following SQL:

To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple
columns, use the following SQL syntax:

To DROP a CHECK Constraint:


To drop a CHECK constraint, use the following SQL:
SQL DEFAULT Constraint:
The DEFAULT constraint is used to insert a default value into a column. The default value will
be added to all new records, if no other value is specified.

SQL DEFAULT Constraint on CREATE TABLE:


The following SQL creates a DEFAULT constraint on the "City" column when the "Persons"
table is created:

The DEFAULT constraint can also be used to insert system values, by using functions like
GETDATE():

SQL DEFAULT Constraint on ALTER TABLE:


To create a DEFAULT constraint on the "City" column when the table is already created, use the
following SQL:
To DROP a DEFAULT Constraint:
To drop a DEFAULT constraint, use the following SQL:

SQL CREATE INDEX Statement:


The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database
application to find data fast; without reading the whole table. Indexes
An index can be created in a table to find data more quickly and efficiently. The users cannot
see the indexes, they are just used to speed up searches/queries. Note: Updating a table
with indexes takes more time than updating a table without (because the indexes also need
an update). So you should only create indexes on columns (and tables) that will be
frequently searched against.

SQL CREATE UNIQUE INDEX Syntax:


Creates a unique index on a table. Duplicate values are not allowed:

Note: The syntax for creating indexes varies amongst different databases. Therefore: Check
the syntax for creating indexes in your database.

CREATE INDEX Example:


The SQL statement below creates an index named "PIndex" on the "LastName" column in
the "Persons" table:

If you want to create an index on a combination of columns, you can list the column names
within the parentheses, separated by commas:
Lab Tasks:

In lab Tasks:

Code and Output:


Conclusion:
In this lab, we explored various SQL constraints and commands, which are essential for
maintaining data integrity and enforcing rules within a database. By applying constraints
such as `PRIMARY KEY`, `FOREIGN KEY`, `UNIQUE`, `NOT NULL`, `CHECK`, and `DEFAULT`, we
learned how to structure data relationships, avoid redundancy, and ensure valid entries in
database tables. These constraints help enforce rules at the database level, reducing the
need for application-level validations. Overall, the lab demonstrated how SQL constraints are
fundamental for effective database design and reliable data management, supporting
accurate and consistent data across tables.

You might also like