SQL Lab Prerequisite Basic of Sql and query
SQL Lab Prerequisite Basic of Sql and query
SQL stands for Structured Query Language. SQL is used to create, remove, alter the
database and database objects
in a database management system and to store, retrieve, update the data in a database.
SQL is a standard language
for creating, accessing, manipulating database management system. SQL works for all
modern relational database
management systems, like SQL Server, Oracle, MySQL, etc.
SQL commands can be categorized into five categories based on their functionality
SQL commands can be categorized into five categories based on their functionality
);
Suppose, you want to create a Student table with five columns in the SQL
database. To do this, you have to write the following DDL command:
Example 1:
CREATE TABLE Student
(
Roll_No. int ,
First_Name varchar (20) ,
Last_Name varchar (20) ,
Age int ,
Marks int,
Dob Date
);
SQL>desc Student;
Example 2:
create table Employee
(
empid varchar(10),
empname varchar(20) ,
gender varchar(7),
age number(3),
dept varchar(15) ,
doj Date
);
SQL> desc Employee
Example 3:
create table BOOK
(
Book_id varchar(4),
Title varchar(10),
Publisher_name varchar(10),
Pub_year int
);
SQL> desc BOOK;
2. ALTER
This command is used to add, delete or change columns in the existing table. The
user needs to know the existing table name and can do add, delete or modify tasks
easily.
Syntax: –
.ADD:
SQL> alter table employee add(designation varchar(15));
Table altered.
II.MODIFY
SQL> alter table employee modify (designation varchar(20));
Table altered
Example 1:
ALTER TABLE Student
ADD CGPA number;
SQL>desc Student;
Example 2:
ALTER TABLE Employee
ADD Salary number;
SQL>desc Employee;
Example 3:
ALTER TABLE BOOK
ADD Author_nmae varchar(20);
SQL>desc Student;
3. RENAME:
It is possible to change name of table with or without data in it using simple
RENAME command.
We can rename any table object at any point of time.
Syntax –
RENAME <Table Name> To <New_Table_Name>;
Example:
RENAME TABLE Employee To EMP;
4. TRUNCAT:
This command is used to remove all rows from the table, but the structure of the
table still exists.
Syntax –
Syntax to remove an existing table.
Example:
TRUNCATE TABLE Student;
5. DROP
This command is used to remove an existing table along with its structure from
the Database.
Syntax –
Syntax to drop an existing table.
DROP TABLE table_name;
Example: DROP TABLE Student_info;
DML(DATA MANIPULATION LANGUAGE):
Data manipulation language allows the users to query and manipulate data in
existing schema in object.
It allows following data to insert, delete, update and recovery data in schema
object.
DML COMMANDS:
❖ INSERT
❖ UPDATE
❖ DELETE
1. INSERT
This command is used to enter the information or values into a row. We can
connect one or more records to a single table within a repository using this
instruction.
Syntax:
Insert into Table_ Name Values(column1, column2, ....);
Example:
CREATE TABLE Student
(
Roll_No int ,
First_Name varchar (20) ,
Last_Name varchar (20) ,
Marks int,
Dob Date
);
SQL>desc Student;
SQL> insert into Student values(‘01’,’Adit,’k’’,25,’11-02-2004’);
SQL> insert into Student values(02,“Arpitha”,”S”, 20,’21-12-2003’);
SQL> insert into Student values(03,“Jorge”,”D”, 20, 18,’10-08-2001’);
Insert 2 more rows.
SQL>desc Student;
2. UPDATE
This allows the user to update the particular column value using the where
clause condition. This command is used to alter existing table records.
Syntax:
UPDATE <table_ name>
SET <column_ name = value>
WHERE condition;
Example:
UPDATE Students
SET Marks= 21
WHERE First_ name = “Arpitha”;
SQL>desc Students;
3. DELETE
a) Delete some rows
DELETE statement is used to delete rows from a table. Generally DELETE
statement removes one or more records from a table.
Syntax:
DELETE FROM table_name WHERE condition;
Example:
Delete from Students where Roll_no=’111’;
Example:
Select * from Students;
SQL> desc Students;
Example:
SELECT DISTINCT Roll_No FROM Students;
Example:
SELECT * FROM students
WHERE students_name IN ( Arpitha, Jorge);
6. Renaming:
The select statement can be used to rename either a column or the entire table.
Syntax:
Renaming a column:
SELECT column name AS new name FROM table_name;
Example:
Renaming a table:
Example:
RENAME Student TO Stu_details;
7. SELECT DATE
It is used to retrieve a date from a database. If you want to find a particular date
from a database, you can use this statement.
Syntax:
SELECT Column_Names(S) from table-name
WHERE condition(date_column);
8. SELECT NULL
Null values are used to represent missing unknown data.
There can be two conditions:
Syntax:
SELECT COLUMN_NAME(S) FROM TABLE_NAME
WHERE COLUMN_NAME IS NOT NULL;
Example:
SELECT Student_name, Marks FROM STUDENTS
WHERE MARKS IS NOT NULL;
ORDER BY Clause
ORDER BY is a clause in SQL which shows the result-set of the SELECT statement
in either ascending or descending order.
a) with one row
Syntax:
SELECT Column_Name FROM Table_Name
ORDER BY Column_Name;
Example:
SELECT * FROM Students ORDER BY Reg_no;
GROUP BY clause
GROUP BY is an SQL keyword used in the SELECT query for arranging the same
values of a column in the group by using SQL functions.
Syntax:
SELECT Column_Name(S) FROM Table_Name
GROUP BY Column_Name(S);
Example:
SELECT COUNT (marks), Student_name GROUP BY marks;
c) COUNT() Function
The COUNT() function returns the number of rows in a database table.
Syntax:
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
Example:
1. SELECT COUNT(*)
FROM Student;
2. SELECT COUNT(Marks)
FROM Student
GROUP BY marks
HAVING COUNT(*) > 50;
d) SUM() clause
It is used to return the total summed value of an expression.
Syntax:
SELECT SUM(aggregate_expression)
FROM tables
[WHERE conditions];
Example:
SELECT SUM(Marks)
FROM Student
Where roll_no=’111’;
DCL (DATA CONTROL LANGUAGE)
DCL stands for data control language. DCL commands are used
for providing and taking back the access rights on the database
and database objects. DCL command used for controlling user’s
access on the data. Most used DCL commands are GRANT and
REVOKE
GRANT
used to provide access right to the user.
REVOKE
REVOKE command is used to take back access right from the
user, it cancels access right of the user from the
database object.
Syntax
REVOKE ALL ON Employee FROM user;
COMMIT
COMMIT command is used to save or apply the modification in the
database.
ROLLBACK
ROLLBACK command is used to undo the modification.
SAVEPOINT
SAVEPOINT command is used to temporarily save a transaction,
the transaction can roll back to this point when it's needed.
Syntax :
Just write COMMIT or ROLLBACK or SAVEPOINT