You are on page 1of 28

1

MKM COLLEGE OF MANAGEMENT &


INFORMATION TECHNOLOGY FOR
GIRLS, HODAL
(AFFILIATED TO M.D.U
ROHTAK) (SESSION (2022-2023)

PRACTICAL FILE
OF
SQL

SUBMITTED TO : SUBMITTED BY:-


Mrs. Anamika Aggarwal NAME : Mani Devi
CLASS- MCA 1st Year
Roll No .
2

INTRODUCTION TO SQL
Structured query language (SQL) is a powerful and simple language used to create
access and manipulate data and structure in the database. SQL is like Plain English and
easy to understand and to write.

SQL provides a small and coincise set of commands. This helps us to save time and
reduce the amount of programming required to perform Complex queries.

COMPONENTS OF SQL
There are three components of SQL :

1. Data is used to define a database, modified structure after it has been created
and destroyed it after no longer needed. DDL commands are :
● CREATE TABLE
● CREATE VIEW
● ALTER TABLE
● DROP TABLE
2. Data Manipulation language (DML)

DML is used to manipulate data such as insert, modify, delete or query the data
in the database. DML commands are:

● INSERT
● DELETE
● UPDATE
● SELECT
3. Data control language (DCL)
DCL is used to grant and revoke author authorisation for database access auditing
the Database used and dealing with the transactions. DCL commands are:

● COMMIT
● ROLLBACK
● GRANT
● REVOKE
3

COMMANDS OF SQL
1. CREATE TABLE STATEMENT

This command is used to create table.

The syntax of create table command

is:

CREATE TABLE<table name>

(<column specification>,

.................................................)

Example:

2. DESCRIBE STATEMENT

To display the structure of the table we can use describe statement.

Syntax: >desc<table name>

Example:

3. ALTER TABLE STATEMENT

It allows changing/modifying the structure of an existing table.


4

Commands of alter table are as follows:

● Adding new

columns: Syntax:
5

Alter Table<table name>

Add<column

specification>,

<column specification>,.......);

Example:

● Modifying Existing Columns

: Syntax:

Alter Table<table name>

Modify<column name><new details for the column>);

Example :

● Dropping A Column From A

Table: Syntax:

Alter Table<table

name> Drop<column

name>; Example:

Alter table student


6

Drop name;
7

4. DROP TABLE STATEMENT

Tables may be destroyed with the SQL command DROP TABLE.

Syntax:

Drop Table<table name>;

Example:

5. INSERT COMMAND

It is used to insert a row into a table.

Syntax:

Insert into<table name>

Values(value1,value2,.......

Example:

● Insert Data in Specified

columns Syntax:

Insert into< table name>(column 1, column2,.....)

values (value1, value2,.....)

Example:
8

● Inserting Through Parameter

Substitution Example:

6. UPDATE COMMMAND

It is used to modify or change some or all the values in an existing table.

Syntax:

update<table name>Set

<column i> = <new value>

[<column j> = <new value>

………

]
9

[where <condition >];


10

Example:

7. DELETE COMMAND

Rows of a table may be deleted using a delete command.

Syntax:

Delete from <table name>

[where <condition(s)>];

Example:

● Deletion of all the

rows Syntax:

Delete<table name>;
11

Example:

8. SELECT COMMAND

It is used to view or retrieve the information already stored in the database.

Syntax:

Select

[distinct]<columns(s)>

From<table name>

[where<condition>]

[order by<column (s) [asc|desc]>];

● Selecting all

columns Syntax:

Select * from <table name>;

Example:
12

● Selecting

columns Syntax:

Select <columns(s)>from<table name>;

Example:

● Eliminating redundant data (with keyword

distinct) Syntax:

Select distinct< columns(s)> from <table name>;

Example:

● Selecting specific rows - where

Clause Syntax:

Select

<columns(s)>

From<table name>
13

[Where <condition>];
14

Example:

● Sorting data in a

table Syntax:

Select * from<table name>

Order by <column (s) [asc|desc]>];

Example:

9. GRANT COMMAND

Grant command iis used to provide permissions like Select, All, Execute to user
on the database objects like Tables, Views, Databases and other objects in a SQL
Server.

Syntax:

Grant(privileges)

On objectname

To username

[with grant option];


15

Example:

Grant

all On

emp

To Shivank;

10.REVOKE COMMAND

Privileges ones granted can be revoked later if needed by using the revoke
command.

Syntax:

Revoke ( privileges

) On objectname

From username;

Example:

Revoke update, insert

On emp

From shivank;

11.COMMIT COMMAND

It is the transactional command used to save the data in the database.

Syntax:

Commit

[Work];

Example:
16

12.ROLLBACK COMMAND

It is used to undo transactions that have not already been saved to the database.

Syntax:

Rollback [Work];

Example:

13. SAVEPOINT COMMAND


The statement gives a name to and marks a point in the processing of
current transaction. This marker allows you to Rollback to that point.
Hence, savepoint are spacial operations that allow you to divide the work of
a transactional into different segments.
17

Syntax:

Savepoint<savepoint _name>;

Example:
18

14.CONSTRAINTS:

● Not Null constraint

It specifies that value of column cannot be null.

Syntax:

[constraint <name>]Not Null

Example:

● Unique constraint

It specifies that values of two rows for a specified column must be unique or
different.

Syntax:

Create table< tablename> data type (size) unique;

Example:

● Primary key constraint

It specifies one or more attribute that make a primary key of the table.

Syntax:

Create table< tablename> data type (size) primary key;


19

Example:

● Check constraint

Check constraints all or Oracle to verify the validity of data being entered on a
table against a set of constant values.

Syntax:

● Default constraint

It is used to define a default value for an attribute.

Example:
20

● Foreign key constraint

It is used to enforce integrity between tables in a relational database. A column


defined as foreign key is used to reference a column defined as a primary key in
another table.

Example:

● Table constraint

It is used when a constraint is to be applied on a group of columns of the table.

Example:
21

15. VIEWS

A view is a virtual table with no data. It is like a window through which you can
view the data of another table, which is called the base table.

Sql commands to refer the view:

● SELECT
● INSERT
● UPDATE
● DELETE

Syntax:

Create view<view name>[<view column name>]

As <select statement>;

Example:

● Changing column names

If any column in the view is to be given a different name other than the name of
the column from which it is derived, it is done by specifying the new column
names.

Example:
22

● Read only view

If we include with read only clause in the end of view definition view
automatically becomes non updatable.

Example:

● Group function in view

Beautiful Nations can contain functions and group by clauses.

Example:
23

16.Inbuilt SQL functions


a. Multiple Row Functions :
● Count(x)

This function returns the number of rows or non null values for column x.

Syntax:

Count([distinct |all]<x>)

Example:

● Count(*)

This function returns the number of rows in the table including duplicates and
those with nulls.

Syntax:

Count(*)

Example:

● Sum(x)

This function returns the sum of values for the column x.

Syntax:

Sum([distinct |all]<x>)

Example:
24

● Avg(x)

This functions return the average of values for the column X , ignoring null
values in the column.

Syntax:

Avg([distinct |all]<x>)

Example:

● Min(x)

This function returns the minimum of values for the column x for all the rows.

Syntax:

Min([distinct |all]<x>)

● Max(x)

This function returns the maximum of values for the column Max for all the rows.

Syntax:

Max([distinct |all]<x>)
25

Example of Min & Max:

b. Single Row Functions :


● Abs(x)

This function returns the absolute value of x.

Syntax:

Abs(x)

Example:

● Power(m, n)

This function returns a number M raised to the nth power.


26

Syntax:

Power(m,n)

Example:

● Sqrt(x)

Return square root of n. If n < 0 or null then the result is Null.

Syntax:

Sqrt(x)

Example:

c. String Functions :
● Lower(string)

This Function converts the string into lower case.

Example:
27

● Upper(string)

This function convert the string into uppercase.

Example:

● Inticap(string)

This function returns the string with the first letter in caps.

Example:
28

● Length(x)

It returns the length of string x.

Example:

● Concat(x1,x2)

This function concatenates tge string x1 with x2.

Example:

You might also like