Structured Query Language
Prepared By:
Reema Agrawal
Asst Professor
MERI
SQL
Structured Query language is a language that
provides an interface to database systems.
SQL was developed by IBM in 1970.
Features Of SQL:
SQL can be used by a range of users,
including those with little or no programming
experience.
It reduces the amount of time required for
creating and maintaining systems.
It is an English like language.
Data types:
SQL data type is an attribute that specifies
type of data of any object.
Each column, variable and expression has
related data type in SQL.
These are of several forms and sizes
Data types:
Char- Maximum length of 255 characters.
Alphabets only.
Varchar- Maximum of 8,000 characters.
Alphanumeric
Int- Numeric and negative
Float- Numeric, decimal and negative integers
Number- It is of floating type. Number (P,S). P-
determines the maximum length of the data.
S- Number of places to the right of the
decimal.
CREATE DATABASE
Create DATABASE databasename;
The CREATE TABLE command
The CREATE TABLE command defines each
column of the table uniquely.
Each column has a minimum of three
attributes – a name, datatype and size.
Each table column definition is separated
from the other by a comma.
Finally, the SQL statement is terminated with
a semi colon.
The CREATE TABLE command
Syntax:
CREATE TABLE table_name(column_name1
data_type(size),column_name2
data_type(size),column_name3
data_type(size),....);
Inserting Data Into Tables
Once a table is created, the most natural
thing to do is load this table with data to be
manipulated later.
Syntax:
INSERT INTO <tablename> values
(<‘expression 1’>, <‘expression2’>);
For specific columns:
INSERT INTO <tablename>
(<columnname1>, <columnname2>)
values (<‘expression 1’>, <‘expression2’>);
Table Name: Employee
Table Name: Student
Viewing Data In the Tables:
Once the data has been inserted into a table,
the next logical operation would be to view
what has been inserted.
The SELECT command is used to retrieve rows
selected from one or more tables.
To view the whole table
Selected columns and all rows
Selected rows and all columns
Selected columns and selected rows
Viewing Data In the Tables:
To view the whole table:
Syntax:
SELECT * FROM <tablename>;
To view selected columns and all rows
Syntax:
SELECT <columnname1>, <columnname2>
FROM <tablename>;
Viewing Data In the Tables:
Selected rows and all columns
Syntax:
SELECT * FROM <tablename> WHERE
<condition>;
Selected rows and selected columns
Syntax:
SELECT <columnname1>, <columnname2>
FROM <Tablename> WHERE <condition> ;
Eliminating Duplicate Rows
Specific columns
SELECT DISTINCT <columnname1>,
<columnname2> from <tablename>;
Entire rows
SELECT DISTINCT * from <tablename>;
Sorting Data In a table
The rows retrieved from the table will be
sorted in either ascending or descending
order depending on the condition specified in
the SELECT sentence.
Syntax:
Select * from <Tablename> order by
<columnname1>;
Select * from <Tablename> order by
<columnname1>desc;
DELETE OPERATION
The DELETE in SQL is used to remove either
all the rows from a table or a set of rows of a
table.
All the rows:
Syntax:
DELETE FROM <TableName>;
A set of rows from table:
Syntax:
DELETE FROM <TableName> WHERE
<condition>;
ALTER TABLE
Adding new columns
ALTER TABLE <tablename>
ADD(<newcolumnname><datatype> (<size>),
<newcolumnname><datatype> (<size>));
Dropping column
ALTER TABLE <tablename> DROP COLUMN
<columnname>;
Modifying columns
ALTER TABLE <tablename> MODIFY
(<columnname> <Newdatatype>(<Newsize>));
Updating The contents Of a table:
The UPDATE command is used to change or
modify data values in a table.
All the rows:
UPDATE <TableName> SET
<columnname1>=<expression1>,
<columnname2>=<expression2>;
A selected set of rows
UPDATE <TableName> SET
<columnname1>=<expression1>,
<columnname2>=<expression2> where
<condition>;
Renaming, Truncating And Dropping
Tables:
Renaming tables:
RENAME <TableName> to <NewTableName>;
Truncating tables:
TRUNCATE TABLE <TableName>;
Dropping tables:
DROP TABLE <TableName>;
Some Queries:
SELECT * FROM student WHERE name LIKE '%John
%‘ (with either first or last name as JOHN)
SELECT * FROM student WHERE name LIKE 'John
%‘ (with first name JOHN)
SELECT * FROM student WHERE name LIKE
'%John‘ (with last name JOHN)
SELECT * FROM student WHERE name LIKE '%a%'
AND name LIKE '%e%‘ (with either first alphabet
as a or e)