Structured Query Language
Introduction
The Structured Query Language (SQL) is a non-procedural programming
language used for storing, manipulatingand retrieving the data in a databases.
SQL was the first commercial language introduced for E.F
Codd's Relational model.
Data Definition Language
It is used to define the structure of tables in the database.
CREATE, ALTER, USE, DROP, TRUNCATE COMMAND
Data Manipulation Language
It is used for managing data in database.
SELECT, UPDATE, DELETE, INSERT COMMAND
Data Control Language
It is used to control user access in a database.
GRANT, REVOKE COMMAND
Grant: Gives user access privileges to database.
Revoke: Take back permissions from user.
It is used to manage transactions in database.
COMMIT, ROLLBACK, SAVEPOINT COMMAND
The COMMIT command is the transactional command used to save changes
invoked by a transaction to the database.
The ROLLBACK command is the transactional command used to undo
transactions that have not already been saved to the database.
The SAVEPOINT command is used to temporarily save a transaction so that
you can rollback to that point whenever necessary.
SQL – CREATE DATABASE:
Syntax:
SQL> CREATE DATABASE database_name;
Example:
SQL> CREATE DATABASE STUDENT_DB;
SQL> SHOW DATABASES;
SQL – SELECT DATABASE:
The SQL USE statement is used to select any existing database in the SQL
schema.
SQL> USE database_name;
SQL – DROP DATABASE:
This statement is used to drop an existing SQL Database.
Syntax:
SQL> DROP DATABASE database_name;
Example:
SQL> DROP DATABASE STUDENT_DB;
SQL – CREATE TABLE:
Syntax:
SQL> CREATE TABLE table_name(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size)
);
Example:
SQL> CREATE TABLE student(
Student_ID int,
Student_namevarchar (255),
City varchar (255),
Marks int
);
SQL – ALTER TABLE:
TO ADD COLUMN
Syntax:
ALTER TABLE table_name ADDcolumn_name datatype;
TO DROP COLUMN
Syntax:
ALTER TABLE table_name DROP COLUMNcolumn_name;
TO ADD NOT NULL
Syntax:
ALTER TABLE table_name MODIFYcolumn_name datatype NOT NULL;
TO ADD PRIMARY KEY
Syntax:
ALTER TABLE table_name ADDPRIMARYKEY(column);
How to check whether a table exists & the property or description of a table?
Syntax:
SQL> DESC table_name;
Example:
SQL> DESC student;
SQL – DROP TABLE:
This statementis used to drop an existing table in database.
SQL DROP statement is used to delete or remove indexes from a table in the
database.
Syntax:
SQL> DROP TABLE table_name;
Example:
SQL> DROP TABLE student;
SQL – TRUNCATE TABLE:
The SQL TRUNCATE TABLE command is used to delete complete data from an
existing table.
You can also use DROP TABLE command to delete complete table but it would
remove complete table structure from the database and you would need to re-
create this table once again if you wish you store some data.
Syntax:
TRUNCATE TABLE table_name;
SQL – SELECT TABLE:
This statementis used to select data from a database.
Syntax:
SELECT column-name1,column-name2,column-name(s)from table_name;
Example:
SELECT studentid,studentnamefrom student;
A special character asterisk * is used to address all the data (belonging to all
columns) in a query.
Syntax:
SELECT * from table_name;
Example:
SELECT * from student;
SQL – INSERT TABLE:
Syntax:
INSERT INTO table_name (column-names)
VALUES (values);
SQL – DELETE TABLE:
Syntax:
DELETE FROM table_name
WHERE [condition];
The statement SQL DELETE ALL ROWS is used to delete all rows from the
table.
Syntax:
DELETE FROM table_name;
SQL – UPDATE TABLE:
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
CONSTRAINT
NOT NULL CONSTRAINT
Syntax:
CREATE TABLE table_name(
column_name1 data_type(size) NOT NULL,
column_name2 data_type(size) NOT NULL,
column_name3 data_type(size)
);
PRIMARY KEY CONSTRAINT
Syntax:
CREATE TABLE table_name(
column_name1 data_type(size) PRIMARY KEY,
column_name2 data_type(size) NOT NULL,
column_name3 data_type(size)
);
UNIQUE CONSTRAINT
Syntax:
CREATE TABLE table_name(
column_name1 data_type(size) NOT NULL UNIQUE,
column_name2 data_type(size) NOT NULL,
column_name3 data_type(size)
);
CHECK CONSTRAINT
Syntax:
CREATE TABLE table_name(
column_name1 INTEGER AUTOINCREMENT,
column_name2 data_type(size),
column_name3 data_type(size) DEFAULT value,
CHECK column_name1 condition
);
DEFAULT CONSTRAINT
Syntax:
CREATE TABLE table_name(
column_name1 INTEGER AUTOINCREMENT,
column_name2 data_type(size),
column_name3 data_type(size) DEFAULT value
);
CLAUSE
WHERE CLAUSE
Syntax:
SELECT column_name1, column_name2
FROM table_name
WHERE [condition];
ORDER BY CLAUSE
Syntax;
SELECT column_name1, column_name2
FROM table_name
ORDER BY column_name1,
column_name2, ASC;
SELECT column_name1,
column_name2
FROM table_name
ORDER BY column_name1,
column_name2, DESC;
GROUP BY CLAUSE
Syntax;
SELECT column_name, function (column_name)
FROM table_name
WHERE condition
GROUP BY column_name;
LIKE CLAUSE
Syntax:
SELECT column_name1, column_name2
FROM table_name
WHERE column_name1 LIKE ‘A%’;
Syntax:
SELECT column_name1, column_name2
FROM table_name
WHERE column_name1 LIKE ‘A_’;
AND/OR OPERATOR
Syntax:
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] AND [condition2]...AND [conditionN];
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] OR [condition2]...OR [conditionN];