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

Funda Lecture 5

The document outlines SQL constraints used to enforce rules on data within tables, including CHECK, DEFAULT, NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY constraints. It provides syntax examples for creating tables with these constraints and explains their functions, such as ensuring data integrity and uniqueness. Additionally, it highlights the relationship between PRIMARY KEY and FOREIGN KEY constraints in maintaining referential integrity across tables.
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)
2 views4 pages

Funda Lecture 5

The document outlines SQL constraints used to enforce rules on data within tables, including CHECK, DEFAULT, NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY constraints. It provides syntax examples for creating tables with these constraints and explains their functions, such as ensuring data integrity and uniqueness. Additionally, it highlights the relationship between PRIMARY KEY and FOREIGN KEY constraints in maintaining referential integrity across tables.
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/ 4

Fundamentals of Database System

Midterms (SQL Constraints)

SQL Constraints - SQL constraints are used to specify rules for the data in a table. If there is an
between the constraint and the data action, the action is aborted by the constraint.

(***Note: Constraints can be specified when the table is created (inside the CREATE TABLE state
or after the table is created inside the ALTER TABLE statement. ***)

Sample Code:
SQL CREATE TABLE + CONSTRAINT Syntax
CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);

Types of Constraints in SQL

1. CHECK - Ensures that the value in a column meets a specific condition

2. DEFAULT - Specifies a default value for a column

3. NOT NULL - Indicates that a column cannot store NULL value. The NOT NULL constraint enfor
to NOT accept NULL values.
***(Note: The NOT NULL constraint enforces a field to always contain a value. This means that y
cannot insert a new record, or update a record without adding a value to this field.***)

Example Not Null


CREATE TABLE PersonsNotNull
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
4. UNIQUE - Ensures that each row for a column must have a unique value. The UNIQUE constra
identifies each record in a database table.

Example Unique Value:


CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
Or
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (P_Id)
)

5. PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combin
of two or more columns) have a unique identity which helps to find a particular record in a table
easily and quickly

Example Primary Key

CREATE TABLE Persons


(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
Or

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

(***Note: The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness
column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint d
Thus, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constrain
table. ***)

6. FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in an
A FOREIGN KEY in one table points to a PRIMARY KEY in another table. Refer to the table below:

The "Persons" table:

P_Id LastName FirstName Address

1 Hansen Ola Timoteivn 10

2 Svendson Tove Borgvn 23

3 Pettersen Kari Storgt 20

The "Orders" table:

O_Id OrderNo P_Id


1 77895 3

2 44678 3

3 22456 2

4 24562 1

(***Note: The "P_Id" column in the "Orders" table points to the "P_Id" column in the "Persons" ta
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 links between tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key c
because it has to be one of the values contained in the table it points to.***).

Example Foreign Key

CREATE TABLE Orders


(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)

Or

CREATE TABLE Orders


(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy