DDL DML New
DDL DML New
DROP
REVOKE
GRANT
DDL commands
DESCRIBE tab_1
C1 NUMBER(38)
C2 TIMESTAMP(6)
What Are Constraints?
Column,…
[Constraint constraint_name] Constraint_type(column,…),
Not null Constraint
Ensures that null values are not permitted for the column
Table created.
1 row created.
Unique
SQL >insert into violation
uni_tab values(100,'wipro')
SQL> /
insert into
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C005035) violated
The Unique key constraint
Allows Null
SQL> insert into
uni_tab(c2) values(‘tcs');
1 row created.
SQL> select *
from uni_tab;
C1 C2
100hp
nulls tcs
cts
Primary Key
• What is a primary key?
– A primary key is a single field or combination
of fields that uniquely defines a record.
– A table can have only one primary key.
Table created.
1 row created.
Unique
SQL >insert into violation
pri_tab values(500,‘12-feb-2010')
SQL> /
insert into
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C005036) violated
The Primary key constraint
Does not allow NULLS
Table created.
SQL> select * from pri_tab;
C1 C2
500 16-Oct-16
C1 C2
500 16-Oct-16
SQL>select *
from for_tab;
C1 C5
500m
null f
m
Check Constraints
Syntax
[constraint <name>] check(<condition>)
Table created.
I1 I2 C1 C2
1 14-JUL-12 08.06.16.870000 AM 500 14-JUL-12
13-JUL-12 08.06.16.870000 AM
x x
500 12-JUL-12
15-JUN-12
F x
UNI_TAB
UNIQUE FOR_TAB
C1 C5 Age>=18 VOTE
C1 C2
500 m
100 HP AGE NAME
100 WIPRO x 501 25 RAMU
x f x 17 RAHUL
TCS
m
CTS
CONSTRAINTS
UNIQUE Age>=18
ATM Not null RTO VOTE
x
KA04V9455 CAR x 17 RAHUL
x 6789
TT
QUALIS
F
P ACCOUNTS
TRANS
ACCNO WD DEP
ACCNO NAME BALANCE
12345 NULL 10000
12345 RAHUL 5000
12345 NULL 10000
x 12345 SAYEN 10000
54321 NULL 5000
x SAYEN 5000 x
CONSTRAINT INFORMATION
select a.column_name,a.constraint_name,b.table_name,
b.constraint_type
from user_CONS_COLUMNS a,user_CONSTRAINTS b
where a.constraint_name=b.constraint_name
and a.table_name='&TABLE_NAME'
/
COLUMN LEVEL
TABLE LEVEL
34
CONSTRAINT LEVEL
create table Col_Tab(
cust_id int primary key,
cust_name varchar2(10),
address varchar2(50))
Modify clause
– Add not null constraint
– Change column data type
– Modify columnsize(width)
Drop clause
– Drop Columns
– Drop Constraints
RENAME clause
– Rename Columns
– Rename Constraints
ENABLE/DISABLE clause
– Enable Constraints
– Disable Constraints
TABLE
– Read Only --11G
– Read write --11G
SAMPLE TABLE
Create table Test1(
c1 int ,
c2 date,
c3 char(1),
c4 varchar2(10))
desc test1 Name Null? Type
C1 NUMBER(38)
C2 DATE
C3 CHAR(1)
38 C4 VARCHAR2(10)
Adding a Column
• Use the ADD clause to add columns.
New
colu
mn
SYNTAX
desc test1
Name Null? Type
C1 NUMBER(38)
C2 DATE
C3 CHAR(1)
C4 VARCHAR2(10)
C5 TIMESTAMP(6) WITH TIMEZONE
Dropping a Column
Use the DROP COLUMN clause to drop
columns you no longer need from the
table.
desc test1
Name Null? Type
C1 NUMBER(38)
C2 DATE
C3 CHAR(1)
C4 VARCHAR2(10)
DROP COLUMN
SQL> CREATE TABLE TES_EMP AS SELECT * FROM EMP;
Table created.
select UC.column_name,UC.constraint_name,U.constraint_type
from user_constraints U join user_cons_columns UC
on u.constraint_name=uc.constraint_name
and U.table_name='TEST1'
C1 PRK_TEST1 P
Dropping a Constraint
SYNTAX
SYNTAX
Alter table e2
enable constraint
pk_pe;
Disable a Constraint
SYNTAX
Alter table e2
disable constraint
pk_pe;
CONSTRAINT STATUS
SELECT constraint_name,status
From user_CONSTRAINTS
Where table_name='&TABLE_NAME'
/
TABLE_NAME READ_ONLY
------------------------------ -----------------
DEPT NO
BONUS NO
SALGRADE NO
EMP_1 NO
EMP YES
SQL> ALTER TABLE EMP READ WRITE;
WITHOUT CASCADE
EMP
DEPT EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
FK_DEPTNO 7876
7900
ADAMS
JAMES
CLERK
CLERK
7788
7698
20-FEB-81
20-FEB-81
1100
950
20
30
DELETE *
FROM DEPT ERROR at line 1:
ORA-02292: integrity constraint
(SCOTT.FK_DEPTNO) violated - child
record found
WITH CASCADE
ALTER TABLE emp
ADD CONSTRAINT
FK_DEPTNO REFERENCES DEPT
ON DELETE CASCADE
EMP
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
DEPT
7369 SMITH CLERK 7902 17-DEC-80 800 20
DEPTNO DNAME LOC 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
10 ACCOUNTIG NEWYORK
7521 WARD SALESMAN 7698 20-FEB-81 1250 500 30
20 RESEARCH DALLAS 7566 JONES MANAGER 7839 20-FEB-81 2975 20
30 SALES CHICKAGO 7654 MARTIN SALESMAN 7698 20-FEB-81 1250 1400 30
STATEMENT DESCRIPTION
CREATE TABLE Creates a table
STATEMENT DESCRIPTION
INSERT Adds a new row to the table
INSERT INTO
DEPT1 (DEPTNO,DNAME,LOC)
VALUES (50,’HR’,’MUMBAI’)
INSERT INTO
DEPT1 VALUES (50,’HR’,’MUMBAI’)
DEFAULT ORDER
INSERT Statement
DESCRIBE dept1
INSERT INTO
DEPT1 (DNAME,DEPTNO,LOC)
VALUES (‘HR’,50,’MUMBAI’)
CHANGED ORDER
INSERT Statement
DESCRIBE dept1
INSERT INTO
DEPT1 VALUES
(&DEPTNO,’&DNAME’,’&LOC’)
SUBSTITUTION VARIABLES
INSERT Statement
DESCRIBE dept1
INSERT INTO
DEPT1 SELECT DEPTNO,DNAME,LOC
FROM DEPT WHERE DEPTNO=30
INSERT INTO
DEPT1 ( DEPTNO,DNAME,LOC)
VALUES (50,NULL,NULL)
INSERT INTO
DEPT1 ( DEPTNO)
VALUES (50)
INSERTING NULLS
INSERT Statement
create table defa_Tab(c1 int primary key,
c2 timestamp default current_timestamp);
DESCRIBE Defa_tab
Name Null? Type
C1 NOT NULL NUMBER(38)
C2 TIMESTAMP(6)
C1 C2
10018-OCT-16 08.43.20.596000 AM
100018-OCT-16 08.43.32.004000 AM
INSERTING DEFAULT
UPDATE Statement
• Modify existing rows with the UPDATE
statement.
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
UPDATE emp
SET sal=1.1*sal
WHERE empno=7566;
1 row updated.
ENAME SAL
SMITH 880
UPDATE Statement
UPDATE emp
SET sal=1.1*sal;
14 rows updated.
DELETE Statement
14 rows deleted.
MERGE
• DML Operation
• Introduced in Oracle 9i
• UPSERT
MERGE
SQL> INSERT INTO
2 S1 VALUES
SQL> CREATE TABLE 3 (555,'SACHIN');
S1(SL INT PRIMARY KEY, 1 row created.
NAME VARCHAR2(20));
SQL> INSERT INTO
2 S1 VALUES
Table created. 3 (666,'VEERU');
1 row created.
SQL> CREATE TABLE SQL> INSERT INTO
S2(SL INT PRIMARY KEY, 2 S2 VALUES
3 (666,'VIRAT');
NAME VARCHAR2(20)); 1 row created.
SQL> COMMIT;
Commit complete.
MERGE
BEFORE MERGE AFTER MERGE
Id number 4
Name Varchar2
A. Populate the table with following data 25
Id Name
1001 Prithivi
1002 Agni
1003 Tejas
a. Add primary key constraint to the id column
1004
b. Add Trishulwith datatype varchar2 and size 15
a column called location
c. Update Location with ‘bangalore’,’chennai’,’hyderabad’,’delhi’ respectively for the rows
1,2,3,4.
d. Rename the column location as Place
e. Rename the table dept_tab as itpl_tab
f. Drop constraint primary key
g. Drop the table ITPL_tab
h. Flashback the dropped table itpl_tab.
PRACTICE SESSIONS-2
1. Create the IT_tab table based on the following table instance chart.
Column name Datatype size
Id number 2
Name Varchar2
A. Populate the table with following data 25
Id Name
Null TCS
5001 HP
B. Change
5001the datasize to fit the
INFIabove ID values
C. Try to Add a constraint primary key for the column id. Give your observations.
D. Try to Add a Unique constraint for the column id. Give your observations
E. Delete the row where name is INFI. Try to implement primary/unique constraint and give
your observations.
PRACTICE SESSIONS-3
1. Create a table called metrocities. Add appropriate columns
and datatypes with a check constraint on the city name
which should take only metro cities of India.
4. Display the data in the format SMITH’s job is CLERK and SMITH’S
NEWSAL 900
1.SQL * PLUS
2.ISQL * PLUS => obsolete
3.SQLPLUS WORK SHEET
4.TOAD
5.SQL DEVELOPER
6.ENTERPRISE MANAGER