Professional Documents
Culture Documents
Constraints DBMS
Constraints DBMS
1
SQL Command Types
Data Definition Language (DDL)
Used to create and modify the structure of
database objects
Data Manipulation Language (DML)
Used to insert, update, delete, and view
database data
2
DDL Commands
Used to create and modify the structure of
database objects
CREATE
ALTER
DROP
3
DML Commands
Used to insert, view, and modify database
data
INSERT
UPDATE
DELETE
SELECT
DDL commands need to be explicitly saved or
rolled back
COMMIT
ROLLBACK
4
Creating a Database Table
Syntax
:
CREATE TABLE table_name
( fieldname1 datatype,
fieldname2 datatype, …);
Example:
CREATE TABLE my_students
( s_id NUMBER(6),
s_name VARCHAR2(30),
s_dob DATE,
s_class CHAR(2));
5
Constraints
Rules that restrict the values that can be
inserted into a field
Types of constraints
6
Constraint Levels
Table constraint
Restricts the value of a field with respect to all
other table records
Example: primary key value must
be unique for each record
Column constraint
Restricts values in a specific column
Example: values in an S_GENDER
field must be ‘M’ or ‘F’
7
Constraint Names
Internal name used by DBMS to identify the
constraint
Each constraint name in a user schema
must be unique
If you do not name a constraint, the system
8
Constraint Names
Constraint naming convention:
tablename_fieldname_constraintID
Constraint ID values:
Primary key: pk
Foreign key: fk
Check condition: cc
Not NULL: nn
Unique: uk
Example constraint name:
my_students_s_id_pk
9
Primary Key Constraints
Table-level
Defining a primary key:
Example:
s_id NUMBER(6)
CONSTRAINT student_s_id_pk PRIMARY KEY
10
Primary Key Constraints
Can be defined when field is declared
11
Primary Key Constraints
Can also be defined after all table field definitions are
completed
12
Composite Primary Keys
Synta
x:
CONSTRAINT constraint_name
PRIMARY KEY (field1, field2)
Must be defined after fields that
compose key are defined
13
Foreign Key Constraints
Table-level
Can only be defined after field is defined as a
14
Foreign Key Constraints
Can be defined when field is
declared
15
Foreign Key Constraints
Can also be defined after all table field
definitions are completed
16
Value Constraints
Column-level
Restricts data values that can be inserted in
a field
In general, avoid value constraints
17
Types of Value Constraints
19