0% found this document useful (0 votes)
6 views36 pages

3 - Physical Model

The document provides an overview of physical database design, focusing on SQL table creation, data types, and constraints such as primary key, foreign key, unique, and check constraints. It outlines the importance of entity and referential integrity in database management. Additionally, it includes examples of SQL commands for defining these constraints and their functionalities.

Uploaded by

Omer Polat
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)
6 views36 pages

3 - Physical Model

The document provides an overview of physical database design, focusing on SQL table creation, data types, and constraints such as primary key, foreign key, unique, and check constraints. It outlines the importance of entity and referential integrity in database management. Additionally, it includes examples of SQL commands for defining these constraints and their functionalities.

Uploaded by

Omer Polat
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/ 36

Physical

Database Design
And
CONSTRAINTS
13.03.2025 Prof. Dr. Nergiz CAGILTAY 1
CREATE TABLE table_name
( { column_name data_type [ DEFAULT
default_expr ]
[ column_constraint [ ... ] ] |
table_constraint
[ CONSTRAINT constraint_name ]
{ NOT NULL | NULL | UNIQUE |
PRIMARY KEY | CHECK (expression)
CONSTRAINT constraint_name ] {
UNIQUE ( column_name [, ... ] )
| PRIMARY KEY ( column_name [,
... ] ) | CHECK ( expression ) |
FOREIGN KEY ( column_name [,
... ] ) REFERENCES reftable [ (
refcolumn [, ... ] ) ]] [ ON DELETE
action ] [ ON UPDATE action ] }
13.03.2025 Prof. Dr. Nergiz CAGILTAY 3
Most Common data types

char(size)
Fixed-length character string. Size is specified
in parenthesis. Max 255 bytes.

varchar(size)
Variable-length character string. Max size is
specified in parenthesis.

13.03.2025 Prof. Dr. Nergiz CAGILTAY 4


Most Common data types

number(size)
Number value with a max number of column digits
specified in parenthesis

date Date
valuenumber(size,d)
Number value with a maximum number of digits
of "size" total, with a maximum number of "d"
digits to the right of the decimal.

13.03.2025 Prof. Dr. Nergiz CAGILTAY 5


Constraint

• a rule about the data entered into the table


• can be associated to a column and table
• When tables are created, it is common for
one or more columns to have constraints
associated with them

13.03.2025 Prof. Dr. Nergiz CAGILTAY 6


Popular Constraints

13.03.2025 Prof. Dr. Nergiz CAGILTAY 7


Primary Key Constraint

• to define a key value that uniquely


identifies each table row,
• the primary key value for each row in the
table must be unique.
• The primary key constraint can consist of
more than one column in the table.

13.03.2025 Prof. Dr. Nergiz CAGILTAY 8


Primary Key Constraint

CREATE TABLE Books(


isbn CHAR(10),
title CHAR(80),
CONSTRAINT PK_books
PRIMARY KEY(isbn));

13.03.2025 Prof. Dr. Nergiz CAGILTAY 9


Primary Key Constraint
CREATE TABLE orders (
id number,
dt date,
name char(10),
desc varchar(500),
CONSTRAINT PK_orders
PRIMARY KEY (id));

13.03.2025 Prof. Dr. Nergiz CAGILTAY 10


NULL or NOT NULL Constraint
Specifies that a column must have
some value.
NULL (default) allows NULL values in
the column.

13.03.2025 Prof. Dr. Nergiz CAGILTAY 11


NULL or NOT NULL Constraint
CREATE TABLE SECTION (
id NUMBER(3),
name CHAR(30)
CONSTRAINT section_name_null
NOT NULL,
cost NUMBER(8,2),
CONSTRAINT PK_section
PRIMARY KEY (id));

13.03.2025 Prof. Dr. Nergiz CAGILTAY 12


DEFAULT Constraint

Specifies some default value if


no value entered by user.

13.03.2025 Prof. Dr. Nergiz CAGILTAY 13


DEFAULT Constraint
Column Constraint
CREATE TABLE SECTION (
id NUMBER(3),
name CHAR(30)
CONSTRAINT section_name_null
NOT NULL,
cost NUMBER(8,2) DEFAULT 1.00,
CONSTRAINT PK_section
PRIMARY KEY (id));

13.03.2025 Prof. Dr. Nergiz CAGILTAY 14


DEFAULT Constraint
Column Constraint

CREATE TABLE distributors(


name varchar(40)
DEFAULT 'Luso Films',
did integer DEFAULT
nextval('dist_serial'));

13.03.2025 Prof. Dr. Nergiz CAGILTAY 15


UNIQUE Constraint

Specifies that column(s) must


have unique values

13.03.2025 Prof. Dr. Nergiz CAGILTAY 16


UNIQUE Constraint
Column Constraint

CREATE TABLE distributors (


did integer,
name varchar(40) UNIQUE );
CREATE TABLE distributors (
did integer,
name varchar(40),
UNIQUE(name) );
Table Constraint

13.03.2025 Prof. Dr. Nergiz CAGILTAY 17


Check Constraints

Applies a condition to an input column


value.

13.03.2025 Prof. Dr. Nergiz CAGILTAY 18


Check Constraints
CREATE TABLE section (
id NUMBER(3)
CONSTRAINT section_id
CHECK (id > 0),
name CHAR(30) NOT NULL,
book_count NUMBER(6),
CONSTRAINT PK_section
PRIMARY KEY (id));

13.03.2025 Prof. Dr. Nergiz CAGILTAY 19


Check Constraints

CREATE TABLE items (


status CHAR (1) DEFAULT 'A‘
CONSTRAINT itmStsNull NOT NULL
CHECK (status IN ('A', 'X')),
...

13.03.2025 Prof. Dr. Nergiz CAGILTAY 20


Foreign Key Constraints

Specifies that column(s) are a


table foreign key

13.03.2025 Prof. Dr. Nergiz CAGILTAY 21


Foreign Key Constraints

[CONSTRAINT symbol]
FOREIGN KEY [id]
(index_col_name, ...)
REFERENCES tbl_name
(index_col_name, ...)
[ON DELETE {RESTRICT |
CASCADE | SET NULL |
NO ACTION}]
[ON UPDATE {RESTRICT |
CASCADE | SET NULL |
NO ACTION}]

13.03.2025 Prof. Dr. Nergiz CAGILTAY 22


Foreign Keys - example
CREATE TABLE Orders(
ordernumber NUMBER,
cardnum INTEGER,
qty INTEGER,
order_date DATE,
ship_date DATE,
cid INTEGER,
CONSTRAINT PK_orders
PRIMARY KEY (ordernumber),
CONSTRAINT FK_orders
FOREIGN KEY(cid)
REFERENCES customers(cid));

13.03.2025 Prof. Dr. Nergiz CAGILTAY 23


Specifying ON DELETE /
ON UPDATE

When defining a foreign key constraint it


is possible to specify in an ON DELETE /
ON UPDATE clause
what action that shall take place if the
corresponding record in the referenced
table is deleted or updated

13.03.2025 Prof. Dr. Nergiz CAGILTAY 24


Specifying ON DELETE /
ON UPDATE

NO ACTION:
an attempt to delete or update a
primary key value will not be allowed to
proceed if there is a related foreign key
value in the referenced table
13.03.2025 Prof. Dr. Nergiz CAGILTAY 25
Specifying ON DELETE /
ON UPDATE

CREATE TABLE Orders(


ordernumber NUMBER,
cid INTEGER,
CONSTRAINT PK_orders
PRIMARY KEY (ordernumber),
CONSTRAINT FK_orders
FOREIGN KEY(cid)
REFERENCES customers(cid)
ON DELETE NO ACTION);

13.03.2025 Prof. Dr. Nergiz CAGILTAY 26


Specifying ON DELETE /
ON UPDATE
SET DEFAULT:

If a key value in the referenced table


is deleted the corresponding values
in the foreign key table is set to the
default value for the columns in the
foreign key
13.03.2025 Prof. Dr. Nergiz CAGILTAY 27
Specifying ON DELETE /
ON UPDATE
CREATE TABLE t3 (
Act INTEGER NOT NULL DEFAULT 99,
Description VARCHAR,
CONSTRAINT Fkt3
FOREIGN KEY (Act)
REFERENCES t1(Id)
ON UPDATE CASCADE
ON DELETE SET DEFAULT);

13.03.2025 Prof. Dr. Nergiz CAGILTAY 28


Specifying ON DELETE /
ON UPDATE
CASCADE:

Delete or update the row from the


parent table and automatically delete
or update the matching rows in the
child table.

13.03.2025 Prof. Dr. Nergiz CAGILTAY 29


Specifying ON DELETE /
ON UPDATE
CREATE TABLE t3 (
Act INTEGER NOT NULL DEFAULT 99,
Description VARCHAR,
CONSTRAINT Fkt3
FOREIGN KEY (Act)
REFERENCES t1(Id)
ON UPDATE CASCADE
ON DELETE SET DEFAULT);

13.03.2025 Prof. Dr. Nergiz CAGILTAY 30


Specifying ON DELETE /
ON UPDATE
SET NULL:
Delete or update the row from the
parent table and set the foreign key
column(s) in the child table to NULL.
This is only valid if the foreign key
columns do not have the NOT NULL
qualifier specified.
13.03.2025 Prof. Dr. Nergiz CAGILTAY 31
DISABLE Constraints

You may suffix DISABLE to any


other constraint to make database
ignore the constraint,
the constraint will still be available
for later use

13.03.2025 Prof. Dr. Nergiz CAGILTAY 32


DISABLE Constraints

CREATE TABLE book (


isbn NUMBER(10)
CONSTRAINT bookisbn
CHECK (isbn BETWEEN 1 AND 200),
cost NUMBER(8,2)
DEFAULT 0.00 DISABLE,
...

13.03.2025 Prof. Dr. Nergiz CAGILTAY 33


Entity Integrity

• Each table must have a primary key


• No two rows with the same primary key value
• No NULL values in a primary key
• Ensures entities are traceable

13.03.2025 Prof. Dr. Nergiz CAGILTAY 34


Referential Integrity
• Foreign keys in some cases can be NULL
• Ensures valid references among tables

13.03.2025 Prof. Dr. Nergiz CAGILTAY 35


• Groups should be formed out of 3 people
• Each group is responsible for finding an operational system data
• When you find your data ask my permission for its appropriateness
for this course
• Fill out the form until October, 25 @17:30 PM

https://docs.google.com/document/d/1yvVE
mOQhGqiqWLtvN0S40pkKGUhQQK8W/edit

13.03.2025 Prof. Dr. Nergiz CAGILTAY 36

You might also like