Professional Documents
Culture Documents
History of SQL
Features of SQL
SQL Commands
DDL commands
Create
Alter
Drop
Truncate
Column names
Column sizes
Data type
Specifies kind of data that column stores
Number
Date/time
Large object
Basic Built-in Data Types
12
Character
VARCHAR2 / NVARCHAR2
CHAR / NCHAR
Numeric
NUMBER
DATE
OTHERS
CLOB, BLOB, LONG, RAW, LONG RAW
Constraints
13
A rule that restricts the values that can be inserted into a column
A mechanism used to protect
the relationship between data within an Oracle table, or
the correspondence between data in two different tables
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
Types of Constraints
14
Syntax:
CREATE TABLE <tablename>(column name
datatype DEFAULT <value>);
Example:
CREATE TABLE employee
(empno NUMBER(6) PRIMARY KEY,
ename VARCHAR2(30) NOT NULL,
grade CHAR(3) DEFAULT TG0,
dob DATE, deptno number(2) );
Primary Key Constraints
18
Syntax:
CREATE TABLE <table name>
(<Colname> <datatype>[(<size>)] [ [CONSTRAINT
constraint_name] PRIMARY KEY ],
<colname> <datatype>[(<size>)],
. . .);
Primary Key Constraints
19
Column Level Primary Key
20
tablename_ columnname_constraintID
Foreign Key Constraints
23
Syntax:
Column level
CREATE TABLE <tablename>(Column name datatype CONSTRAINT
constraint_name
REFERENCES primary_key_table_name (column_name));
Table level
CREATE TABLE table_name
(column1 datatype null/not null, column2
datatype null/not null, ,CONSTRAINT
fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ...
column_n)
);
Foreign Key Constraints
24
A foreign key value must match an existing value in the parent table or
be NULL.
Foreign keys are based on data values and are purely logical, not
physical, pointers.
Foreign Key Constraints
25
Foreign Key Constraints - Column Level
26
Syntax:
CREATE TABLE <tablename>(column name datatype NOT
NULL);
Example:
CREATE TABLE employee
(empno NUMBER(6) PRIMARY KEY,
ename VARCHAR2(30) NOT NULL,
grade CHAR(2),
dob DATE, deptno number(2) REFERENCES
dept(deptno),CHECK (empno > 2999));
Types of Value Constraints
31
Unique: specifies that a non-primary key column must have a unique value
Can contain a NULL Value
Duplicate is not allowed.
Syntax:
CREATE TABLE <table name>(column name datatype
CONSTRAINT term_term_desc_uk UNIQUE (term_desc));
Example:
CREATE TABLE employee
(empno NUMBER(6) PRIMARY KEY,
ename VARCHAR2(30) NOT NULL,
grade CHAR(2) DEFAULT TG0,
email varchar2(20) constraint em_email UNIQUE,
dob DATE, deptno number(2) REFERENCES
dept(deptno), CHECK (empno > 2999));
Create Table from Another Table
32
Syntax:
CREATE TABLE <tablename>
AS ( Select query);
Example:
CREATE TABLE empcopy
AS (SELECT *
FROM emp
WHERE empno > 1010);
Modifying and Deleting Database Tables
33
To add a column
Syntax
ALTER TABLE tablename ADD(<columnname>
data_declaration [constraints], <columnname>
data_declaration [constraints]);
Example:
ALTER TABLE emp ADD (Hire_date DATE);
New column would be initially NULL for all the rows.
Modifying Existing Column Data Definitions
36
To delete:
Drop table <tablename>; => DROP TABLE emp;
All data and structure in the table is deleted.
Any pending transactions are committed.
All indexes are dropped.
You cannot roll back the DROP TABLE statement.
Examples
ALTER TABLE EMP
ADD CONSTRAINT emp_dept_id_fk
FOREIGN KEY(dept_id)
REFERENCES dept(dept_id);
To disable a constraint:
ALTER TABLE tablename DISABLE CONSTRAINT constraint_name;
To enable a constraint:
ALTER TABLE tablename ENABLE CONSTRAINT constraint_name;
Example
Change the increment value, maximum value, minimum value, cycle option, or
cache option.
History of SQL
Features of SQL
SQL Commands
DDL commands
Create Sequence
Thank You