0% found this document useful (0 votes)
36 views38 pages

WSE2007 ELA AP2023246000731 2024-01-23 Reference-Material-I

SQL is a standard language used to store, manipulate and retrieve data in relational databases. It allows users to query the database in several ways using English-like statements. SQL commands are used to communicate with the database and perform tasks like creating tables, adding data, modifying data, and retrieving data.

Uploaded by

edrickwitch01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views38 pages

WSE2007 ELA AP2023246000731 2024-01-23 Reference-Material-I

SQL is a standard language used to store, manipulate and retrieve data in relational databases. It allows users to query the database in several ways using English-like statements. SQL commands are used to communicate with the database and perform tasks like creating tables, adding data, modifying data, and retrieving data.

Uploaded by

edrickwitch01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

SQL Basics

Dr. Selva Kumar S


SCOPE,
VIT AP University
What is SQL?

SQL stands for Structured Query Language. It is used for storing and
managing data in Relational Database Management System (RDBMS).

It is a standard language for Relational Database System. It enables a


user to create, read, update and delete relational databases and tables.

All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL
Server use SQL as their standard database language.

SQL allows users to query the database in several ways, using English-
like statements.
What are the SQL?

SQL follows the following rules:


Structure query language is not case sensitive. Generally, keywords of SQL
are written in uppercase.

Statements of SQL are dependent on text lines. We can use a single SQL
statement on one or multiple text lines.

Using the SQL statements, you can perform most of the actions in a
database.

SQL depends on tuple relational calculus and relational algebra. case-


sensitive
What is SQL Process?
When an SQL command is executing for any RDBMS, then the system
figures out the best way to carry out the request and the SQL engine
determines how to interpret the task.

In the process, various components are included. These components can


be optimization Engine, Query engine, Query dispatcher, classic, etc.

All the non-SQL queries are handled by the classic query engine, but
SQL query engine won't handle logical files.
What is SQL Process?
What Are the Advantages of SQL?

High speed

No coding needed

Well defined standards

Portability

Interactive language

Multiple data view


What is SQL Datatype?
SQL Datatype is used to define the values that a column can contain.

Every column is required to have a name and data type in the database
table.
What is SQL Datatype?
SQL Keywords
SQL Commands

SQL commands are instructions. It is used to communicate with


the database. It is also used to perform specific tasks, functions,
and queries of data.
SQL can perform various tasks like creating a table, add data to
tables, drop the table, modify the table, set permission for users.
Types of SQL Commands
There are five types of SQL commands: DDL, DML, DCL,
TCL, and DQL.
Data Definition Language (DDL)
DDL changes the structure of the table like creating a table, deleting a
table, altering a table, etc.

All the commands of DDL are auto-committed which means it


permanently saves all the changes in the database.

Here are some commands that come under DDL:

CREATE

ALTER

DROP

TRUNCATE
Data Definition Language (DDL)
CREATE
DROP
ALTER
TRUNCATE
RENAME

Create : 1. For creating a database


Syntax CREATE DATABASE database_name

Example CREATE DATABASE VTOP


2. For creating a Table
Syntax CREATE TABLE table_name ( column1 datatype, column2 datatype, ..... columnN datatype );
Example CREATE TABLE my_table( first_name varchar(20),age INT);

3. For creating a Table by using As statement


create a table using another table.
CREATE TABLE NewTableName AS SELECT Column1, column2,..., ColumnN FROM ExistingTableName
CREATE TABLE new_tb( first_name ,age) from my_table ;
Data Definition Language (DDL)

DROP :
This statement is used to drop an existing table or a database.

Syntax DROP DATABASE DatabaseName;


DROP TABLE TableName;

TRUNCATE :

This command is used to delete the information present in the table but does not delete the table.
So, once you use this command, your information will be lost, but not the table.

Syntax: TRUNCATE TABLE TableName;


Data Definition Language (DDL)

ALTER :
This command is used to delete, modify or add constraints or columns in an existing table.

Syntax ALTER TABLE TableName ADD ColumnName Datatype;

ALTER TABLE TableName DROP COLUMN ColumnName;;

BACKUP DATABASE :

This statement is used to create a full backup of an existing database..


Syntax: BACKUP DATABASE DatabaseName TO DISK = 'filepath';;
Data Manipulation Language
DML commands are used to modify the database. It is responsible for all
forms of CHANGES in the database.

The command of DML is not auto-committed which means it can't


permanently save all the changes in the database. They can be rolled back.

Here are some commands that come under DML:


INSERT

UPDATE

DELETE
SQL Commands: Data Manipulation Language Commands
(DML)
INSERT
Insert command is used to insert data into a table.
Syntax
Insert into <table_name> (column list) values (column values);

Insert into my_table(first_name,age


(or)

Insert into my_table

UPDATE
The Update command is used to update existing data within a table.
Syntax
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

UPDATE Customers SET ContactName ssk CustomerID = 1;


SQL Commands: Data Manipulation Language Commands
(DML)

DELETE
This statement is used to delete the existing records in a
table.

Syntax
DELETE FROM TableName WHERE Condition;

Example

DELETE FROM Customers WHERE CustomerName ssk';


Data Control Language
DCL commands are used to GRANT and TAKE BACK authority from any
database user.

Here are some commands that come under DCL:


Grant
Revoke
Data Control Language - Grant
GRANT: It is used to give user access privileges to a database.

Example:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOT HER_USER;

REVOKE: It is used to take back permissions from the user.

Example:

REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;


Transaction Control Language
TCL commands can only be used with DML commands like INSERT,
DELETE and UPDATE.

These operations are automatically committed in the database that's why


they cannot be used while creating tables or dropping them.
Here are some commands that come under TCL:
COMMIT

ROLLBACK

SAVEPOINT
Transaction Control Language - COMMIT

Commit:
Commit command is used to save all the transactions to the database.

Syntex:

COMMIT;

Example:
DELETE FROM CUSTOMERS WHERE AGE = 25;
COMMIT;
Transaction Control Language - Rollback
Rollback:
Rollback command is used to undo transactions that have not already been saved to the
database.
Syntex:
ROLLBACK;
Example:
DELETE FROM CUSTOMERS WHERE AGE = 25; ROLLBACK;

SAVEPOINT:
It is used to roll the transaction back to a certain point without rolling back the
entire transaction.
Syntex:
SAVEPOINT SAVEPOINT_NAME;
Data Query Language
DQL is used to fetch the data from the database.

It uses only one command:


SELECT
This is the same as the projection operation of relational algebra.

It is used to select the attribute based on the condition described by the WHERE clause.

Syntax:
SELECT expressions FROM TABLES WHERE conditions;
Example:
SELECT emp_name FROM employee WHERE age > 20;
Data Query Language

Apart from just using the SELECT keyword individually, you can use the
following keywords with the SELECT statement:

DISTINCT

ORDER BY

GROUP BY

HAVING Clause

INTO
SQL Commands: Data Manipulation Language Commands
(DML)

SELECT: Retrieves data from one or more tables.

FROM: Specifies the table from which you're retrieving the data.

WHERE: Filters the results based on a condition.

GROUP BY: Groups rows that have the same values in specified columns.

HAVING: Filters the result of a GROUP BY.

ORDER BY: Sorts the results in ascending or descending order.

JOIN: Combines rows from two or more tables based on related columns.
SQL Commands: Data Manipulation Language Commands
(DML)
SELECT * FROM tablename;
SELECT column1, column2 FROM tablename;
SELECT column1, column2 FROM tablename WHERE
SELECT column1, column2 FROM tablename ORDER BY column1 ASC;

Regular expressions in the SELECT Command


LIKE operator
LIKE: The LIKE operator is used in a 'WHERE' clause to search for a specified pattern in a column

wild-card: There are two primary wildcards used in conjunction with the `LIKE` operator.

percent sign (%) Represents zero, one, or multiple characters

underscore sign(_) Represents a single character

SELECT column_name FROM table_name WHERE column_name LIKE 'pattern%';

SELECT FirstName FROM Customers WHERE FirstName LIKE 'Ma%';


SQL Commands: Data Manipulation Language Commands
(DML)
Find values that have a specific pattern anywhere

SELECT column_name FROM table_name WHERE column_name LIKE

Example:
SELECT BookTitle FROM Books WHERE BookTitle LIKE '%life%';

Using `_` Wildcard


Find values of a specific length where you only know some characters:

SELECT column_name FROM table_name WHERE column_name LIKE 'p_ttern ;

Example:

SELECT Word FROM Words WHERE Word LIKE 'h_l__';


SQL Commands: Data Manipulation Language Commands
(DML)

The GROUP BY statement groups rows that have the same values into
summary rows, like "find the number of customers in each city.

Syntax:
SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s)

Example:

SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ORDER


BY COUNT(CustomerID) DESC;
SQL Operator

Arithmetic Operators

Comparison Operators

SQL Operator
Compound Operators

Logical Operators

Bitwise Operators
SQL Operators
SQL Arithmetic Operators
SQL Operators
SQL Comparison Operators
SQL Operators
SQL Compound Operators
SQL Operators
SQL Logical Operators
SQL Operators
SQL Bitwise Operators
Example:
SQL> CREATE TABLE EMPLOYEE ( EMP_ID INT NOT NULL, EMP_NAME VARCHAR (25) NOT NULL, PHONE_NO INT ADDRESS CHAR (30),
PRIMARY KEY (ID) );

DESC EMPLOYEE;
DELETE FROM table_name WHERE condition
DROP TABLE "table_name";
SELECT * FROM table_name;
INSERT INTO TABLE_NAME VALUES (value1, value2, value 3, .... Value N);
INSERT INTO TABLE_NAME[(col1, col2, col3,.... col N)] VALUES (value1, value2, value 3, ... Value N);
UPDATE table_name SET column_name = value WHERE condition;
Example:
UPDATE table_name SET column_name = value1, column_name2 = value WHERE condition;

DELETE FROM table_name WHERE some_condition;


Exercises
1. Create Table Employee with attributes firstName, LastName, SSN, Address, Salary, Birthday, Sex, SupervisorSSN, DepartmentNo.

2. Create a Table Department with attributes DNo, DNAMe, ManagerSSN, MgrStartdate.

3. Insert the data given above in both employee, department and project tables.

4. Display all the information.

5. Display Employee name along with his SSN and Supervisor SSN.

6. Display the employee names whose bdate is 29-MAR-1959 .

7. Display salary of the employees without duplications.

8. Display the MgrSSN, MgrStartDate of the manager of department.

9. Modify the department number of an employee having fname as to 5

10. Alter Table department add column DepartmentPhoneNum of NUMBER data type and insert values into this column only.

11. Alter table department to modify the size of DepartmentPhoneNum.

12. Modify the field name DepartmentPhoneNum of departments table to PhNo.

13. Rename Table Department as DEPT.

You might also like