Structured Query Language

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

INTRODUCTION

TO

SQL
Structured Query Language
Business need Database

“An organized collection of information”


OR
“Set of tables which have different class of data”
WHAT IS SQL?

 Structured Query Language.

 A language to communicate with the database.

 A tool to store, manipulate ad retrieve the data stored in relational database.

IBM implemented the language, originally it was called as


SEQUEL(Structured English Query Language).

 The syntax in plain English to question the database.

 The first Relational database was released by Relational Software later becoming
Oracle.
CONCEPT OF SQL
 The user specifies a certain condition.

 The program will go through all the records in the database file and select those records that
satisfy the condition.

 The result of the query will then be stored in form of a table.

 Perform CRUD function:

CREATE New databases


New tables in a database
Records in a database

READ Retrieve data from a database

UPDATE Update records in a database

DELETE Delete records from a database


BASICS OF DATABASE
Columns

Table

Rows
Keys

Primary Key
Primary Keys are how every row in the table is searchable.
They can be a single column or a combination of columns that
make up a unique identification number.

Foreign Key
Foreign Keys are used to link tables together within a database.
These links are called relationships.
r y key
a
Prim

COURSEID COURSENAME FACULTYID

101 JAVA 9
Tuple 101 JAVA 9

102 AMQ 9
Relation
Cardinality
103 SQL 2

104 GROOVY 2

Attributes

Degree
SQL COMMANDS

SQL commands are mainly categorized into four categories


as:

1. DDL- Data Definition Language


2. DQL-Data Query Language
3. DML-Data Manipulation Language
4. DCL-Data Control Language
5. TCL-Transaction Control Language
DDL Statement

Data Definition Language actually consists of the SQL commands that


can be used to define the database schema.

List of DDL commands: 

• CREATE: This command is used to create the database or its objects.

• DROP: This command is used to delete objects from the database.

• ALTER: This is used to alter the structure of the database.

• TRUNCATE: This is used to remove all records from a table,


including all spaces allocated for the records are removed.

• COMMENT: This is used to add comments to the data dictionary.

• RENAME: This is used to rename an object existing in the database.


CREATE A TABLE

Syntax: NAME NULL? TYPE


----------------------- ------------------ ----------------------
CREATE TABLE table_name ROLL_NO NOT NULL INT(3)
NAME NOT NULL VARCHAR(20)
( column1 data_type(size), SUBJECT NOT NULL VARCHAR(20)
column2 data_type(size),
column3 data_type(size),
.... );

Example Query:
This query will create a table named Students with three columns, ROLL_NO, NAME and SUBJECT.

CREATE TABLE Students


( ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20) );
TRUNCATE

Removes all rows from the table

Restriction:
You cannot truncate the table if it is linked with another table

Syntax:
TRUNCATE TABLE<Table_name>;

Example:
TRUNCATE TABLE Customer;
DROP

Drops the entire table structure

Syntax:
DROP TABLE<Table_name>;

Example:
DROP TABLE Customer;
TRUNCATE VS DROP

Table Table after truncate Table after drop

In truncate only the data is removed,


Whereas,
in drop the entire structure is removed.
DML: Data Manipulation Language
[Defines the Data of Table]

INSERT Syntax for Insert:

UPDATE INSERT INTO TableName [(column 1,column 2….)]


VALUES (value1, value2…..);
DELETE

SELECT
Example:

INSERT INTO Customer (Id, Cname, phoneno, address)


VALUES (2, ‘Mini’, 9923412345, ‘Chennai’);
UPDATE ROWS IN ATABLE

UPDATE TableName SET ColumnName=value[ColumnName=value,


…..] [WHERE condition];

UPDATE Customer SET


Modify existing rows with emailid=‘Tiny15@gmail.com’ WHERE
the UPDATE statement. cname=‘Tiny’;

All rows in the table are


UPDATE Customer SET
modified if we omit the
emailid=‘Tiny15@gmail.com’;
WHERE clause.
DELETE ROWS FROM TABLE
DELETE [FROM] table [ WHERE condition];

Remove existing rows from a DELETE FROM Customer WHERE


table by using the DELETE CId=2;
statement.

To delete all rows from


DELETE FROM Customer;
the table.

Deletion is not possible if the row to be deleted is referred in the child table.

Deletion of the parent record is made possible by using a foreign key reference option.
DELETE VS TRUNCATE

DELETE Deletes all rows or a specific row from the table.

Can be reverted to its previous state.

Rows that are referred in the child table cannot be removed.

TRUNCATE Removes all rows from the table.

Cannot be reverted.

Will not work if the truncated table is referenced.


DATABASE TRANSACTION Oh no! When I rolled-
I understand, actually
back, all the
you should give a
manipulation is lost. I
check point in between
I deleted a record by Don’t worry. You want only the deletion to
transactions, and the
mistake. Can I get can retrieve the get rolled-back. What
Roll-back will happen
the record back? data, unless you should I do?
until the needed check
haven’t given the
point.
‘commit’ statement.

Reverting the data is performed using Reverting the data until the check point
ROLLBACK. is done by save point.
DATABASE TRANSACTION

A transaction is a logical unit of work.

Transaction begins when DML statement is executed.

Transaction should end with either Commit or Rollback.

DDL commands are by default auto commit.

COMMIT Ends the current transaction by making all pending data changes permanent.

SAVEPOINT Marks a save point within the current transaction.


Savepoint_name
ROLLBACK Ends the current transaction by discarding all pending data changes.

ROLLBACK TO This rolls back the current transaction to the specified savepoint, thereby discarding any
Savepoint_name
changes or savepoints created after the savepoint to which you are rolling back.
BASIC STRUCTURE OF A SQL QUERY

GENERAL STRUCTURE SELECT,ALL/DISTINCT,*,AS,FROM,WHERE

COMPOSITION IN,BETWEEN,LIKE,”%_”

GROUPING GROUP BY,HAVING,COUNT(),SUM(),AVG(),MAX(),MIN()

DISPLAY ORDER ORDER BY,ASC/DESC

LOGICAL OPERATORS AND,OR,NOT

OUTPUT INTO TABLE/CURSORE TO FILE[ADDICTIVE],TO


PRINTER,TO SCREEN

UNION UNION

You might also like