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

Classwork 69686 2

The document discusses database concepts and the Structured Query Language (SQL), focusing on creating tables with constraints to maintain data integrity. It categorizes constraints into column constraints and table constraints, detailing various types such as NOT NULL, PRIMARY KEY, UNIQUE, CHECK, DEFAULT, and FOREIGN KEY. Examples are provided to illustrate the syntax for creating tables with specified constraints.

Uploaded by

erictsam58
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 views3 pages

Classwork 69686 2

The document discusses database concepts and the Structured Query Language (SQL), focusing on creating tables with constraints to maintain data integrity. It categorizes constraints into column constraints and table constraints, detailing various types such as NOT NULL, PRIMARY KEY, UNIQUE, CHECK, DEFAULT, and FOREIGN KEY. Examples are provided to illustrate the syntax for creating tables with specified constraints.

Uploaded by

erictsam58
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

UNIT IV

DATABASE CONCEPTS

AND

THE STRUCTURED QUERY


LANGUAGE

SESSION 08

1
Creating table with constraints:
A constraint is condition or check applicable on a field or set of fields.
Constraints are important to maintain data integrity.

Constraints are mainly classified into two:

 Column constraints: These constraints are applied to only one


column at a time.
 Table constraints: These constraints are applied to more than one
column at a time.

Syntax:

Create table <table name>

(<column name><data type>[(<size>)]<column constraint>,

<Column name><data type>[(<size>)]<column constraint>,………..

<table constraint>(<column name>,<column name>…….));

Different constraints:
1. NOT NULL
2. PRIMARY KEY
3. UNIQUE
4. CHECK
5. DEFAULT
6. FOREIGN KEY

1. NOT NULL: All the rows in the specified column should have some values.

2. PRIMARY KEY: This constraint declares a column as the primary key of


the table. Primary Key column cannot allow null values.

2
3. UNIQUE: This constraint ensures that no two rows have the same value in
the specified column.

Difference between Primary Key and Unique constraint:


Primary Key Constraint Unique Constraint
This constraint can be applied only This constraint can be applied to
one column at a time in a table. more than one column in a table.

Primary key does not allow NULL It allows NULL values.


values in that column
Eg: Create table Emp (EmpNO Eg: Create table Emp (EmpNo
int(2) Not Null Primary Key , int(2) Unique, Ename char(20) Not
Ename char(2) Not Null); Null);
Example:

1. Create the table CUSTOMER with the following specifications:

Column Data Size Constraint


Name Type
CustomerId Int 10 Primary Key

Name Varchar 30 Not empty

Phone Varchar 20 Unique

Create table Customer (CustomerId int(10) not null primary key,


Name Varchar(30) Not Null,
Phone varchar(20) unique);

You might also like