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

DDL Lab3

The document discusses SQL constraints and auto-increment fields. It defines six types of SQL constraints - NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT - that can be applied at the column or table level to enforce data rules. It also explains that auto-increment allows a unique number to be automatically generated for a primary key field each time a new record is inserted into a table. The syntax for defining auto-increment and examples of using constraints and auto-increment in table creation statements are provided.

Uploaded by

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

DDL Lab3

The document discusses SQL constraints and auto-increment fields. It defines six types of SQL constraints - NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT - that can be applied at the column or table level to enforce data rules. It also explains that auto-increment allows a unique number to be automatically generated for a primary key field each time a new record is inserted into a table. The syntax for defining auto-increment and examples of using constraints and auto-increment in table creation statements are provided.

Uploaded by

karim mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Database2 (Section 3)

SQL Constraints:

- SQL constraints are used to specify rules for the data in a table.

- Constraints can be specified when the table is created (inside the


CREATE TABLE statement) or after the table is created (inside the
ALTER TABLE statement).

- In SQL, we have the following constraints:

1. NOT NULL - Indicates that a column cannot store NULL value


2. UNIQUE - Ensures that each row for a column must have a unique value
3. PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures
that a column (or combination of two or more columns) have a unique
identity which helps to find a particular record in a table more easily and
quickly
4. FOREIGN KEY - Ensure the referential integrity of the data in one table to
match values in another table.
5. CHECK - Ensures that the value in a column meets a specific condition.
6. DEFAULT - Specifies a default value for a column and it will be added
to all new records IF no other value is specified.

- There are two levels to create a constraint:


1- Based on column level.
2- Based on table level.

Constraint type Column level Table level


1. Not null 

2. Primary key  

3. Composite key (primary key) 

4. Foreign key  

5. check  

1
6. default 

7. unique  

- Constraint Syntax in SQL:

1. Based on column level: constraint const_name const_type

Ex:

employee_id int constraint e_eid_pk primary key,


dno int not null constraint emp_dno_fk foreign key references
departments(deptid),

2. Based on table level: constraint const_name const_type (column_name)

Ex:

employee_id int not null,


dno int not null,
constraint e_eid_pk primary key(employee_id),
constraint emp_dno_fk foreign key(dno) references departments(deptid)

SQL Auto Increment Field:

- Auto-increment allows a unique number to be generated automatically


when a new record is inserted into a table.

- Often this is the primary key field that we would like to be created
automatically every time a new record is inserted.

- Auto Increment Syntax in SQL:

Identity

Or

Identity (seed value, increment value)

2
EX:

Employees
emp_i F_nam mini L_nam ss bdat E_addres gende salar manage dn
d e t e n e s r y r o

use company
create table employees
(
emp_id int not null constraint pk_emp_eid primary key identity,
fname nvarchar(20) not null,
mname nvarchar(20) not null,
lname nvarchar(20) not null,
ssn nvarchar(14) not null,
badte date,
e_address nvarchar(100),
--salary1 money,
salary2 decimal(10,2) constraint ck_emp_salary check(salary2 between 1000 and
10000) constraint def_emp_salary default 3000,
gender nvarchar(5) constraint ck_emp_gender check(gender ='male' or gender
='female') constraint def_emp_gender default 'male',
dno int not null,
manager int constraint fk_emp_manager foreign key references employees(emp_id)
)

You might also like