You are on page 1of 9

Theory and Concept

Practical #1

Objective: Create tables and specify the Questionnaires in SQL.

Theory & Concepts:

Introduction about SQL-


SQL (Structured Query Language) is a nonprocedural language, you specify what you
want, not how to get it. A block structured format of English key words is used in this
Query language. It has the following components.

DDL (Data Definition Language)-


The SQL DDL provides command for defining relation schemas, deleting relations and
modifying relation schema.

DML (DATA Manipulation Language)-


It includes commands to insert tuples into, delete tuples from and modify tuples in the
database.

View definition-
The SQL DDL includes commands for defining views.
Transaction Control- SQL includes for specifying the beginning and ending of
transactions.

Integrity-
The SQL DDL includes commands for specifying integrity constraints that the data
stored in the database must specify. Updates that violate integrity constraints are allowed.

Authorization-
The SQL DDL includes commands for specifying access rights to relations and views.

Data Definition Language-

The SQL DDL allows specification of not only a set of relations but also information
about each relation, including-
 Schema for each relation
 The domain of values associated with each attribute.
 The integrity constraints.
 The set of indices to be maintained for each relation.
 The security and authorization information for each relation.
 The physical storage structure of each relation on disk.

Domain types in SQL-

The SQL standard supports a variety of built in domain types, including-


Char (n)- A fixed length character length string with user specified length .
Varchar (n)- A variable character length string with user specified maximum
length n.
Int- An integer.
Small integer- A small integer.
Numeric (p, d)-A Fixed point number with user defined precision.
Real, double precision- Floating point and double precision floating point numbers with
machine dependent precision.
Float (n)- A floating point number, with precision of at least n digits.
Date- A calendar date containing a (four digit) year, month and day of the month.
Time- The time of day, in hours, minutes and seconds Eg. Time 09:30:00 .
Number- Number is used to store numbers (fixed or floating point).

SQL COMMANDS
1. COMMAND NAME: CREATE
COMMAND DESCRIPTION: CREATE command is used to create objects
in the database.
2. COMMAND NAME: DROP
COMMAND DESCRIPTION: DROP command is used to delete the object from the
database.
3. COMMAND NAME: TRUNCATE
COMMAND DESCRIPTION: TRUNCATE command is used to remove
all the records from the table
4. COMMAND NAME: ALTER
COMMAND DESCRIPTION: ALTER command is used to alter the structure of database
5. COMMAND NAME: RENAME
COMMAND DESCRIPTION: RENAME command is used to rename the objects.
QUERY: 01

Q1. Write a query to create a table employee with empno, ename, designation, and salary.

Syntax for creating a table:

SQL: CREATE <OBJ.TYPE><OBJ.NAME> (COLUMN NAME.1 <DATATYPE>


(SIZE), COLUMN NAME.1 <DATATYPE> (SIZE) ……………………………);

QUERY: 01

SQL>CREATE TABLE EMP (EMPNO NUMBER (4),ENAME VARCHAR2 (10),


DESIGNATIN VARCHAR2 (10),SALARY NUMBER (8,2));
Table created.

QUERY: 02

Q2. Write a query to display the column name and datatype of the table employee.
Syntax for describe the table:

SQL: DESC <TABLE NAME>;


SQL> DESC EMP;

Name Null? Type


----------------------------------------- -------- -------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)

QUERY: 03

Q3. Write a query for create a from an existing table with all the fields

Syntax For Create A from An Existing Table With All Fields

SQL> CREATE TABLE <TRAGET TABLE NAME> SELECT * FROM<SOURCE


TABLE NAME>;

QUERY: 03

SQL> CREATE TABLE EMP1 AS SELECT * FROM EMP;


Table created.

SQL> DESC EMP1


Name Null? Type
----------------------------------------- -------- ------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)

QUERY: 04

Q4. Write a query for create a from an existing table with selected fields

Syntax For Create A from An Existing Table With Selected Fields

SQL> CREATE TABLE <TRAGET TABLE NAME> SELECT EMPNO, ENAME


FROM <SOURCE TABLE NAME>;

QUERY: 04

SQL> CREATE TABLE EMP2 AS SELECT EMPNO, ENAME FROM EMP;


Table created.

SQL> DESC EMP2


Name Null? Type
----------------------------------------- -------- ----------------------
EMPNO NUMBER (4)
ENAME VARCHAR2 (10)

QUERY: 05

Q5. Write a query for create a new table from an existing table without any record:

Syntax for create a new table from an existing table without any record:

SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT * FROM


<SOURCE TABLE NAME> WHERE<FALSE CONDITION>;

QUERY: 05
SQL> CREATE TABLE EMP3 AS SELECT * FROM EMP WHERE
1>2;

Table created.

SQL> DESC EMP3;


Name Null? Type
----------------------------------------- -------- ------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);

ALTER & MODIFICATION ON TABLE

QUERY: 06
Q6. Write a Query to Alter the column EMPNO NUMBER (4) TO EMPNO NUMBER (6).

Syntax for Alter & Modify on a Single Column:

SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME><DATATYPE>


(SIZE);

QUERY: 06
SQL>ALTER TABLE EMP MODIFY EMPNO NUMBER (6);

Table altered.
SQL> DESC EMP;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)

QUERY: 07
Q7. Write a Query to Alter the table employee with multiple columns (EMPNO, ENAME.)

Syntax for alter table with multiple column:

SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME1><DATATYPE>


(SIZE), MODIFY <COLUMN NAME2><DATATYPE>
(SIZE)………………………………………….;

QUERY: 07

SQL>ALTER TABLE EMP MODIFY (EMPNO NUMBER (7), ENAME


VARCHAR2(12));
Table altered.

SQL> DESC EMP;


Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);

QUERY: 08
Q8. Write a query to add a new column in to employee
Syntax for add a new column:
SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME><DATA
TYPE><SIZE>);

QUERY: 08

SQL> ALTER TABLE EMP ADD QUALIFICATION VARCHAR2(6);


Table altered.
SQL> DESC EMP;

Name Null? Type


----------------------------------------- -------- ----------------------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)

QUERY: 09
Q9. Write a query to add multiple columns in to employee
Syntax for add a new column:
SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME1><DATA
TYPE><SIZE>,(<COLUMN NAME2><DATA
TYPE><SIZE>,………………………………………………………………);

QUERY: 09

SQL>ALTER TABLE EMP ADD (DOB DATE, DOJ DATE);


Table altered.
SQL> DESC EMP;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE
DOJ DATE

REMOVE / DROP

QUERY: 10
Q10. Write a query to drop a column from an existing table employee
Syntax for add a new column:
SQL> ALTER TABLE <TABLE NAME> DROP COLUMN <COLUMN NAME>;

QUERY: 10

SQL> ALTER TABLE EMP DROP COLUMN DOJ;

Table altered.
SQL> DESC EMP;
Name Null? Type
----------------------------------------- -------- -------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE

QUERY: 11
Q10. Write a query to drop multiple columns from employee
Syntax for add a new column:
SQL> ALTER TABLE <TABLE NAME> DROP <COLUMN NAME1>,<COLUMN
NAME2>,…………………………….. ;

QUERY: 11

SQL> ALTER TABLE EMP DROP (DOB, QUALIFICATION);


Table altered.

SQL> DESC EMP;


Name Null? Type
----------------------------------------- -------- --------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)

REMOVE

QUERY: 12
Q10. Write a query to rename table emp to employee
Syntax for add a new column:
SQL> ALTER TABLE RENAME <OLD NAME> TO <NEW NAME>

QUERY: 12

SQL> ALTER TABLE EMP RENAME EMP TO EMPLOYEE;


SQL> DESC EMPLOYEE;
Name Null? Type
----------------------------------------- -------- --------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
Practical List-1
1.Logon to Oracle by system/manager.

2.Create a user of your name & assign it a password.

3.Grant resource ,connect to a new user.

4.Disconnect from Oracle.

5.Connect as a new user.

6.See list of tables by using”Tab / user_tables /cat” tables.

7.View the user name from dual.

8.Clear the screen.

9.Perform some mathematical operations by using dual.(Addition,multiplicationetc)

10.Create a table student with following attributes:

Roll_No number 6

Name varchar 15

City varchar 12

Pincode varchar 8

Age number 2

11.Display the structure of table.

12.Insert the following data into the table.

Roll_no Name City Pincode Age

1 Amit Delhi 214213 21

2 Kumar Bombay 214101 25

3 Kahitij Madras 110101 16

4 Puneet Calcutta 1010107 18

5 Rohit Delhi 1102207 19


6 Karnal Dehradun 101301 28

7 Shweta Dehradun 98102 23

8 Mohit Bombay 12434 22

9 Pankaj Bombay 98632 17

10 Parul Gwaliar 76312 24

11 Rohit Barodra 12131 29

13. Show all records.


14. Show names of all students living in Delhi.
15. Show record of all students whose name starts with “A”.
16. Show record of all students whose name ends with “it”.
17. Show records of all students having age greater than 25 & living in Dehradun.
18. Show the names of all cities(names of cities should not be repeated).
19. Show the names of students alphabetically in ascending order.
20. Show the records of those students which are either less than 20 year or they are
living in Bombay.
21. Show records of all those students who are not living in Dehradun.
22. .Insert the following data further into the same table.

12 Gaurav Rampur 312125 NULL


NULL Manish NULL 314136 NULL
14 Aviral NULL 319143 29
15 NULL Gwaliar 313149 25

23. Assign Roll_no “13” to Manish.


24. Assign the name “Abhijeet” to Roll_no 15.
25. Set the cities of Roll_no.13 & 14 to Meerut.
26. Increase all age by 3 years.
27. Set the age of all students living in Meerut to “25”.

You might also like