You are on page 1of 26

EXPERIMENT 2

1. Table creation
Write a query to create a table student with following attributes: - name , character type , size-10 - roll_no, numeric type, size-3 - branch character type, size-10
Query: SQL> create table student ( name varchar2(10), roll_no number(3), branch varchar2(10) ); Output: Table created.

2. Adding data into tables


Add data to all the attributes in the order mentioned during table creation: add a record in student table
a. Query: SQL> insert into student values('Anjali',101,'IT'); Output: 1 row created.

Add data to all the attributes in the different order: add a record in studnt table
b. Query: SQL> insert into student(roll_no,branch,name) values(102,'IT','Sanjay'); Output: 1 row created

Add data to few attributes: add a record to student table (only to roll number and name)
c. Query: SQL> insert into student(roll_no,name) values(103,'John'); Output: 1 row created.

3. Adding multiple rows using one query


Add multiple rows to student table using one query:
Query: SQL> insert into student values('&name',&roll,'&branch'); Enter value for name: Smith Enter value for roll: 104 Enter value for branch: IT old 1: insert into student values('&name',&roll,'&branch') new 1: insert into student values('Smith',104,'IT') Output: 1 row created. SQL> / Enter value for name: Siya Enter value for roll: 201 Enter value for branch: CSE old 1: insert into student values('&name',&roll,'&branch') new 1: insert into student values('Siya',201,'CSE') Output: 1 row created. SQL> / Enter value for name: Amit Enter value for roll: 202 Enter value for branch: CSE old 1: insert into student values('&name',&roll,'&branch') new 1: insert into student values('Amit',202,'CSE') Output: 1 row created.

SQL> / Enter value for name: tripti Enter value for roll: 203 Enter value for branch: CSE old 1: insert into student values('&name',&roll,'&branch') new 1: insert into student values('tripti',203,'CSE') Output: 1 row created. SQL> / Enter value for name: tripti Enter value for roll: 203 Enter value for branch: CSE old 1: insert into student values('&name',&roll,'&branch') new 1: insert into student values('tripti',203,'CSE') Output: 1 row created.

4. Viewing data of a table


All columns and all rows to view all the details of all the students from student table
a. Query: SQL> select * from student; Output: NAME ---------Anjali Sanjay John Smith Siya Amit tripti tripti ROLL_NO ---------101 102 103 104 201 202 203 203 BRANCH ---------IT IT IT CSE CSE CSE CSE

8 rows selected.

All columns and selected rows to view all the details of all the students in IT branch from student table
b. Query: SQL> select * from student where branch='IT'; Output: NAME ---------Anjali Sanjay Smith c. ROLL_NO ---------101 102 104 BRANCH ---------IT IT IT

Selected columns and all rows to view roll number and name of all the students from student table
Query: SQL> select roll_no, name from student; Output: ROLL_NO ---------101 102 103 104 201 202 203 203 8 rows selected. NAME ---------Anjali Sanjay John Smith Siya Amit tripti tripti

Selected columns and selected rows to view roll number and name of all the students in IT branch from student table
d. Query: SQL> select roll_no, name from student where branch='IT'; Output: ROLL_NO ---------101 102 104 NAME ---------Anjali Sanjay Smith

5.

Eliminating duplicate rows when using select statement :

To view the distinct branch names from student table


Query: SQL> select distinct(branch) from student; Output: BRANCH ---------IT CSE

6.

To view the data in sorted form

a. Sorted in ascending order to view the details of all the students order by name in ascending order:
Query: SQL> select * from student order by name; Output: NAME ROLL_NO ---------- ---------Amit 202 Anjali 101 John 103 Sanjay 102 Siya 201 Smith 104 tripti 203 tripti 203 8 rows selected. BRANCH ---------CSE IT IT CSE IT CSE CSE

OR
Query: SQL> select * from student order by name asc;

Output: NAME ROLL_NO ---------- ---------Amit 202 Anjali 101 John 103 Sanjay 102 Siya 201 Smith 104 tripti 203 tripti 203 8 rows selected. BRANCH ---------CSE IT IT CSE IT CSE CSE

Sorted in descending order to view the details of all the students order by name in ascending order:
b. Query: SQL> select * from student order by name desc; Output: NAME ROLL_NO ---------- ---------tripti 203 tripti 203 Smith 104 Siya 201 Sanjay 102 John 103 Anjali 101 Amit 202 8 rows selected. 7.Creating BRANCH ---------CSE CSE IT CSE IT IT CSE

a table from an existing table :

Create a table student1 from student table with following attributes: - sname - sroll - sbranch:
Query: SQL> create table student1(sname,sroll,sbranch) as select name,roll_no, branch from student;

Output: Table created. Note: The above query automatically set the datatype and size of different attributes as of the existing tables attributes and this query also copies the contents of the student (if any) to student1 table. 8.Adding

data into a table from an existing table

First create a table student2 with following attributes: - s_name character type, size-10, - s_roll numeric type, size-3, - s_branch character type, size 10 After creating this table copy the data from existing table as follows:
Query: SQL> create table student2 ( s_name varchar2(10), s_roll number(3), s_branch varchar2(10) ); Output: Table created. Queryto add data from existing table: SQL> insert into student2 select name,roll_no,branch from student; Output: 8 rows created.

EXPERIMENT 3
1. Deleting records or rows from a table
a. Deleting few rows: Status before deletion
Query: SQL> select * from student2; Output: S_NAME S_ROLL S_BRANCH ------------------- ---------Anjali 101 IT Sanjay 102 IT John 103 Smith 104 IT Siya 201 CSE Amit 202 CSE tripti 203 CSE tripti 203 CSE 8 rows selected.

Delete query: delete all the details of roll number 101 from student2 table:
Query: SQL> delete from student2 where s_roll=101; Output: 1 row deleted.

Status after deletion


Query: SQL> select * from student2; Output: S_NAME ---------Sanjay John Smith Siya Amit tripti tripti S_ROLL ---------102 103 104 201 202 203 203 S_BRANCH ---------IT IT CSE CSE CSE CSE

7 rows selected.

b.

Deleting all the rows:

Status before deletion


Query: SQL> select * from student2; Output: S_NAME ---------Sanjay John Smith Siya Amit tripti tripti S_ROLL ---------102 103 104 201 202 203 203 S_BRANCH ---------IT IT CSE CSE CSE CSE

7 rows selected.

Delete query : to delete all the records of student 2 table


Query: SQL> delete from student2; Output: 7 rows deleted.

Status after deletion


Query: SQL> select * from student2; Output: no rows selected 2.

Updating the contents of a table:


Updation using where clause: update the name of a student from Riya to Siya in student table:
(a)

Status of student table before updation:


Query: SQL> select * from student;

Output: NAME ---------Anjali Sanjay John Smith Siya Amit tripti tripti ROLL_NO ---------101 102 103 104 201 202 203 203 BRANCH ---------IT IT IT CSE CSE CSE CSE

8 rows selected. Update query: SQL> update student set name='Riya' where name='Siya'; Output: 1 row updated.

Status of student table after updation


Query: SQL> select * from student; Output: NAME ---------Anjali Sanjay John Smith Riya Amit tripti tripti 8 rows selected. ROLL_NO ---------101 102 103 104 201 202 203 203 BRANCH ---------IT IT IT CSE CSE CSE CSE

Updation without where clause: Add 100 to roll number of all the students in student table:
(b)

Status of student table before updation:


Query: SQL> select * from student; Output: NAME ---------Anjali Sanjay John Smith Riya Amit tripti tripti 8 rows selected. Update query: SQL> update student set roll_no=roll_no+100; Output: 8 rows updated. ROLL_NO ---------101 102 103 104 201 202 203 203 BRANCH ---------IT IT IT CSE CSE CSE CSE

Status of student table after updation:


Query: SQL> select * from student ; Output: NAME ---------Anjali Sanjay John Smith Riya Amit tripti tripti 8 rows selected. ROLL_NO ---------201 202 203 204 301 302 303 303 BRANCH ---------IT IT IT CSE CSE CSE CSE

3. Modifying the structure of a table


a. Adding new columns to a table: Add contact (numeric, size-10) to student table Status of student table before adding the new column
Query: SQL> select * from student ; Output: NAME ---------Anjali Sanjay John Smith Riya Amit tripti tripti 8 rows selected. Query to add new column: Query: SQL> alter table student add (contact number(10)); Output: Table altered. ROLL_NO ---------201 202 203 204 301 302 303 303 BRANCH ---------IT IT IT CSE CSE CSE CSE

Status of student table after adding new column


Query: SQL> select * from student ; Output: NAME ---------Anjali Sanjay John ROLL_NO ---------201 202 203 BRANCH CONTACT ------------------IT IT

Smith Riya Amit tripti tripti

204 301 302 303 303

IT CSE CSE CSE CSE

b. Deleting existing column of a table : delete the contact attribute from student table Status of student table before deleting contact attribute
Query: SQL> select * from student ; Output: NAME ---------Anjali Sanjay John Smith Riya Amit tripti tripti ROLL_NO ---------201 202 203 204 301 302 303 303 BRANCH CONTACT ------------------IT IT IT CSE CSE CSE CSE

8 rows selected. Query to delete contact column: SQL> alter table student drop column contact; Output: Table altered.

Status of student table after dropping the contact attribute:


Query: SQL> select * from student ; Output: NAME ---------Anjali ROLL_NO ---------201 BRANCH ---------IT

Sanjay John Smith Riya Amit tripti tripti 8 rows selected.

202 203 204 301 302 303 303

IT IT CSE CSE CSE CSE

c. Modifying the column datatype or size: change the branch attribute datatype from varchar2 to char and size from 10 to 12.
Query: SQL> alter table student modify (branch char(12)); Output: Table altered. Note: Alter command cannot do following: (i) change the name of a table (ii) change the name of a column (iii) decrease the size of a datatype if existing data contents are larger than the new size (iv) changing of non compatible datatypes if data exists. 4.

Renaming a table:

Change the name of table student1 to s1:


Query: SQL> rename student1 to s1; Output: Table renamed. 5.

Truncating a table :

Truncate table s1: Status of s1 before truncate command


Query: SQL> select * from s1;

Output: SNAME ---------Anjali Sanjay John Smith Siya Amit tripti tripti SROLL ---------101 102 103 104 201 202 203 203 SBRANCH ---------IT IT IT CSE CSE CSE CSE

8 rows selected. Truncate query: SQL> truncate table s1; Output: Table truncated.

Status of s1 after truncate command


Query: SQL> select * from s1; Output: no rows selected Note: Truncate command is equivalent to the delete command in the sense that it deletes all the rows of a table but these commands are different. Difference between truncate command and delete command: (i) Truncate is combination of drop and automatically recreation of a table. (j) Truncate is DDL command whereas delete is DML command. (k) Truncate command does not return the number of rows deleted whereas delete command does. 6.

Destroying tables: destroy s1 table:

Query: SQL> drop table s1; Output: Table dropped.

7. Finding out the table/s created by a user


Query: SQL> select * from tab; Output: TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------STUDENT TABLE STUDENT2 TABLE S TABLE S2 TABLE CUSTOMER TABLE 5 rows selected. 8.

Displaying the table structure:

Query: SQL> desc student; Output: Name Null? Type ----------------------------------------- -------- ---------------------------NAME ROLL_NO BRANCH VARCHAR2(10) NUMBER(3) CHAR(12)

OR
Query: SQL> describe student; Output: Name Null? Type ----------------------------------------- -------- ---------------------------NAME ROLL_NO BRANCH VARCHAR2(10) NUMBER(3) CHAR(12)

EXPERIMENT 4
DATA CONSTRAINTS:

Primary key Foreign key Unique Not null Check

I Setting constraints during the table creation


1. Primary key

a.
Query:

Primary key declaration at column level

SQL> create table student ( roll_no number(3) primary key, name varchar2(10), branch varchar2(10) ); Output: Table created.

b.
Query:

Primary key declaration at table level

SQL> create table student1 ( roll_no number(3), name varchar2(10), branch varchar2(10), primary key(roll_no) ); Output: Table created.

2. Foreign key a.
Query: SQL> create table result ( roll_no number(3) references student(roll_no), maths number(3), science number(3), english number(3) ); Output: Table created.

Foreign key declaration at column level

b.
Query:

Foreign key declaration at table level

SQL> create table result1 ( roll_no number(3), maths number(3), science number(3), english number(3), foreign key(roll_no) references student1(roll_no) ); Output: Table created.

3. Unique constraint a.
Query: SQL> create table emp ( e_id varchar2(4) primary key, e_name varchar2(10), e_contact number(10) unique ); Output: Table created.

Unique constraint declaration at column level

b.
Query:

Unique constraint declaration at table level

SQL> create table emp1 ( e_id varchar2(4) primary key, e_name varchar2(10), e_contact number(10), unique(e_contact) ); Output: Table created. 4. Not null constraint (can only be declared at column level)

Not null constraint at column level


Query: SQL> create table emp2 ( e_id varchar2(4) primary key, e_name varchar2(10) not null, e_contact number(10) ); Output: Table created.

5. Check constraint a.
Query: SQL> create table emp3 ( e_id varchar2(4) primary key check(e_id like E%), e_name varchar2(10) not null check(e_name=upper(e_name)), e_deptt varchar2(3) check(deptt in(IT,CSE,EEE,ECE,MAE)) ); Output: Table created.

Check constraint declaration at column level

b.
Query:

Check constraint declaration at table level

SQL> create table emp4 ( e_id varchar2(4) primary key, e_name varchar2(10) not null, e_deptt varchar2(3), check(e_id like E%), check(e_name=upper(e_name)), check(deptt in(IT,CSE,EEE,ECE,MAE)) ); Output: Table created.

II. Naming the constraints during table creation


1. Assigning names to primary key, not null, unique, check at column level
Query: SQL> create table student ( roll number(3) constraint pk primary key, name varchar2(10) constraint ck check(name=initcap(name)), branch varchar2(10) constraint nn not null, contact number(10) constraint u unique ); Output: Table created.

2.
Query:

Assigning names to primary key, unique, check at table level

SQL> create table student1 ( roll number(3), name varchar2(10), branch varchar2(10), contact number(10), constraint pk1 primary key(roll), constraint ck1 check(name=initcap(name)), constraint u1 unique(contact) ); Output: Table created.

3.
Query:

Assigning name to foreign key at column level

SQL> create table result ( roll number(3) constraint fk references student(roll), maths number(3), science number(3), english number(3) ); Output: Table created.

4.
Query:

Assigning name to foreign key at table level

SQL> create table result1 ( roll number(3), maths number(3), science number(3), english number(3), constraint fk1 foreign key(roll)references student1(roll) ); Output: Table created.

III Setting constraints using alter command


First of all create tables student, student1, result,result1 as follows:
Query to create student table: SQL> create table student ( roll number(3), name varchar2(10), branch varchar2(10), contact number(10) ); Output: Table created.

Query to create student1 table: SQL> create table student1 ( roll number(3), name varchar2(10), branch varchar2(10), contact number(10) ); Output: Table created. Query to create result table : SQL> create table result ( roll number(3), maths number(3), science number(3), english number(3) ); Output: Table created. Query to create result1 table : SQL> create table result1 ( roll number(3), maths number(3), science number(3), english number(3) ); Table created.

1. a. Query

Primary key, unique & check constraints Assigning constraint without name

SQL> alter table student add ( primary key(roll), check(name=upper(name)), unique(contact) ); Output: Table altered. b. Query SQL> alter table student1 add( constraint pk2 primary key(roll), constraint ck2 check(name=upper(name)), constraint u unique(contact) ); Output: Table altered.

Adding constraint with name

2. Not null
a. Query SQL> alter table student modify (branch not null); Output: Table altered b. Query SQL> alter table student1 modify (branch constraint nn not null);

Adding null constraint without name

Adding null constraint with name

Output: Table altered

3. Foreign key
a. Query SQL> alter table result add foreign key(roll) references student(roll); Output: Table altered b. Query SQL> alter table result1 add constraint fk2 foreign key(roll) references student(roll); Output: Table altered Note: - Oracle will not allow constraints defined using the alter table to be applied to the table if data previously placed in the table violates such constraints. - If a primary key constraint was being applied to a table & the column has duplicate values in it, the primary key constraint will not be set to that column.

Adding foreign keyconstraint without name

Adding foreign key constraint with name

IV Dropping constraints
1. Foreign key

Dropping foreign key using constraint name


Query SQL> alter table result1 drop constraint fk2; Output: Table altered

2. a.
Query

Primary key Dropping primary key without using constraint name

SQL> alter table student drop primary key; Output: Table altered

b.
Query

Dropping primary key using constraint name

SQL> alter table student1 drop constraint pk2; Output: Table altered

3. a.
Query

Unique Dropping unique without using constraint name

SQL> alter table student drop unique(contact); Output: Table altered

b.
Query

Dropping unique using constraint name

SQL> alter table student1 drop constraint u; Output: Table altered

4.

Check

Dropping check using constraint name


Query SQL> alter table student1 drop constraint ck2;

Output: Table altered

5. a.
Query

Not null Dropping not null without using constraint name

SQL> alter table student modify(branch null); Output: Table altered

b.
Query

Dropping not null using constraint name

SQL> alter table student1 drop constraint nn ; Output: Table altered

You might also like