Practical File OF SQL: MKM College of Management & Information Technology For Girls, Hodal
Practical File OF SQL: MKM College of Management & Information Technology For Girls, Hodal
Practical File OF SQL: MKM College of Management & Information Technology For Girls, Hodal
PRACTICAL FILE
OF
SQL
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
is:
(<column specification>,
.................................................)
Example:
2. DESCRIBE STATEMENT
Example:
● Adding new
columns: Syntax:
5
Add<column
specification>,
<column specification>,.......);
Example:
: Syntax:
Example :
Table: Syntax:
Alter Table<table
name> Drop<column
name>; Example:
Drop name;
7
Syntax:
Example:
5. INSERT COMMAND
Syntax:
Values(value1,value2,.......
Example:
columns Syntax:
Example:
8
Substitution Example:
6. UPDATE COMMMAND
Syntax:
update<table name>Set
………
]
9
Example:
7. DELETE COMMAND
Syntax:
[where <condition(s)>];
Example:
rows Syntax:
Delete<table name>;
11
Example:
8. SELECT COMMAND
Syntax:
Select
[distinct]<columns(s)>
From<table name>
[where<condition>]
● Selecting all
columns Syntax:
Example:
12
● Selecting
columns Syntax:
Example:
distinct) Syntax:
Example:
Clause Syntax:
Select
<columns(s)>
From<table name>
13
[Where <condition>];
14
Example:
● Sorting data in a
table Syntax:
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
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:
On emp
From shivank;
11.COMMIT COMMAND
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:
Syntax:
Savepoint<savepoint _name>;
Example:
18
14.CONSTRAINTS:
Syntax:
Example:
● Unique constraint
It specifies that values of two rows for a specified column must be unique or
different.
Syntax:
Example:
It specifies one or more attribute that make a primary key of the table.
Syntax:
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
Example:
20
Example:
● Table constraint
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.
● SELECT
● INSERT
● UPDATE
● DELETE
Syntax:
As <select statement>;
Example:
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
If we include with read only clause in the end of view definition view
automatically becomes non updatable.
Example:
Example:
23
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)
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
Syntax:
Abs(x)
Example:
● Power(m, n)
Syntax:
Power(m,n)
Example:
● Sqrt(x)
Syntax:
Sqrt(x)
Example:
c. String Functions :
● Lower(string)
Example:
27
● Upper(string)
Example:
● Inticap(string)
This function returns the string with the first letter in caps.
Example:
28
● Length(x)
Example:
● Concat(x1,x2)
Example: