Professional Documents
Culture Documents
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.
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.
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.
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.
6.
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
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
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.
7 rows selected.
b.
7 rows selected.
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.
Updation without where clause: Add 100 to roll number of all the students in student table:
(b)
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.
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:
Truncating a table :
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.
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:
a.
Query:
SQL> create table student ( roll_no number(3) primary key, name varchar2(10), branch varchar2(10) ); Output: Table created.
b.
Query:
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.
b.
Query:
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.
b.
Query:
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)
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.
b.
Query:
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.
2.
Query:
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:
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:
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.
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.
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);
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.
IV Dropping constraints
1. Foreign key
2. a.
Query
SQL> alter table student drop primary key; Output: Table altered
b.
Query
SQL> alter table student1 drop constraint pk2; Output: Table altered
3. a.
Query
b.
Query
4.
Check
5. a.
Query
b.
Query