You are on page 1of 61

SQL

SQL

• SQL stands for Structured Query Language


• SQL lets you access and manipulate databases
• SQL became a standard of the American National
Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization
(ISO) in 1987
SQL
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and
views
Types of DBMS
languages
Types of DBMS languages:

• Data Definition Language (DDL): DDL is used for


specifying the database schema.
• To create the database instance – CREATE
• To alter the structure of database – ALTER
• To drop database instances – DROP
• To delete tables in a database instance
– TRUNCATE
• To rename database instances – RENAME
• All these commands specify or update the database
schema : Data Definition language.
• Data Manipulation Language (DML): DML is used
for accessing and manipulating data in a database.
• To read records from table(s) – SELECT
• To insert record(s) into the table(s) – INSERT
• Update the data in table(s) – UPDATE
• Delete all the records from the table – DELETE
• Data Control language (DCL): DCL is used for
granting and revoking user access on a database –
• To grant access to user – GRANT
• To revoke access from user – REVOKE
• In practical data definition language, data
manipulation language and data control languages
are not separate language; rather they are the parts
of a single database language such as SQL.
Transaction Control Language:

• It manage the changes made by DML .


• COMMIT-make transaction changes permanent
• ROLLBACK-undo changes made by transaction
either since it started or since save point
• SAVEPOINT-set point to which transaction can be
rolled back
• SET TRANSACTION-establish properties for
transaction
SQL QUERIES-1

DDL-COMMANDS
CREATE
• SQL CREATE TABLE statement is used to create
table in a database.
• If you want to create a table, you should name the
table and define its column and each column's data
type.
• To create the table:
• create table table-name (column-name1 datatype1,
column-name2 datatype2, column-name3
datatype3, column-name4 datatype4 );
create table STUDENTS(id number(10), name
varchar(20), age number(4));
•You can verify it, if you have created the table
successfully by looking at the message displayed by the
SQL Server, else you can use DESC command as follows:
•SQL> DESC STUDENTS;

Practice:
Create a table Employee having attributes as id, name,
department, age.
Inserting Data into Table

1. Specify both the column names and the


values to be inserted:
INSERT INTO table_name (column1, column2,
 column3, ...)
VALUES (value1, value2, value3, ...);
Inserting Data into Table
2. If you are adding values for all the columns of the
table, you do not need to specify the column names
in the SQL query.
However, make sure the order of the values is in
the same order as the columns in the table. Here,
the INSERT INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);
Inserting Data into Table

• Insert command is used to insert data into a table.


Following is its general syntax,
• INSERT into table-name values(data1,data2,..);
• Eg: INSERT into STUDENTS values(101, ‘Adam’ ,15);
2.Alter
• alter command is used for alteration of table
structures. There are various uses
of alter command, such as,
• to add a column to an existing table
• To rename any existing column
• To change the datatype of any column or to modify
its size.
• alter is also used to drop a column.
• To Add Column to existing Table
Using alter command we can add a column to an
existing table.
alter table table-name add(column-name datatype);
alter table STUDENTS add(address char);

• The above command will add a new


column address to the Student table
• To Add Multiple Column to existing Table
Using alter command we can even add multiple
columns to an existing table.
Syntax:

alter table table-name add(column-name1


datatype1, column-name2 datatype2, column-
name3 datatype3);
Example:

alter table STUDENTS add(father_name varchar(60),


mother_name varchar(60), dob date);

•The above command will add three new columns to


the Student table
• To Modify an existing Column
• alter command is used to modify data type of an
existing column .
alter table table-name modify(column-name datatype);
• alter table Student modify(address varchar(30));
The above command will modify address column of
the Student table
• To Rename a column
• Using alter command you can rename an existing
column.
alter table table-name rename column old-column-
name to column-name;
alter table Student rename column address to
Location;
• To Drop a Column
• alter command is also used to drop columns.
alter table table-name drop(column-name);

alter table Student drop(address);


Location
• The above command will drop address column from
the Student table
example

CREATE TABLE STUDENTS (  ID number(10)   
NOT NULL,  NAME varchar2(20) NOT NULL,  AGE
number(5)  NOT NULL,  ADDRESS varchar2 (25) );
•It will get that no null value is taken;
Truncate command
• truncate command removes all records from a
table. But this command will not destroy the table's
structure.
truncate table table-name
Example:
truncate table Student;
• The above query will delete all the records
of Student table.
• truncate command is different
from delete command.

delete command will delete all the rows from a table


whereas truncate command re-initializes a
table(like a newly created table).
RENAME query

• rename command is used to rename a table.


• rename old-table-name to new-table-name;
rename Student to Student_record;
• The above query will rename Student table
to Student_record.
DROP command
• drop query completely removes a table from database. This
command will also destroy the table structure. Following is its
Syntax,
• drop table table-name;
• EG:drop table Student;
• The above query will delete the Student table completely.
• It can also be used on Databases. For Example, to drop a database,
• drop database Test;The above query will drop a database
named Test from the system.

SELECT DISTINCT
• The SELECT DISTINCT statement is used to return only
distinct (different) values.
• Inside a table, a column often contains many duplicate values;
and sometimes you only want to list the different (distinct)
values.
Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;

SELECT DISTINCT Name FROM STUDENTS;


PRACTICE

• create table ‘YOUR NAME’ with attributes


name,reg_no,section and phone_number.
• 1.Add two column in the existing table with Address
and Percentage.
• 2.Change the Reg_No name to ID.
• 3.delete the column address
• 4.rename the table.
• 5.drop the table.
SQL Create Constraints
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL Constraints
SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a
table. This ensures the accuracy and reliability of the data in the
table. If there is any violation between the constraint and the data
action, the action is aborted.

Constraints can be column level or table level. Column level


constraints apply to a column, and table level constraints apply to
the whole table.
SQL Constraints
The following constraints are commonly used in SQL:
NOT NULL - Ensures that a column cannot have a NULL value

UNIQUE - Ensures that all values in a column are different

PRIMARY KEY - A combination of a NOT NULL and


UNIQUE. Uniquely identifies each row in a table
SQL Constraints
The following constraints are commonly used in SQL:
FOREIGN KEY - Prevents actions that would destroy links
between tables
CHECK - Ensures that the values in a column satisfy a specific
condition
DEFAULT - Sets a default value for a column if no value is
specified
SQL NOT NULL Constraint

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255),
FirstName varchar(255),
Age int
);
SQL Primary Key Constraint
The PRIMARY KEY constraint uniquely identifies each
record in a table.

Primary keys must contain UNIQUE values, and cannot


contain NULL values.

A table can have only ONE primary key;


SQL Primary Key Constraint

CREATE TABLE Persons (


ID int,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
Practice: example

Customers (CustomerID, CustomerName, Address,


City, PostalCode, Country );

insert into customers values (101, 'Name1',


'Address1', 'City1', 11111, 'India')
Practice: example

create table Customers (CustomerID int,


CustomerName varchar(150), Address varchar(250),
City varchar(200), PostalCode int, Country
varchar(200));

insert into customers values (101, 'Name1',


'Address1', 'City1', 11111, 'India')
• In SQL, which of the following is a data definition
language commands?
• A.RENAME
• B.REVOKE
• C.GRANT
• D.UPDATE
SQL WHERE Clause

• SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example
SELECT * FROM STUDENTS
WHERE name=‘Rohan’;
SQL AND, OR and NOT Operators
1. The AND and OR operators are used to filter records based
on more than one condition.
2. The AND operator displays a record if all the conditions
separated by AND are TRUE.
3. The OR operator displays a record if any of the conditions
separated by OR is TRUE.
4. The NOT operator displays a record if the condition(s) is
NOT TRUE.
SQL AND, OR and NOT Operators
AND Syntax

SELECT column1, column2, ...


FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
SQL AND, OR and NOT Operators
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
SQL AND, OR and NOT Operators

AND Example
select all fields from "Customers" where country
is “India" AND city is “City2":

SELECT * FROM Customers


WHERE Country=‘India' AND City=‘City2';
SQL AND, OR and NOT Operators
OR Example
select all fields from "Customers" where city is “City1" OR
“City2":
SELECT * FROM Customers WHERE City=‘City1' OR
City=‘City2';
SQL AND, OR and NOT Operators
NOT Example
select all fields from "Customers" where country is NOT
“India":
SELECT * FROM Customers WHERE NOT Country=‘India';
SQL AND, OR and NOT Operators
Combining AND, OR and NOT
select all fields from "Customers" where country is “India"
AND city must be “City2" OR “City3"
(use parenthesis to form complex expressions)
SELECT * FROM Customers WHERE Country=‘India' AND
(City=‘City2' OR City=‘City3’);
SQL AND, OR and NOT Operators
Combining AND, OR and NOT
select all fields from "Customers" where country is NOT
“India" and NOT “USA":

SELECT * FROM Customers WHERE NOT Country=‘India' AND


NOT Country='USA';
SQL ORDER BY
The ORDER BY keyword is used to sort the result-set in
ascending or descending order.
The ORDER BY keyword sorts the records in ascending order
by default. To sort the records in descending order, use the
DESC keyword.
Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
SQL ORDER BY
select all customers from the "Customers" table, sorted by the
"Country" column

SELECT * FROM Customers ORDER BY Country;


ORDER BY Several Columns
select all customers from the "Customers" table, sorted by the
"Country" and the "CustomerName" column.

This means that it orders by Country, but if some rows have


the same Country, it orders them by CustomerName

SELECT * FROM Customers ORDER BY Country,


CustomerName;
ORDER BY Several Columns
select all customers from the "Customers" table, sorted
ascending by the "Country" and descending by the
"CustomerName" column

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;
Test for NULL Values
It is not possible to test for NULL values with comparison
operators.
use the IS NULL and IS NOT NULL operators instead

lists all customers with a NULL value in the "Address" field:


SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
Test for NULL Values
It is not possible to test for NULL values with comparison
operators.
use the IS NULL and IS NOT NULL operators instead

lists all customers with a NULL value in the "Address" field:


SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
Test for NULL Values
IS NOT NULL:

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
SQL UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are


different.
Both the UNIQUE and PRIMARY KEY constraints provide a
guarantee for uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE
constraint.
However, you can have many UNIQUE constraints per table,
but only one PRIMARY KEY constraint per table.
SQL UNIQUE Constraint

CREATE TABLE Persons ( ID int NOT NULL UNIQUE, LastName


varchar(255) NOT NULL, FirstName varchar(255), Age int );

ALTER TABLE Persons ADD UNIQUE (ID);


SQL UNIQUE Constraint

CREATE TABLE Persons ( ID int NOT NULL UNIQUE, LastName


varchar(255) NOT NULL, FirstName varchar(255), Age int );

ALTER TABLE Persons ADD UNIQUE (ID);


SQL CHECK Constraint

• The CHECK constraint is used to limit the value range that


can be placed in a column.
• If you define a CHECK constraint on a column it will allow
only certain values for this column.
• If you define a CHECK constraint on a table it can limit the
values in certain columns based on values in other columns
in the row.
SQL CHECK Constraint

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);

ALTER TABLE Persons ADD CHECK (Age>=18);


SQL CHECK Constraint

Now Insert into the table with age values <18

You might also like