Practical 3.
DEFINING DATA
1.Using CREATE Statement
2.Using ALTER Statement
3.Using DROP Statement
4.Using RENAME Statement
5.Using TRUNCATE Statement
1) CREATE TABLE
create table student_info(std_id number(10),std_name varchar2(30),std_class
varchar2(10), DOB date , std_add varchar2(50));
DESC student_info
Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment
STUDENT_INFO STD_ID Number - 10 0 - - -
STD_NAME Varchar2 30 - - - - -
STD_CLASS Varchar2 10 - - - - -
DOB Date 7 - - - - -
-
STD_ADD Varchar2 50 - - - -
1-5
2) ALTER Table (Add column)
ALTER TABLE student_info ADD Email varchar(255);
DESC student_info
Primary
Table Column Data Type Length Precision Scale Nullable Default Comment
Key
STUDENT_INFO STD_ID Number - 10 0 - - -
STD_NAME Varchar2 30 - - - - -
STD_CLASS Varchar2 10 - - - - -
DOB Date 7 - - - - -
STD_ADD Varchar2 50 - - - - -
EMAIL Varchar2 255 - - - - -
Student_info
Std_id Std_name Std_class DOB Stud_add Email
3) ALTER TABLE (DROP COLUMN)
ALTER TABLE student_info DROP COLUMN Email;
DESC student_info
4) RENAME TABLE
ALTER TABLE student_info RENAME TO Student_Details;
Student_Details
Std_id Std_name Std_class DOB Stud_add
DESC Student_Details
5) Truncate Table
Truncate table Student Details
6) DROP TABLE
drop table Student_Details
What is DROP Command?
DROP is used to delete a whole database or just a table.
What is TRUNCATE Command?
The major difference between TRUNCATE and DROP is that truncate is used to delete the data inside the table not the whole table.
Practical 5.
Creating table with constraints
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each
row in a table
FOREIGN KEY - Prevents actions that would destroy links between tables
CHECK CONSTRAINT- we can specify a condition for a field, which should be satisfied
at the time of entering values for this field
1. NOT NULL –
If we specify a field in a table to be NOT NULL. Then the field will never accept null value. That
is, you will be not allowed to insert a new row in the table without specifying any value to this
field.
For example, the below query creates a table Student with the fields ID and NAME as NOT
NULL. That is, we are bound to specify values for these two fields every time we wish to insert
a new row.
CREATE TABLE Student(
ID int NOT NULL,
NAME varchar(10) NOT NULL,
ADDRESS varchar(20));
2. UNIQUE –
This constraint helps to uniquely identify each row in the table. i.e. for a particular column, all
the rows should have unique values. We can have more than one UNIQUE columns in a table.
For example, the below query creates a table Student where the field ID is specified as
UNIQUE. i.e, no two students can have the same ID. Unique constraint in detail.
CREATE TABLE Student(
ID int NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20));
3. PRIMARY KEY –
Primary Key is a field which uniquely identifies each row in the table. If a field in a table as
primary key, then the field will not be able to contain NULL values as well as all the rows
should have unique values for this field. So, in other words we can say that this is combination
of NOT NULL and UNIQUE constraints.
A table can have only one field as primary key. Below query will create a table named Student
and specifies the field ID as primary key.
CREATE TABLE Student(library
ID int NOT NULL,
NAME varchar(10),
ADDRESS varchar(20),
PRIMARY KEY(ID));
4. CHECK –
Using the CHECK constraint we can specify a condition for a field, which should be satisfied at
the time of entering values for this field.
For example, the below query creates a table Student and specifies the condition for the field
AGE as (AGE >= 18 ). That is, the user will not be allowed to enter any record in the table with
AGE < 18. Check constraint in detail
CREATE TABLE Student1234(
ID int NOT NULL,
NAME varchar(10) NOT NULL,
AGE int NOT NULL CHECK (AGE >= 18));
insert into Student1234 values (101,'Mahesh',25);
insert into Student1234 values (102,'Yogesh',15);
5. FOREIGN KEY –
Foreign Key is a field in a table which uniquely identifies each row of a another table. That is,
this field points to primary key of another table. This usually creates a kind of link between the
tables.
Consider the two tables as shown below:
CREATE TABLE Customers (C_ID int NOT NULL, NAME varchar(50), ADDRESS varchar(200),
PRIMARY KEY(C_ID));
C_ID NAME ADDRESS
1 RAMESH DELHI
2 SURESH NOIDA
3 DHARMESH GURGAON
CREATE TABLE Orders(O_ID int NOT NULL,ORDER_NO int NOT NULL,
C_ID int, PRIMARY KEY (O_ID),FOREIGN KEY (C_ID) REFERENCES Customers(C_ID));
O_ID ORDER_NO C_ID
1 2253 3
2 3325 3
3 4521 2
4 8532 1
Practical 4
Manipulating Data
1. Using INSERT Statement
2. Using UPDATE Statement
3. Using DELETE Statement
4. Using SELECT statement
1)INSERT
insert into student_info values (101,'Mahesh','FYIT', DATE'2005-04-22','Sector 28 Vashi');
insert into student_info values (102,'Yogesh','FYIT',
TO_DATE('2004/09/27','yyyy/mm/dd'),'Ghansoli');
insert into student_info values (103,'Sarita','FYIT', DATE'2007-06-01','Sector 12 Vashi');
insert into student_info values (104,'Kasturi','FYIT', DATE'2006-10-02','Vashi');
insert into student_info values (105,'Rahul','FYIT', DATE'2007-12-30','Kurla');
insert into student_info values (106,'Kapil','FYIT', DATE'2005-08-15','Dombivali');
2) Select
select * from student_info;
Show all records from table
Draw a table with all column name and write a records (10RECORDS)
3) UPDATE
update student_info SET std_Name = 'Mansi' WHERE std_id = 101;
select * from student_info;
Draw a table with all column name and write a records
4)DELETE
DELETE FROM student_info WHERE std_name = 'Kapil';
Delete * from student_info;
Draw a table with all column name and write a records
desc student_info
Create table Employee and Department using constraint
Insert 10 Record in Employee
Insert 4 Department in Department table
Emp_id Emp_Name Emp_Add Emp_DOB D_Name Emp_Sal Age
1001 Murti Kale Vashi HR
1002 Dhanaji Kopar ACCOUNT
Kolhe
1003 Sarika Ghansoli IT
Kamble
1004 Sayyad Khan Mumbra HR
1005 Kasturi Dombivali IT
Shaha
1006 Anvi Wagh Thakurli IT
1007 Devansh vashi ACCOUNT
Mohite
1008 Sabana Azmi kopar HR
1009 Mohit Patil ghansoli IT
D_id D_Name
101 HR
102 IT
103 ACCOUNT
104 Social Activity