0% found this document useful (0 votes)
6 views

DBDev 02 (4) 2

Uploaded by

T O K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DBDev 02 (4) 2

Uploaded by

T O K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

Guide to Oracle 10g

Chapter 2

By Ahmed M. Zeki for ITBIS373


Objectives
After completing this chapter, you should be able to:
! Use SQL commands to create, modify, and drop database
tables
! Explain Oracle user schemas
! Define and create Oracle DB tables
! Debug Oracle SQL commands
! Use online help resources available through the Oracle
Technology Network (OTN)
! View information about your DB tables using Oracle 10g data
dictionary views
! Modify and delete database tables using SQL*Plus
2 / 90
Introduction to SQL
! Structured Query Language (SQL)
◦ Standard query language for RDBs
◦ Users interact with RDB using this high-level query language
◦ Consists of about 30 commands
◦ Enables users to create DB objects and manipulate and view
data

3 / 90
Introduction to SQL
! SQL-99
◦ Most recent version
◦ Not all DBMSs fully comply with SQL-99

! SQL-92
◦ Almost all DBMSs support SQL-92 (SQL ANSI)
◦ Most vendors add extensions to the standard SQL
commands to make their DBs more powerful and easy to use

4 / 90
Introduction to SQL
! Data Definition Language (DDL) commands
◦ Used to create new DB objects
● User names
● Tables
◦ To modify or delete existing objects
◦ The command immediately change the DB, so you don’t need
to save the change explicitly
◦ Usually done by DBAs
◦ Some utilities can be used to execute DDL commands, they
will automatically generate the underlying SQL commands.

5 / 90
Introduction to SQL
! Data Manipulation Language (DML) commands
◦ To insert, update, delete, and view DB data
◦ Must explicitly saved, to make the new data visible to other
DB users

6 / 90
Introduction to SQL
! Reserved words
◦ All SQL commands

! SQL*Plus are not case sensitive, but it is


recommended to use uppercase for reserved words

7 / 90
Oracle 10g User Accounts
! User account
◦ Created for each user
◦ Identified using unique username and password
◦ Each user can create tables and other data objects that
reside in his area of the DB, which is called a user schema

! The data objects within a user schema are called DB


objects or schema objects
◦ Includes also views

8 / 90
Defining Oracle 10g DB Tables
! Tables
◦ Primary data objects in RDB
◦ Table name, column names, data type, size

! Constraints
◦ Restrictions on data values that column can store
● Whether it is a primary key, accepts NULL

9 / 90
Defining Oracle 10g DB Tables
! Oracle naming standard
◦ Rules that Oracle corporation has established for naming all
database objects
◦ Objects must be 1 to 30 characters long
◦ Contain letters, numbers, and special symbols $, _, and #
◦ Begin with character
● Not allowed: book-5, #course, $price

! CREATE TABLE SQL syntax


CREATE TABLE tablename
(columnname1 data_type,
columnname2 data_type, …)

10 / 90
Oracle 10g Data Types
! Data type
◦ Specifies kind of data that column stores
◦ Provides means for error checking
◦ Enable DBMS to use storage space more efficiently

◦ Basic types
● Character
● Number
● Date/time
● Large object

11 / 90
Character Data Types
! Stores characters and numbers not used for calculations

! VARCHAR2
◦ Variable-length character data
◦ Values in different rows can have a different number of characters
◦ Up to 4000 characters
◦ If the user, inserts a value smaller than the maximum, size, DBMS
stores only the actual character values without trailing blanks
◦ If a user inserts more characters ! error
◦ Supports ASCII only, NVARCHAR2 supports Unicode
◦ Syntax
columnname VARCHAR2(maximum_size)
◦ Example
s_last VARCHAR2(30)

12 / 90
Character Data Types
! CHAR
◦ Fixed-length character data
◦ Up to 2000 characters
◦ Data values for different rows all have the same number of characters
◦ Recommended to be used with values such as SR, JR, SO, FR, all have the
same length
◦ If more characters inserted ! error
◦ DBMS adds trailing blank spaces to fill the maximum size
◦ It uses storage space more efficiently than VARCHAR2, and DBMS
processes data retrieved from CHAR column faster
◦ Supports ASCII only, NCHAR supports Unicode
◦ Syntax
columnname CHAR[(maximum_size)] default is 1

◦ Example
s_class char(2)

13 / 90
Character Data Types
! LONG
◦ Not recommended
◦ Limitations
◦ Up to 2 GB of character data

14 / 90
Number Data Type
! Oracle 10g stores all numerical data using the NUMBER
data type.
! The NUMBER data type stores
◦ Negative
◦ Positive
◦ Fixed
◦ Floating-point
numbers between 10-130 and 10125, with precision up to 28
decimal places.
◦ Syntax
● columnname NUMBER [([precision,] [scale])]
◦ Precision is the total number of digits both to the left and to the
right of the decimal point
◦ Scale specifies the number of digits on the right side of the
decimal point
15 / 90
Number Data Types
! Integer numbers
◦ Whole number with no digits on the right side of the
decimal point
◦ Syntax (same as NUMBER but omit the scale value
● columnname NUMBER(precision)
◦ Use integers to declare surrogate keys
◦ f_id NUMBER(5)

◦ If you enter a value < specified precision ! DBMS does not display or
store leading zeros
◦ If you enter a value > specified precision ! error
◦ If you enter digits on the right side of the decimal point, the value
rounded to the nearest whole number

16 / 90
Number Data Types
! Fixed-point number
◦ Contains specific number of decimal places
◦ Column declaration specifies both precision and scale
◦ Example
● price NUMBER(5, 2)
● The number ranges between 0 and 999.99

◦ If you enter a value > specified precision ! error


◦ If you enter more digits on the right side of the decimal
point more than specified by the ‘scale’, the value rounded
to the specified scale

17 / 90
Number Data Types
! Floating-point number
◦ Contains a variable number of decimal places
◦ The decimal point can appear anywhere, or can be omitted

◦ Examples: 2.7045, 3.25, 4.0

◦ Syntax
columnname NUMBER
◦ Example
s_gpa NUMBER

18 / 90
Date and Time Data Types
! Datetime data subtypes
◦ Store actual date and time values
◦ DATE
◦ TIMESTAMP

! Interval data subtypes


◦ Store elapsed time interval between two datetime values
◦ INTERVAL YEAR TO MONTH
◦ INTERVAL DAY TO SECOND

19 / 90
Date and Time Data Types
! DATE
◦ Stores dates from 31 Dec 4712BC - 31 Dec 4712AD
◦ Default date format
● DD-MON-YY
◦ Default time format
● HH:MI:SS AM
● If time is not entered ! default time:12:00:00 AM
◦ Syntax:
columnname DATE
◦ Example:
s_dob DATE

20 / 90
Date and Time Data Types
! TIMESTAMP
◦ Stores date values similar to DATE data type, except it
also stores fractional seconds in addition to the century,
year, month, day, hour, minute, and second
◦ Syntax
columnname TIMESTAMP (fractional_seconds_precision)
◦ Example
sl_date_received TIMESTAMP(2)
◦ Example
● 04-SEP-06 09.26.01.123975
DD-Mon-YY HH.MM.SS.FFFFFF
● If you omit the fractional_seconds_precision specification, the default
value is six decimal places.

21 / 90
Date and Time Data Types
! INTERVAL YEAR TO MONTH
◦ Stores time interval expressed in years and months

◦ Syntax
+|– elapsed_years - elapsed_months
● Ex: +02-11 : specifies a positive time interval of 2 years
and 11 months.

◦ Adding + to a known date means the result is a date after


the given date.
◦ Adding – to a known date means the result is a date before
the given date.

22 / 90
Date and Time Data Types
◦ You use the following command to declare an
INTERVAL YEAR TO MONTH data column:
● INTERVAL YEAR [(year_precsion)] TO MONTH
● Ex: time_enrolled INTERVAL YEAR TO MONTH

● Year_precsion is the maximum allowable digits for the


year
● If omitted, the default value is 6, which enables the column
to display a maximum interval of 999,999 years.

23 / 90
Date and Time Data Types
! INTERVAL DAY TO SECOND
◦ Stores time interval expressed in days, hours, minutes, and
seconds
◦ Syntax
● +|– elapsed_days elapsed_hours:elapsed_minutes:elapsed_seconds

● Ex: -04 03:20:32.00

◦ Declaration
● Columnname INTERVAL DAY [( leading_precision)]
TO SECOND{(fractional_seconds_precision)]
● The default values for these expressions are 2 and 6 respectively.
● Ex: time_enrolled INTERVAL DAY TO SECOND

24 / 90
Large Object (LOB) Data Types
! Stores binary data such as:
◦ Digitized sounds or images
◦ References to binary files from word processor or
spreadsheet

! Syntax
◦ columnname Lob_data_type
◦ You don’t specify the size, the DB automatically allocate the
correct size

25 / 90
Large Object (LOB) Data Types

◦ Ex: f_image BLOB


f_image BFILE

26 / 90
Constraints
! Rules that restrict data values that can be entered into
column

! Types of constraints:
◦ Integrity constraints: define primary and foreign keys

◦ Value constraints: define specific data values or data ranges that


must be inserted into columns and whether values must be
unique or not NULL

27 / 90
Levels of Constraints
! Table constraint: restricts the data value with respect
to all other values in the table.
◦ Example: primary key constraint which must be unique and
can’t appear in this column in more than one table row.

! Column constraint: limits value that can be placed in


specific column, irrespective of values that exist in
other rows.
◦ Example of column constraints are value constraints, which
specify that a certain value or set of values must be used, and
NOT NULL constraints which specify that a value cannot
be NULL.
◦ Example: GENDER value is either M or F

28 / 90
Levels of Constraints
! Constraint definitions should be placed either:
◦ At end of CREATE TABLE command after table columns
declared
◦ Within each column definition

29 / 90
Levels of Constraints
! Every constraint in a user schema must have a unique
constraint name that must adhere to the Oracle
naming standards:

◦ You explicitly define the constraint name, or


◦ Omit the constraint name ! allow Oracle to assign the
constraint a system-generated name:

● Uses prefix SYS_Cn where n is number that makes the number


unique

30 / 90
Levels of Constraints
◦ This type of naming strategy makes it difficult to determine,
solely by its name, with which table the constraint is
associated.

◦ Therefore, it is good practice to specify constraint names


explicitly using descriptive names ! easily identify the
constraints that exists in the user schema:
● A convention for assigning unique and descriptive constraint
names is called the constraint naming convention.

31 / 90
Levels of Constraints
! The constraint naming convention uses the following
syntax:
tablename_columnname_constraintID
ConstraintID is a two character abbreviation that describes the
constraint.

● But this causes the name to be longer than 30 characters which is


the maximum, hence you must abbreviate either the table name or
the column name.

32 / 90
Integrity Constraints
! Primary keys constraint
◦ Defines primary and foreign keys and their corresponding
table and column references.

◦ Syntax (within a column declaration)


● CONSTRAINT constraint_name PRIMARY KEY

◦ Syntax (at end of table definition)


● CONSTRAINT constraint_name PRIMARY KEY
(columnname)

33 / 90
Integrity Constraints
◦ Example (Constraint definition is independent of the column declaration):
CREATE TABLE location
(loc_id NUMBER(6),
bldg_code VARCHAR2(12),
room VARCHAR2(6),
capacity NUMBER(5),
CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id));

◦ Example (Constraint definition is created within the column definition):


CREATE TABLE location
(loc_id NUMBER(6)
CONSTRAINT location_loc_id_pk PRIMARY KEY,
bldg_code VARCHAR2(12),
room VARCHAR2(6),
capacity NUMBER(5));

34 / 90
Integrity Constraints
! Foreign keys constraint
◦ Column constraint that specifies that the value a user inserted in a column
must exist as a primary key in a referenced table.

◦ Syntax (within a column declaration)


CONSTRAINT constraint_name
REFERENCES primary_key_tablename (primary key_columnname)

◦ Syntax (at end of column definition)


CONSTRAINT constraint_name
FOREIGN KEY (columnname)
REFERENCES primary_key_tablename (primary key_columnname)

35 / 90
Integrity Constraints
! Example (independently of the column declaration)
CONSTRAINT faculty_loc_id_fk FOREIGN KEY (loc_id)
REFERENCES location (loc_id)

! Example (within the column declaration)


loc_id NUMBER(6) CONSTRAINT faculty_loc_id_fk
REFERENCES location (loc_id)

36 / 90
Integrity Constraints
! Composite keys constraint
◦ A primary key composed of two or more data columns, defined by
listing all of the columns that make up the composite primary key,
with each column name separated by a comma.

◦ Syntax
CONSTRAINT constraint_name
PRIMARY KEY (columnname1, columnname2, …)

◦ You should place the composite key definition after the commands
in the CREATE TABLE command that define the columns that
make up the key

37 / 90
Integrity Constraints
! Example

CREATE TABLE enrollment


(s_id NUMBER(5) CONSTRAINT enrollment_s_id_fk
REFERENCES student(s_id),
c_sec_id NUMBER(8) CONSTRAINT enrollment_c_sec_id_fk
REFERENCES course_section(c_sec_id),
CONSTRAINT enrollment_s_id_c_sec_id_pk
PRIMARY KEY (s_id, c_sec_id));

38 / 90
Integrity Constraints
! Value Constraints: column level constraints that
restrict the data values that users can enter into a
given column.

! Types:
1. CHECK
2. NOT NULL
3. DEFAULT
4. UNIQUE

39 / 90
1. CHECK Conditions
! Specify that a column value must be:
◦ a specific value, such as (M, F)
◦ Fall within a specific range ( 0 < x < 1000 )

! You should create check condition constraints only when


the number of allowable values is limited and not likely to
change.

! You must be prudent when specifying check conditions in


table definitions, because once the table is populated with
data, it is difficult or impossible to modify the constraint –
all rows must satisfy the constraint.

40 / 90
1. CHECK Conditions
! The DBMS must be able to evaluate each expression
in a check condition as either true or false. You can
combine expressions using the logical operators
AND and OR.

◦ X AND Y both must be true


◦ X OR Y only one must be true

41 / 90
1. CHECK Conditions
! Ex:
CONSTRAINT student_s_class_cc CHECK
((s_class = ‘FR’) OR (s_class = ‘SO’)
OR (s_class = ‘JR’) OR (s_class = ‘SR’))

! Ex:
CONSTRAINT course_credits_cc
CHECK((credits > 0) AND (credits < 12))

42 / 90
2. NOT NULL Constraints
! Specifies whether the user must enter a value for a
specific row, or whether the value can be NULL
(absent or unknown).

! Primary key column automatically have a NOT


NULL constraint.

! Ex:
s_last VARCHAR2(30) CONSTRAINT student_last_nn NOT NULL

43 / 90
2. NOT NULL Constraints
! NOT NULL can also be set in the CREATE
statement:
CREATE TABLE employee
(fname VARCHAR2(8),
ssn VARCHAR2(9) NOT NULL
);

Desc employee

Name Null? Type


----------------------------------------- -------- ----------------------------
FNAME VARCHAR2(8)
SSN NOT NULL VARCHAR2(9)

44 / 90
3. DEFAULT Constraints
! Specifies that a particular column has a default value
that the DBMS automatically inserts for every table
row.

! It must be created in the column declaration (not as a


separate command beginning with CONSTRAINT).

! Ex:
s_state CHAR(2) DEFAULT ‘FL’

The DBMS will insert the default value only if the user inserts a NULL value into
the s_state column.

45 / 90
4. UNIQUE Constraints
! specifies that a column must have a unique value for
every table row.

! Same as a primary key constraint except NULL


values are allowed in the column.

! Ex:
CONSTRAINT term_term_desc_uk UNIQUE (term_desc)

Each term has a unique description. Column name


Or
abc char(5) UNIQUE, …

46 / 90
Creating DB tables using SQL*Plus
! Start the application: SQL*Plus
! Log onto the Oracle 10g DB: username and password
! Type SQL commands at SQL prompt
! End each command with semicolon ;
! Press Enter to submit commands

47 / 90
Creating DB tables using SQL*Plus
! The SQL*Plus interpreter checks the command for
syntax errors
◦ If the command is error free ! the SQL interpreter
submits the command to the DB.

! SQL commands are not case sensitive

! SQL interpreter ignores


◦ spaces between characters
◦ line breaks

48 / 90
Creating DB tables using SQL*Plus
! When creating DB tables that contain foreign key
references to other tables, you must first create the
table in which the foreign key is a primary key.
◦ Ex: to create the STUDENT table, you must first create the
FACULTY table, as the F_ID column in the STUDENT
table is a foreign key that reference the F_ID column in the
FACULTY table.

◦ The LOCATION table has no foreign key references, so you


can create the LOCATION table before you create any
other tables.

49 / 90
SQL Command to Create the
LOCATION Table

50 / 90
Debugging SQL Commands
! Syntax error ! SQL*Plus interpreter displays
errors information that includes:
◦ Line number within the command that caused the error
◦ Position of the error within the line (Asterisk *)
◦ Causes of SQL command errors are not always readily
apparent

51 / 90
Debugging SQL Commands
◦ Error code and description of the error
● Has 3-char prefix (e.g ORA: indicates the error was generated by
the DBMS) which indicates which Oracle utility generates the
error.
● 5-digit code (00907 indicated missing closing parenthesis)

The error message does not adequately


explain the error cause, nor does it
provide ideas for locating or correcting
the error.
For further information: Oracle
Technology Network (OTN).

52 / 90
Debugging SQL Commands

53 / 90
Debugging SQL Commands
! A last resort debugging technique is to create the
table multiple times, each time adding a column
declaration repeating the process until you find the
declaration causing the error.

54 / 90
Table Deleting
! DROP TABLE tablename

55 / 90
Exiting SQL*PLUS
1. Type EXIT
2. File ! Exit
3. Close button

! Don’t simply shut down the PC.

! When exiting SQL*PLUS, your DB connection


disconnects automatically.

56 / 90
Creating a table with Foreign Key
Constraint

57 / 90
Viewing Information about Tables
! DESCRIBE tablename

! DESCRIBE is SQL*PLUS and not standard SQL


command. So it does not require semicolon

! SQL*PLUS commands can be abbreviated, e.g. DESC


instead of DESCRIBE.

58 / 90
Viewing Information about Tables

Note that DESCRIBE does not list


the foreign key constraint on the
LOC_ID column in the FACULTY
table.

59 / 90
Viewing Information about Tables
! To view information about constraint other than the
NOT NULL constraint use Oracle 10g data
dictionary views.

! Oracle 10g data dictionary consists of tables that


contain information about the structure of the DB.

! When the DBA creates a new DB, the system creates


the data dictionary in a user schema named SYS.

60 / 90
Viewing Information about Tables
! The Oracle 10g DBMS automatically updates the data
dictionary tables as users create, update and delete DB
objects.

! In fact, users and DBAs do not directly view, update, or


delete values in the data dictionary tables. Rather, users
interact with the data dictionary using the data dictionary
views.

! A view is a DB object that the DBMS bases on an actual DB


table and which enables the DBMS to present the table data
in a different format based on the needs of users.

61 / 90
Viewing Information about Tables
! A view can serve to hide some table columns in
which the user has no interest or doesn’t have
required privileges to view.

62 / 90
Viewing Information about Tables
! Categories of data dictionary views:

◦ USER: shows the objects in the current user’s schema

◦ ALL: shows both the objects in the current user’s schema


and the objects that the user has privileges to manipulate.
● Example, another user might create a table, and then give you
the privilege to update the table.

◦ DBA: allows users who are DBAs to view information about


all DB objects.

63 / 90
Viewing Information about Tables
! General command to retrieve information from a
data dictionary view:
! SELECT view_columnname1, view_columnname2, …
FROM prefix_object;

◦ Prefix specifies the view category, and can have the value
USER, ALL, or DBA.

◦ Object is the type of DB object you are examining, such as


TABLES or CONSTRAINTS.

64 / 90
Viewing Information about Tables
! Example: the following command retrieves the names
of all of a user’s DB tables by retrieving the
TABLE_NAME column from the USER_TABLE
view:

! SELECT table_name
FROM user_tables;

65 / 90
Viewing Information about Tables
! Similarly, the following command uses the ALL prefix
and retrieve the names of all DB tables that a user
has either created or has been given object privileges
to manipulate:

! SELECT table_name
FROM all_tables;

66 / 90
Viewing Information about Tables

67 / 90
Viewing Information about Tables
! You can retrieve information about a variety of DB
objects using different data dictionary views.
Example: to retrieve the names of all of your DB
objects is:
SELECT object_name
FROM user_objects;

68 / 90
Viewing Information about Tables
! Use DESCRIBE to list the names of the columns in a
specific DB view.

69 / 90
Viewing Information about Tables
! Then you query the USER_CONSTRAINTS view to
determine the constraints that exists in your DB
tables.

70 / 90
To List the FACULTY table constraints:

! To retrieve a list of all the constraints for a specific


table in your user scheme:

Must be in capital letters because


the data dictionary stores all objects
information in uppercase. And
because it is a string enclose it
between ‘ ‘

71 / 90
Modifying and Deleting DB Tables
! Plan tables carefully to avoid having to change structure of
database tables later, which may lead to errors in the
retrieving applications.

! Oracle 10g provides techniques for modifying tables.

◦ Changing table name


◦ Adding new columns
◦ Deleting columns
◦ Changing data type
◦ Changing maximum size of an existing column

72 / 90
Modifying and Deleting DB Tables
! Unrestricted action
◦ Some specifications of tables can always be modified

! Restricted action
◦ Table specifications that can be modified only in certain
situations

73 / 90
Unrestricted Actions when Modifying
Database Tables

74 / 90
Restricted Actions when Modifying
Database Tables

75 / 90
Deleting Tables
! Delete table with caution, because there is always a
chance that some DB application is still using it.

! Dropping a table will delete the table structure from


the data dictionary.

! Syntax
◦ DROP TABLE tablename;

76 / 90
Deleting Tables
! To drop a table that contains columns that other tables reference as
foreign keys, you have two options:
(Ex: you can’t drop LOCATION because the FACULTY table reference the
LOC_ID column as a foreign key)

1. Drop all the tables that contain the foreign key references.
● Ex: to delete the LOCATION table, you could first delete the FACULTY table, and
then you could delete the LOCATION table.

2. Delete all of the foreign key constraints that reference the table. To delete the
foreign key constraints that reference a table use:

DROP TABLE tablename


CASCADE CONSTRAINTS;

77 / 90
Deleting Tables
! Trying to drop without cascade will lead to an error:

78 / 90
Renaming Tables
! Syntax
◦ RENAME old_tablename TO new_tablename;

! Ex:
◦ RENAME faculty TO nw_faculty;

! DBMS automatically transfers to the new table


◦ integrity constraints
◦ indexes
◦ privileges
that referenced the old table.

79 / 90
Renaming Tables
! When renaming a table, objects that referenced the
old table, such as views, and stored procedures and
functions become invalid.

80 / 90
Adding Columns to Existing Tables
! Add new column to table
ALTER TABLE tablename
ADD(columnname data_declaration constraints);

Ex:
ALTER TABLE faculty
ADD (start_date DATE);

81 / 90
Modifying Existing Column Data
Definitions
! You can modify the data type of a column only if
existing data values in the column rows are
compatible with the new data type.

! Hence, you can only change a column’s data type to


another data type that stores the same kind of data.

◦ Ex: VARCHAR2 "! CHAR


◦ DATE "! TIMESTAMP

◦ But you can’t change VARCHAR2 to NUMBER or DATE

82 / 90
Modifying Existing Column Data
Definitions
! To change the maximum size parameter of a column:

◦ Making it larger is an unrestricted action


◦ Making it smaller is a restricted action
● Only if the table does not contain any data
● If it contains data then the values for the column being changed
must be NULL

83 / 90
Modifying Existing Column Data
Definitions
! Syntax
ALTER TABLE tablename
MODIFY(columnname new_data_declaration);

! EX:
ALTER TABLE faculty
MODIFY (f_rank CHAR(4));

If was VARCHAR2 with


size 8

84 / 90
Deleting a Column
! Syntax
ALTER TABLE tablename
DROP COLUMN columnname;

85 / 90
Renaming a Column
! Syntax
ALTER TABLE tablename
RENAME COLUMN old_columnname TO
new_columnname;

! Ex:
! ALTER TABLE faculty
RENAME COLUMN f_rank TO faculty_rank;

86 / 90
Adding and Deleting Constraints
! Adding new constraints is a restricted action.

! Difficult to add new constraints to tables that already


contains data, because usually data values do not
conform to the new constraint.

! Syntax
ALTER TABLE tablename
ADD CONSTRAINT constraint_name
constraint_definition;
! Ex:
ALTER TABLE faculty
ADD CONSTRAINT faculty_f_pin_uk UNIQUE(f_pin);

87 / 90
Adding and Deleting Constraints
! Deleting constraints is an unrestricted action

! Syntax
ALTER TABLE tablename
DROP CONSTRAINT constraint_name;

! Ex:
ALTER TABLE faculty
DROP CONSTRAINT faculty_f_pin_uk;

88 / 90
Enabling and Disabling Constraints
! Useful during development, you may need to disable
it and then enable it when the project is completed.

! When you create a new constrain using CREATE


TABLE or ALTER TABLE, the constraint is enabled
automatically.

! When a constraint is enabled, the DBMS enforces


the constraint when users attempt to add new data to
the DB.

89 / 90
Enabling and Disabling Constraints
! To disable existing constraint syntax:
ALTER TABLE tablename
DISABLE CONSTRAINT constraint_name;

! Ex:
ALTER TABLE faculty
DISABLE CONSTRAINT faculty_loc_id_fk;

! To enable existing constraint syntax


ALTER TABLE tablename
ENABLE CONSTRAINT constraint_name;

! Ex:
ALTER TABLE faculty
ENABLE CONSTRAINT faculty_loc_id_fk;

90 / 90

You might also like