You are on page 1of 19

Database

Creating Tables and


Systems
Constraints in
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

 DDL commands execute as soon as they


are issued, and do not need to be
explicitly saved

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

 Integrity: define primary and foreign


keys
 Value: specify values or ranges of
values that can be inserted

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

will automatically generate an unintuitive


name

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:

CONSTRAINT constraint_name 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

primary key in another table


 Syntax:

CONSTRAINT constraint_name REFERENCES


primary_key_table_name (field_name)

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

because they make the database very


inflexible

17
Types of Value Constraints

 Check condition: restricts to specific values


 Example: s_gender (M or F)
CONSTRAINT my_students_s_gender_cc
CHECK (s_gender = ‘M’) OR (s_gender = ‘F’)
 Not NULL: specifies that a field cannot be
NULL
 Example:
CONSTRAINT my_students_s_dob_nn
NOT NULL
18
Types of Value Constraints

 Default: specifies a default value that is inserted


automatically
 Example
: CHAR(2) DEFAULT ‘WI’
s_state
 Unique
 Table constraint
 Specifies that a non-primary key field must have a unique
value
CONSTRAINT consultant_c_email_uk UNIQUE (c_email)

19

You might also like