Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

1 Table Creation

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

Creating Table without Constraints

Create Table Student


(Roll Int,
Name Varchar(40),
DOJ date);
Constraints:

Column Level Constraints – Column and constraint definition in the same


statement.

create table emp_master


(empid integer primary key,
name varchar2(50) not null,
date_of_joining date,
mail_id char(18) unique
);

create table emp_details


(empid integer references emp_master(empid),
deptno integer,
job varchar2(30),
salary integer check ( salary >= 5000)
);

Providing name to the constraint is optional. If user does not provide name then
Oracle will give an auto name.

The names are stored in the data dictionary table User_Constraints.

select constraint_name, constraint_type, table_name from user_constraints


where table_name='EMP_MASTER';

select constraint_name,constraint_type,table_name from user_constraints


where table_name='EMP_DETAILS';

Providing names to Column Level Constraints:

create table emp_master1


(empid integer constraint pk_empid primary key,
name varchar2(50) not null,
date_of_joining date,
mail_id char(18) constraint u_mail_id unique
);

create table emp_details1


(empid integer constraint fk_empid references emp_master(empid),
deptno integer,
job varchar2(30),
salary integer constraint c_salary check ( salary >= 5000)
);

The user defined names are reflected after querying user_constraints.

select constraint_name,constraint_type,table_name from user_constraints


where table_name='EMP_MASTER1';

select constraint_name,constraint_type,table_name from user_constraints


where table_name='EMP_DETAILS1';

Table Level Constraints. – The constraints are defined after the columns.

Constraints have to be on table level under following cases.


1. Comparisons of two or more columns
2. For creating Composite primary, unique or foreign keys.

Any column level constraint can also be defined at the table level. Exception is
only for Not Null constraint.

create table Customers


(customer_id number,
transaction_id varchar2(10),
name varchar2(10) not null,
final_amount integer,
discount integer,
primary key (customer_id, transaction_id),
unique (name),
check (final_amount >= discount)
);

Providing names to Table Level Constraints:

create table Customers1


(customer_id number,
transaction_id varchar2(10),
name varchar2(10) not null,
final_amount integer,
discount integer,
constraint comp_pk primary key (customer_id, transaction_id),
constraint u_name unique (name),
constraint amt_dis check (final_amount >= discount)
);

Creating Foreign key Constraint on the table level.

create table emp_details2


(empid integer,
deptno integer,
job varchar2(30),
salary integer,
constraint fk_empid2 Foreign Key(empid) references emp_master(empid)
);

Duplicating a table

Duplicating a table with data

Create table New_Emp


As
Select * from emp;

Duplicating a table without data

Create table New_Emp1


As
Select * from emp
Where 1 = 2;

DML Statements
Insert statement
-- One complete record
Insert into emp
values(1111,'one','ANALYST',7902,'1-jan-1911',10000,null,10);

-- One Partial Record


Method 1
Insert into emp(empno, ename, deptno)
values(22,'two',10);
Method 2
Insert into emp
values(333,'three',null,null,null,null,null,10);

Inserting record by user interaction (prompt)


Use &

Insert into emp(empno,ename,sal) values(&empno, ‘&ename’, &sal);

Copying Rows from another table (: good)


create table t11(a number, b number);
insert into t11 values(1,2);

create table t22(a number, b number);

insert into t22


select * from t11;

Copying Partial rows


insert into t11 values(3,4);

insert into t22(a)


select a from t11
where a = 3;

Default Values:

create table sales_data


(member_id integer,
country varchar2(60) default 'Denmark',
sales_amt integer);

Insert Into sales_Data Values(1, Default, 1000);

Insert Into Sales_Data (Member_id, Sales_Amt) Values(2, 2000);

Output:

SQL> Select * from Sales_Data;


MEMBER_ID COUNTRY
---------- ------------------------------------------------------------
SALES_AMT
----------
1 Denmark
1000

2 Denmark
2000

Delete statement

To delete specific records


Delete from emp
Where sal >= 3000;

To delete all records


Delete from emp;

Update statement

To update specific values


Update emp
Set sal = 4000
Where job = ‘CLERK’;

To update the entire column


Update emp
Set deptno = 100;

To make a complete column blank


Update emp
Set empno = null;
Referential Integrity

When records are present in the parent and the child tables then by
default the records from the parent tables cannot be deleted if the
dependency exists in the child tables.

CREATE TABLE STUDENT1


(ROLL INTEGER PRIMARY KEY,
NAME VARCHAR(40));

CREATE TABLE RESULT1


(ROLL INTEGER REFERENCES STUDENT1(ROLL),
MARKS INTEGER);

insert into student1 values(1,'a');


insert into result1 values(1,78);

delete from student1;

drop table result1;


drop table student1;

On Delete Cascade  When the parent records are deleted then


the corresponding child records will also get deleted.

CREATE TABLE STUDENT1


(ROLL INTEGER PRIMARY KEY,
NAME VARCHAR(40));

CREATE TABLE RESULT1


(ROLL INTEGER REFERENCES STUDENT1(ROLL) ON DELETE CASCADE,
MARKS INTEGER);

insert into student1 values(1,'a');


insert into result1 values(1,78);

delete from student1;

select * from result1; -- No records in the child table.

drop table result1;


drop table student1;
On Delete Set Null When the master record is deleted then the
related child table’s foreign key value becomes null.

CREATE TABLE STUDENT1


(ROLL INTEGER PRIMARY KEY,
NAME VARCHAR(40));

CREATE TABLE RESULT1


(ROLL INTEGER REFERENCES STUDENT1(ROLL) ON DELETE SET NULL,
MARKS INTEGER);

insert into student1 values(1,'a');


insert into result1 values(1,78);

delete from student1;

select * from result1; -- In the child table the rollno has become null

When relationship exists the parent table cannot get dropped.

Drop Table Student1;

Also the primary key cannot get dropped.

alter table student1


drop primary key;

But cascade clause can be added which will first drops the foreign
keys from the child table.

alter table student1


drop primary key cascade;

DDL Involving Parent Table

SQL> create table student (rollno number primary key, name varchar2(20),
cell_no number);
Table created.

SQL> create table result (rollno number references student(rollno), sem


char, marks number);

Table created.

SQL> drop table student;


drop table student

ORA-02449: unique/primary keys in table referenced by foreign keys

SQL> drop table student cascade constraints;

Table dropped.

You might also like