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

Constraints

The document outlines various database constraints including NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK, explaining their purposes and providing SQL examples for each. It describes how these constraints ensure data integrity and uniqueness in database tables. Additionally, it includes instructions for altering existing tables to add or drop constraints and poses questions for practical application of the concepts discussed.

Uploaded by

spartan7846
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)
2 views3 pages

Constraints

The document outlines various database constraints including NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK, explaining their purposes and providing SQL examples for each. It describes how these constraints ensure data integrity and uniqueness in database tables. Additionally, it includes instructions for altering existing tables to add or drop constraints and poses questions for practical application of the concepts discussed.

Uploaded by

spartan7846
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

Constraints:

NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is specified.
UNIQUE Constraint: Ensures that all values in a column are different.
PRIMARY Key: Uniquely identified each rows/records in a database table.
FOREIGN Key: Uniquely identified a rows/records in any another database table.
CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain conditions.
NOT NULL:

By default, a column can hold NULL values. If you do not want a column to have a NULL value, then you need to define such
constraint on this column specifying that NULL is now not allowed for that column.

A NULL is not the same as no data, rather, it represents unknown data.

Example:

For example, the following SQL creates a new table called CUSTOMERS in which, ID and NAME and AGE, specify not to accept
NULLs:
CREATE TABLE CUSTOMERS(
ID NUMBER NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE NUMBER NOT NULL,
ADDRESS CHAR (25) ,

);

DEFAULT :

The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not provide a specific value.

Example:

For example, the following SQL creates a new table called CUSTOMERS. Here, SALARY column is set to 5000.00 by default, so in
case INSERT INTO statement does not provide a value for this column, then by default this column would be set to 5000.00.
CREATE TABLE CUSTOMERS(
ID NUMBER NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE NUMBER NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2) DEFAULT 5000.00,
);

UNIQUE:

The UNIQUE Constraint prevents two records from having identical values in a particular column. In the CUSTOMERS table, for
example, you might want to prevent two or more people from having identical age.

Example:

For example, the following SQL creates a new table called CUSTOMERS and adds five columns. Here, AGE column is set to UNIQUE,
so that you cannot have two records with same age:

CREATE TABLE CUSTOMERS(


ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL UNIQUE,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

1
PRIMARY KEY:

A primary key is a field in a table which uniquely identifies each row/record in a database table. Primary keys must contain unique
values. A primary key column cannot have NULL values.

A table can have only one primary key, which may consist of single or multiple fields. When multiple fields are used as a primary
key, they are called a composite key.

If a table has a primary key defined on any field(s), then you can not have two records having the same value of that field(s).

Here is the syntax to define ID attribute as a primary key in a CUSTOMERS table.


CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

FOREIGN KEY:

A foreign key is a key used to link two tables together. This is sometimes called a referencing key.

Foreign Key is a column or a combination of columns whose values match a Primary Key in a different table.

The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign Key in the second table.

If a table has a primary key defined on any field(s), then you can not have two records having the same value of that field(s).

Example:

Consider the structure of the two tables as follows:

ORDERS table:
CUSTOMERS table:

CREATE TABLE ORDERS (


CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
ID INT NOT NULL,
DATE DATETIME,
NAME VARCHAR (20) NOT NULL,
CUSTOMER_ID INT references CUSTOMERS(ID),
AGE INT NOT NULL,
AMOUNT double,
ADDRESS CHAR (25) ,
PRIMARY KEY (ID)
SALARY DECIMAL (18, 2),
);
PRIMARY KEY (ID)

);

CHECK :
The CHECK Constraint enables a condition to check the value being entered into a record. If the condition evaluates to false, the
record violates the constraint and isn't entered into the table.
Example:
For example, the following SQL creates a new table called CUSTOMERS and adds five columns. Here, we add a CHECK with AGE
column, so that you cannot have any CUSTOMER below 18 years:

2
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL CHECK (AGE >= 18),
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID) );
CREATE TABLE Persons
(P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Sandnes')
)

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CHECK (P_Id>0)
)
ALTERING AN EXISTING TABLE

Adding a Constraint

1. ALTER table student add PRIMARY KEY(s_id);

2. ALTER table order_detail add FOREIGN KEY(c_id) REFERENCES Customer_Detail(c_id);

3. ALTER table student add CHECK(s_id>0);

Dropping a Constraint

1. ALTER table student DROP PRIMARY KEY;

2. ALTER TABLE Persons DROP chk_Person;

Here chk_person is the constraint name.

QUESTIONS:
1) Create table person having attribute p_id, fname, lname, age, salary, address using the concept of
constraint and insert 3 rows.
2) Create table order having attribute oid, order_no, p_id using the concept of constraint and make p_id
as foreign key and insert 4 rows into the table.
3) Add and delete constraint from existing table.

You might also like