Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

SQL Constraints

SQL constraints are used to define and enforce rules for data within tables. Some common constraints include: 1) NOT NULL ensures columns cannot contain NULL values. 2) UNIQUE prevents duplicate values in a column. 3) PRIMARY KEY uniquely identifies each row combining UNIQUE and NOT NULL. 4) FOREIGN KEY links data between tables by referencing the PRIMARY KEY of another table. Constraints are added during table creation or later using ALTER TABLE to modify structures and relationships. They help ensure data accuracy and integrity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL Constraints

SQL constraints are used to define and enforce rules for data within tables. Some common constraints include: 1) NOT NULL ensures columns cannot contain NULL values. 2) UNIQUE prevents duplicate values in a column. 3) PRIMARY KEY uniquely identifies each row combining UNIQUE and NOT NULL. 4) FOREIGN KEY links data between tables by referencing the PRIMARY KEY of another table. Constraints are added during table creation or later using ALTER TABLE to modify structures and relationships. They help ensure data accuracy and integrity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Constraints

- SQL constraints are used to specify rules for the data in a table.
- A constraint is something that limits 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.
- The following constraints are commonly used in SQL:

1. NOT NULL - Ensures that a column cannot have a NULL value.


2. UNIQUE - Ensures that all values in a column are different.
3. PRIMARY KEY- A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table.
4. FOREIGN KEY- Prevents actions that would destroy links between tables.
5. CHECK- Ensures that the values in a column satisfies a specific condition.
6. DEFAULT- Sets a default value for a column if no value is specified.
7. CREATE INDEX- Used to create and retrieve data from the database very quickly.

1. SQL NOT NULL Constraint


Specifies that this column cannot hold NULL values (constraints of this type are
not nameable).

This enforces a field to always contain a value, which means that you cannot
insert a new record, or update a record without adding a value to this field.

Example:

CREATE TABLE Persons (


ID Char NOT NULL,
Name varchar(255) NOT NULL,
Email varchar(255) NOT NULL,
Age number(3)
);

1.1 SQL NOT NULL on ALTER TABLE


To create a NOT NULL constraint on the "Age" column when the "Persons" table is
already created, use the following SQL:

Example:

ALTER TABLE Persons


MODIFY Age number NOT NULL;

ALTER TABLE Persons


MODIFY name varchar(25) NOT NULL;

2. SQL UNIQUE Constraint


- The UNIQUE constraint ensures that all values in a column are different.

- Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.

CREATE TABLE Persons (


ID Char NOT NULL UNIQUE,
Name varchar(255) NOT NULL,
Age number(3)
);

2.1 SQL UNIQUE Constraint on ALTER TABLE


To add unique constraint to a table that has already been created, you can
use the ‘ALTER TABLE’ statement in SQL.

Syntax: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE


(column_name);
Example:

1st Method: ALTER TABLE Persons


ADD UNIQUE (ID);
2nd Method: ALTER TABLE Person
ADD CONSTRAINT person_unique UNIQUE (ID);

2.2 DROP a UNIQUE Constraint


To drop a unique constraint from a table, you can use the ‘ALTER TABLE’
statement in SQL along with the DROP CONSTRAINT clause.
Syntax: ALTER TABLE table_name DROP CONSTRAINT constraint_name;
Example:

ALTER TABLE Persons


DROP CONSTRAINT person_unique;

3. PRIMARY KEY
- A Primary key uniquely identifies each row table.
- It must contain not null and unique 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 composite keys.
- To create a Primary key in the table, we have to use a keyword; “PRIMARY KEY ( )
- Syntax:
- CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n) );
- Example:

CREATE TABLE employees


(employee_number char(3),
last_name char(50) NOT NULL,
first_name char(50) NOT NULL,
salary number,
dept_id number,
CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);

3.1 SQL PRIMARY KEY on ALTER TABLE


To create a PRIMARY KEY constraint on the "ID" column when the
table is already created, use the following SQL:
Syntax:

ALTER TABLE Persons


ADD CONSTRAINT PK_Person PRIMARY KEY (ID);

3.2 DROP a PRIMARY KEY Constraints


To drop a primary key constraints, use following sql commands.
Syntax:

ALTER TABLE Persons DROP CONSTRAINT PK_Person;

4. FOREIGN KEY.
- Foreign key is used to create a link between two tables.

-The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
- A FOREIGN KEY is a field (or collection of fields) in one table, that refers to
the PRIMARY KEY in another table.

- The table with the foreign key is called the child table, and the table with the
primary key is called the referenced or parent table.

4.1 SQL FOREIGN KEY ON Create table

CREATE TABLE employees (


employee_id CHAR PRIMARY KEY,
employee_name VARCHAR(50),
salary NUMBER(5),
department_id CHAR,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);

You might also like