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

SQL Commands

The document discusses various data definition language (DDL) and data manipulation language (DML) commands in Oracle such as CREATE, ALTER, TRUNCATE, DROP, INSERT. It provides the syntax and examples of using these commands to create and modify database tables, insert data into tables, and delete data and tables. Key points covered include creating a table with the CREATE statement, adding and dropping columns from a table using ALTER, deleting all table data with TRUNCATE, permanently deleting a table with DROP, and inserting new rows into a table with INSERT.

Uploaded by

mallesh yadav
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

SQL Commands

The document discusses various data definition language (DDL) and data manipulation language (DML) commands in Oracle such as CREATE, ALTER, TRUNCATE, DROP, INSERT. It provides the syntax and examples of using these commands to create and modify database tables, insert data into tables, and delete data and tables. Key points covered include creating a table with the CREATE statement, adding and dropping columns from a table using ALTER, deleting all table data with TRUNCATE, permanently deleting a table with DROP, and inserting new rows into a table with INSERT.

Uploaded by

mallesh yadav
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 26

DDL Data definition language

------------------------------
create
--------
used to create db object(table)
--------------------------------
syntax
---------
create table table_name
(
col_nm data_type(s),
.
.
.
);

Db connection process
--------------------------
SQL> conn system/system
Connected.
SQL> show user
USER is "SYSTEM"
SQL> create user ganesh identified by ganesh;

User created.

SQL> conn ganesh/ganesh


ERROR:
ORA-01045: user GANESH lacks CREATE SESSION privilege; logon denied

Warning: You are no longer connected to ORACLE.


SQL> show user
USER is ""
SQL> conn system/system
Connected.
SQL> grant resource,connect to ganesh;

Grant succeeded.

SQL> conn ganesh/ganesh


Connected.

alter
---------
(i) to add one or more columns
-----------------------------------
syntax
------
alter table table_name add column_name data_type(size);

example
-----------
SQL> desc student1
Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(10)
SNAME VARCHAR2(20)
SDOB DATE
SFEE NUMBER(6,2)
SGRADE CHAR(1)

SQL> alter table student1 add (saddress varchar2(20), smobile number(10));

Table altered.

SQL> desc student1


Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(10)
SNAME VARCHAR2(20)
SDOB DATE
SFEE NUMBER(6,2)
SGRADE CHAR(1)
SADDRESS VARCHAR2(20)
SMOBILE NUMBER(10)

(ii) to delete one or more than one column


----------------------------------------------
syntax
---------
alter table table_name drop column column_name;

example
--------

SQL> alter table student1 drop column sgrade;

Table altered.

SQL> desc student1


Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(10)
SNAME VARCHAR2(20)
SDOB DATE
SFEE NUMBER(6,2)
SADDRESS VARCHAR2(20)
SMOBILE NUMBER(10)

SQL> alter table student1 drop (sdob,sfee);

Table altered.

SQL> desc student1


Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(10)
SNAME VARCHAR2(20)
SADDRESS VARCHAR2(20)
SMOBILE NUMBER(10)

(iii) to rename a column


---------------------------
alter table table_name rename column old_col_name to new_col_name;
example
----------
SQL> alter table student1 rename column sid to student_id;

Table altered.

SQL> desc student1


Name Null? Type
----------------------------------------- -------- ----------------------------
STUDENT_ID NUMBER(10)
SNAME VARCHAR2(20)
SADDRESS VARCHAR2(20)
SMOBILE NUMBER(10)

(iv) to rename a table


--------------------------
syntax
------
alter table old_table_name rename to new_tabl_nm;

example
----------
SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
STUDENT TABLE
STUDENT1 TABLE
STUDENT3 TABLE
STUDENT4 TABLE
STUDENT5 TABLE
STUDENT6 TABLE

8 rows selected.

SQL> alter table student1 rename to student1_details;

Table altered.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
STUDENT TABLE
STUDENT1_DETAILS TABLE
STUDENT3 TABLE
STUDENT4 TABLE
STUDENT5 TABLE
STUDENT6 TABLE

8 rows selected.

(v) to modify type/size of a column


----------------------------------------
example
-----------

SQL> select * from student


2 ;

SID SNAME SFEE


---------- -------------------- ----------
1 aj 1234.56
3 raj kumar 2233.56
4 ajay 4455.67
5 ajay 4455.67

SQL> alter table student modify sid varchar2(20);


alter table student modify sid varchar2(20)
*
ERROR at line 1:
ORA-01439: column to be modified must be empty to change datatype

SQL> desc student3;


Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(10)
SNAME VARCHAR2(20)
SDOB DATE
SFEE NUMBER(6,2)

SQL> select * from student3;

no rows selected

SQL> alter table student3 modify sid varchar2(20);

Table altered.

SQL> desc student3;


Name Null? Type
----------------------------------------- -------- ----------------------------
SID VARCHAR2(20)
SNAME VARCHAR2(20)
SDOB DATE
SFEE NUMBER(6,2)

truncate
-----------
used to delete all the table data perminently at a time

syntax
---------
truncate table table_name

example
-----------

SQL> select * from student;


SID SNAME SFEE
---------- -------------------- ----------
1 aj 1234.56
3 raj kumar 2233.56
4 ajay 4455.67
5 ajay 4455.67

SQL> truncate table student;

Table truncated.

SQL> select * from student;

no rows selected

drop
-------
used delete the table perminently from DB

drop table table_name;

example
---------

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
STUDENT TABLE
STUDENT1_DETAILS TABLE
STUDENT3 TABLE
STUDENT4 TABLE
STUDENT5 TABLE
STUDENT6 TABLE

8 rows selected.

SQL> drop table student1_details;

Table dropped.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
STUDENT TABLE
STUDENT3 TABLE
STUDENT4 TABLE
STUDENT5 TABLE
STUDENT6 TABLE

7 rows selected.
DML
-----(Data manipulation language)
Insert
--------
(i) inserting data using values()
------------------------------------
syntax
---------
insert into table_name values(val1,val2,...);

example
---------
SQL> insert into student values(100,'ajay',1234.56);

1 row created.

SQL> select * from student;

SID SNAME SFEE


---------- -------------------- ----------
100 ajay 1234.56

SQL> insert into student(sname,sfee,sid) values('anil',2233.45,101);

1 row created.

SQL> select * from student;

SID SNAME SFEE


---------- -------------------- ----------
100 ajay 1234.56
101 anil 2233.45

SQL> insert into student values(100,'ajay',1234.56,12345);


insert into student values(100,'ajay',1234.56,12345)
*
ERROR at line 1:
ORA-00913: too many values
SQL> insert into student values(100,'ajay');
insert into student values(100,'ajay')
ERROR at line 1:
ORA-00947: not enough values

(ii) inserting data using address method(&)


-------------------------------------------------
syntax
--------
insert into table_name values(&col_nm1, &col_nm2,...);

example
-----------
SQL> insert into student values(&sid,'&sname',&sfee);
Enter value for sid: 102
Enter value for sname: raj
Enter value for sfee: 2323.45
old 1: insert into student values(&sid,'&sname',&sfee)
new 1: insert into student values(102,'raj',2323.45)
1 row created.

SQL> /
Enter value for sid: 103
Enter value for sname: kumar
Enter value for sfee: 3434.56
old 1: insert into student values(&sid,'&sname',&sfee)
new 1: insert into student values(103,'kumar',3434.56)

1 row created.

SQL> select * from student;

SID SNAME SFEE


---------- -------------------- ----------
100 ajay 1234.56
101 anil 2233.45
102 raj 2323.45
103 kumar 3434.56

Insert all
into student values(1,'abc',1234.56)
into student values(2,'xyz',2233.45)
into student values(3,'pqr',2233.67)
into s1 values(1,'abc')
into s1 values(2,'xyz')
select * from dual;

examples
-----------
SQL> desc dual;
Name Null? Type
----------------------------------------- -------- ----------------------------
DUMMY VARCHAR2(1)

SQL> select * from dual;

D
-
X

SQL> desc student


Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(10)
SNAME VARCHAR2(20)
SFEE NUMBER(6,2)

SQL> truncate table student;

Table truncated.

SQL> Insert all


2 into student values(1,'abc',1234.56)
3 into student values(2,'xyz',2233.45)
4 into student values(3,'pqr',2233.67)
5 select * from dual;
3 rows created.

SQL> select * from student;

SID SNAME SFEE


---------- -------------------- ----------
1 abc 1234.56
2 xyz 2233.45
3 pqr 2233.67

SQL> create table s1 as select *from student;

Table created.

SQL> select * from s1;

SID SNAME SFEE


---------- -------------------- ----------
1 abc 1234.56
2 xyz 2233.45
3 pqr 2233.67

SQL> truncate table student;

Table truncated.

SQL> Insert all


2 into student values(1,'abc',1234.56)
3 into student values(2,'xyz',2233.45)
4 into student values(3,'pqr',2233.67)
5 select * from s1;

9 rows created.

SQL> select * from student;

SID SNAME SFEE


---------- -------------------- ----------
1 abc 1234.56
1 abc 1234.56
1 abc 1234.56
2 xyz 2233.45
2 xyz 2233.45
2 xyz 2233.45
3 pqr 2233.67
3 pqr 2233.67
3 pqr 2233.67

9 rows selected.

SQL> truncate table student;

Table truncated.

SQL> truncate table s1;

Table truncated.

SQL> desc student


Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(10)
SNAME VARCHAR2(20)
SFEE NUMBER(6,2)

SQL> alter table s1 drop column sfee;

Table altered.

SQL> desc t1
ERROR:
ORA-04043: object t1 does not exist

SQL> desc s1
Name Null? Type
----------------------------------------- -------- ----------------------------
SID NUMBER(10)
SNAME VARCHAR2(20)

SQL> Insert all


2 into student values(1,'abc',1234.56)
3 into student values(2,'xyz',2233.45)
4 into student values(3,'pqr',2233.67)
5 into s1 values(1,'abc')
6 into s1 values(2,'xyz')
7 select * from dual;

5 rows created.

SQL> select * from student;

SID SNAME SFEE


---------- -------------------- ----------
1 abc 1234.56
2 xyz 2233.45
3 pqr 2233.67

SQL> select * from s1;

SID SNAME
---------- --------------------
1 abc
2 xyz

update
---------
(i) to update one row in one column
------------------------------------
syntax
--------
update table_name set col_name=new_value where condition;

example
-------
SQL> select * from student;
SID SNAME SFEE
---------- -------------------- ----------
1 abc 1234.56
2 xyz 2233.45
3 pqr 2233.67

SQL> update student set sname='ajay' where sid=1;

1 row updated.

SQL> select * from student;

SID SNAME SFEE


---------- -------------------- ----------
1 ajay 1234.56
2 xyz 2233.45
3 pqr 2233.67

(ii) to update one row in all the columns

syntax
---------
update table_name set col_name=new_value where condition;

example
---------
SQL> update student set sid=100, sname='raj', sfee=3344.56 where sid=1;

1 row updated.

SQL> select * from student;

SID SNAME SFEE


---------- -------------------- ----------
100 raj 3344.56
2 xyz 2233.45
3 pqr 2233.67

(iii) to update all the rows in one column

syntax
--------
update table_name set col_nm=new_val;

example
---------
SQL> update student set sfee=1234.56;

3 rows updated.

SQL> select * from student;

SID SNAME SFEE


---------- -------------------- ----------
100 raj 1234.56
2 xyz 1234.56
3 pqr 1234.56
delete
---------
(i) to delete only one row
-----------------------------
delete from table_name where condition;

example
---------
SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7839 KING PRESIDENT 17-NOV-81 5000
10
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7566 JONES MANAGER 7839 02-APR-81 2975
20
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL> delete from emp where empno=7788;

1 row deleted.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7839 KING PRESIDENT 17-NOV-81 5000
10
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7566 JONES MANAGER 7839 02-APR-81 2975
20
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7934 MILLER CLERK 7782 23-JAN-82 1300
10

13 rows selected.

SQL> delete from emp where empno in (7698,7369,7876,7900,9000);

4 rows deleted.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7839 KING PRESIDENT 17-NOV-81 5000
10
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7566 JONES MANAGER 7839 02-APR-81 2975
20
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7934 MILLER CLERK 7782 23-JAN-82 1300
10

9 rows selected.

SQL> rollback;
Rollback complete.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7839 KING PRESIDENT 17-NOV-81 5000
10
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7566 JONES MANAGER 7839 02-APR-81 2975
20
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL> delete from emp;

14 rows deleted.

SQL> select * from emp;

no rows selected

SQL> rollback;

Rollback complete.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
----------
7839 KING PRESIDENT 17-NOV-81 5000
10
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7566 JONES MANAGER 7839 02-APR-81 2975
20
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

DCL
----Data control lang (Admin)
Grant
---------
syntax
---------
grant permissions to user_name;

Revoke
---------
revoke permissions from user_name;

example
-----------
SQL> conn system/system
Connected.
SQL> create user aj identified by aj123;

User created.

SQL> grant dba to aj;

Grant succeeded.

SQL> conn aj/aj123


Connected.
SQL> conn system/system
Connected.
SQL> revoke dba from aj;

Revoke succeeded.

SQL> conn aj/aj123


ERROR:
ORA-01045: user AJ lacks CREATE SESSION privilege; logon denied

Warning: You are no longer connected to ORACLE.

TCL Transaction control lang


------
commit
rollback
savepoint

DRL/DQL Data retrieval/query


---------
select

Operators
----------
(+,-,*,/)
(<,<=,>,>=,<>)
(And, or)
(Between, In, Like, Is null)

Between
----------
select col(s) from table_name where col_nm between
lower_bound_val and Upper_bound_val;

In
-----
select col(s) from tab_name where col_nm in (val1,val2,...);
Like
-----
select col(s) from tab_name where col_nm like 'pattern';

_ (only one char/digit)

% (multiple digits/chars)

select col(s) from tab_name where col_nm between L_B_v and U_B_V;

select col(s) from tab_name where col_nm in (val1,val2,....);

select col(s) from tab_name where col_nm like 'pattern';

select col(s) from tab_name where col_nm is null;

Functions
-------------
DB object

Numeric
---------
1. scalar
---------------
1. abs()
2. power()
3. sqrt()
4. mod()
5. round()
6. trunc()
7. floor()
8. ceil()
9. least()
10. greatest()

2. Group
--------------
sum()
max()
min()
avg()
count()
count(*)

String/char
--------------
ascii()
chr()
lower()
upper()
length()
substr()
instr()//indexOf()
translate()
replace()
trim()
ltrim()
rtrim()
trim() leading
trim() trailing
trim() both
lpad()
rpad()
concat()
dump()
vsize()
nvl()
nvl2()

Date
Conversion
Integrity Constraints (reliability+Consistency)
------------------------------------------------

Unique
not null
primary key Key
check Domain
foreign key Referential Integrity

1. column level
2. table level

simple unique at column level


--------------------------------

SQL> create table t1


2 (
3 id number(10),
4 name varchar2(20),
unique(id)
5 );

examples
-----------
SQL> create table t1
2 (
3 id number(10) unique,
4 name varchar2(20)
5 );

Table created.

SQL> insert into t1 values(1,'a');

1 row created.

SQL> insert into t1 values(1,'b');


insert into t1 values(1,'b')
*
ERROR at line 1:
ORA-00001: unique constraint (ANIL.SYS_C007003) violated

SQL> select * from t1;

ID NAME
---------- --------------------
1 a

SQL> insert into t1 values(null,'b');

1 row created.

SQL> insert into t1 values(null,'c');

1 row created.
SQL> insert into t1 values(null,'d');

1 row created.

SQL> select * from t1;

ID NAME
---------- --------------------
1 a
b
c
d

SQL> create table t2


2 (id number(10),
3 name varchar2(20),
4 unique(name)
5 );

Table created.

SQL> insert into t2 values(1,'a');

1 row created.

SQL> insert into t2 values(2,'a');


insert into t2 values(2,'a')
*
ERROR at line 1:
ORA-00001: unique constraint (ANIL.SYS_C007004) violated

SQL> desc user_constraints


Name Null?
Type
------------------------------------------------------------------------ --------
-------------------------------------------------
OWNER
VARCHAR2(120)
CONSTRAINT_NAME NOT NULL
VARCHAR2(30)
CONSTRAINT_TYPE
VARCHAR2(1)
TABLE_NAME NOT NULL
VARCHAR2(30)
SEARCH_CONDITION
LONG
R_OWNER
VARCHAR2(120)
R_CONSTRAINT_NAME
VARCHAR2(30)
DELETE_RULE
VARCHAR2(9)
STATUS
VARCHAR2(8)
DEFERRABLE
VARCHAR2(14)
DEFERRED
VARCHAR2(9)
VALIDATED
VARCHAR2(13)
GENERATED
VARCHAR2(14)
BAD
VARCHAR2(3)
RELY
VARCHAR2(4)
LAST_CHANGE
DATE
INDEX_OWNER
VARCHAR2(30)
INDEX_NAME
VARCHAR2(30)
INVALID
VARCHAR2(7)
VIEW_RELATED
VARCHAR2(14)

SQL> select constraint_name,constraint_type from user_constraints


2 where table_name='t1';

no rows selected

SQL> select constraint_name,constraint_type from user_constraints


2 where table_name='T1';

CONSTRAINT_NAME C
------------------------------ -
SYS_C007003 U

SQL> create table t3


2 (
3 id number(10),
4 name varchar2(20),
5 constraint t3_id_u unique(id)
6 );

Table created.

SQL> select constraint_name,constraint_type from user_constraints


2 where table_name='T3';

CONSTRAINT_NAME C
------------------------------ -
T3_ID_U U

SQL> insert into t3 values(1,'a');

1 row created.

SQL> insert into t3 values(1,'b');


insert into t3 values(1,'b')
*
ERROR at line 1:
ORA-00001: unique constraint (ANIL.T3_ID_U) violated

SQL> alter table t3 drop constraint unique;


alter table t3 drop constraint unique
*
ERROR at line 1:
ORA-02250: missing or invalid constraint name

SQL> alter table t3 drop constraint t3_id_u;

Table altered.

SQL> insert into t3 values(1,'b');

1 row created.

SQL> insert into t3 values(1,'b');

1 row created.

SQL> insert into t3 values(1,'b');

1 row created.

SQL> truncate table t3;

Table truncated.

SQL> desc t3
Name Null?
Type
------------------------------------------------------------------------ --------
-------------------------------------------------
ID
NUMBER(10)
NAME
VARCHAR2(20)

SQL> alter table t3 add unique(name);

Table altered.

SQL> insert into t3 values(1,'a');

1 row created.

SQL> insert into t3 values(2,'a');


insert into t3 values(2,'a')
*
ERROR at line 1:
ORA-00001: unique constraint (ANIL.SYS_C007006) violated

SQL> create table t4


2 (
3 id number(10),
4 name varchar2(20),
5 sal number(10),
6 deptno nuber(10),
7 unique(id,name)
8 );
deptno nuber(10),
*
ERROR at line 6:
ORA-00907: missing right parenthesis

SQL> create table t4


2 (
3 id number(10),
4 name varchar2(20),
5 sal number(10),
6 deptno number(10),
7 unique(id,name)
8 );

Table created.

SQL> insert into t4 values(1,'a',10,100);

1 row created.

SQL> insert into t4 values(1,'b',11,101);

1 row created.

SQL> insert into t4 values(2,'a',12,102);

1 row created.

SQL> insert into t4 values(1,'a',13,103);


insert into t4 values(1,'a',13,103)
*
ERROR at line 1:
ORA-00001: unique constraint (ANIL.SYS_C007007) violated

SQL> insert into t4 values(1,'b',13,103);


insert into t4 values(1,'b',13,103)
*
ERROR at line 1:
ORA-00001: unique constraint (ANIL.SYS_C007007) violated

SQL> insert into t4 values(2,'a',13,103);


insert into t4 values(2,'a',13,103)
*
ERROR at line 1:
ORA-00001: unique constraint (ANIL.SYS_C007007) violated

SQL> insert into t4 values(2,'b',13,103);

1 row created.

SQL> insert into t4 values(null,'b',13,103);

1 row created.

SQL> insert into t4 values(3,null,13,103);


1 row created.

SQL> insert into t4 values(null,null,13,103);

1 row created.

SQL> insert into t4 values(null,null,13,103);

1 row created.

SQL> select * from t4;

ID NAME SAL DEPTNO


---------- -------------------- ---------- ----------
1 a 10 100
1 b 11 101
2 a 12 102
2 b 13 103
b 13 103
3 13 103
13 103
13 103

8 rows selected.

Not null
-----------
It can be applied only at column level
It is always a simple not null only

example
------------
SQL> create table t3
2 (
3 id number(10) not null,
4 name varchAR2(20) NOT NULL
5 );

Table created.

SQL> insert into t3 vaues(1,'a');


insert into t3 vaues(1,'a')
*
ERROR at line 1:
ORA-00928: missing SELECT keyword

SQL> insert into t3 values(1,'a');

1 row created.

SQL> insert into t3 values(null,'a');


insert into t3 values(null,'a')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("AJ"."T3"."ID")
SQL> insert into t3 values(1,null);
insert into t3 values(1,null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("AJ"."T3"."NAME")

SQL> insert into t3 values(null,null);


insert into t3 values(null,null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("AJ"."T3"."ID")

Primary key
--------------
simple PK
--------------
key column does not allows both duplicate and null values
A table allows only one PK(simple Pk || composite PK)
A table which is decorated with PK will be called as parent table
If table contains PK then we can easily identify a unique row/tuple in the
relation/table

emp
-----
id(pk) name
--------------
1 a
2 a
3 a
4 b
5 b

select * from emp where id=1;

example
---------
SQL> create table t4
2 (
3 id number(10),
4 name varchar2(20),
5 age number(2),
6 primary key(id)
7 );

Table created.

SQL> insert into t4 values(1,'a',10);

1 row created.

SQL> insert into t4 values(1,'b',20);


insert into t4 values(1,'b',20)
*
ERROR at line 1:
ORA-00001: unique constraint (AJ.SYS_C007024) violated
SQL> insert into t4 values(null,'b',20);
insert into t4 values(null,'b',20)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("AJ"."T4"."ID")

SQL> truncate table t4;

Table truncated.

SQL> desc t4
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(10)
NAME VARCHAR2(20)
AGE NUMBER(2)

SQL> select * from t4;

no rows selected

SQL> alter table t4 add primary key(age);


alter table t4 add primary key(age)
*
ERROR at line 1:
ORA-02260: table can have only one primary key

SQL> insert into t4 values(1,'a',10);

1 row created.

SQL> insert into t4 values(2,'a',10);

1 row created.

example
---------
SQL> create table t5
2 (
3 id number(10),
4 name varchar2(20),
5 age number(2),
6 primary key(id,name)
7 );

Table created.

SQL> insert into t5 values(1,'a',10);

1 row created.

SQL> insert into t5 values(1,'b',20);

1 row created.
SQL> insert into t5 values(2,'a',20);

1 row created.

SQL> insert into t5 values(1,'a',40);


insert into t5 values(1,'a',40)
*
ERROR at line 1:
ORA-00001: unique constraint (AJ.SYS_C007025) violated

SQL> insert into t5 values(null,'a',40);


insert into t5 values(null,'a',40)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("AJ"."T5"."ID")

SQL> insert into t5 values(3,null,40);


insert into t5 values(3,null,40)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("AJ"."T5"."NAME")

SQL> insert into t5 values(null,null,40);


insert into t5 values(null,null,40)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("AJ"."T5"."ID")

check
----------
example
---------

SQL> alter table t5 add check(age>=18 and age<=30);

Table altered.

SQL> insert into t5 values(1,'a',15);


insert into t5 values(1,'a',15)
*
ERROR at line 1:
ORA-02290: check constraint (AJ.SYS_C007026) violated

SQL> insert into t5 values(1,'a',18);

1 row created.

SQL> insert into t5 values(2,'a',31);


insert into t5 values(2,'a',31)
*
ERROR at line 1:
ORA-02290: check constraint (AJ.SYS_C007026) violated

foreign key
---------------
It is a referenctial Integrity constraint
A table can have more than one FK
FK field allows duplicate data and null vlaues
A table chich constains FK will be called as Child table
FK field allows the data which matches with data present in PK field
emp
----
id(pk) name
---------------
1 a
2 a
3 b

dept
-----
did(fk) dname
--------------
1
2
3
1
2
3
1
2
3

You might also like