Structuredquerylanguage 22222
Structuredquerylanguage 22222
LANGUAGE
SQL is a standard language for accessing and
manipulating databases.
What is
SQL?
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
What Can SQL
do?
• 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
SQL General Data
Data Types
type Oracle
boolean Byte
integer Number
float Number
string Varchar
(variable) Varchar2
CustomerID CustomerNa ContactName Address City PostalCode Country
me
1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany
Futterkiste
• SELECT column_name,column_name
FROM table_name;
SQL SELECT DISTINCT
Statement
• In a table, a column may contain many duplicate values; and sometimes
you only want to list the different (distinct) values.
• The DISTINCT keyword can be used to return only distinct (different)
values.
• Example
• SELECT DISTINCT City FROM Customers;
SQL WHERE
Clause
• The WHERE clause is used to extract only those records that fulfill a
specified criterion.
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
• Example
• UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg' WHERE
CustomerName='Alfreds Futterkiste';
•
SQL DELETE
Statement
• The DELETE statement is used to delete rows in a table.
• Example
• DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria
Anders';
SQL
Constraints
• SQL constraints are used to specify rules for the data in a table.
• Example
• CREATE TABLE PersonsNotNull (
P_Id int NOT NULL,
LastName varchar(255) NOT NULL, FirstName varchar(255),
Address varchar(255), City varchar(255)
)
SQL UNIQUE
Constraint
• The UNIQUE constraint uniquely identifies each record in a database
table.
• The UNIQUE and PRIMARY KEY constraints both provide a guarantee
for uniqueness for a column or set of columns.
• A PRIMARY KEY constraint automatically has a UNIQUE constraint
defined on it.
• Note that you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
SQL UNIQUE Constraint on CREATE
TABLE
• The following SQL creates a UNIQUE constraint on the "P_Id"
column
when the "Persons" table is created:
• SQL Server / Oracle / MS Access:
• CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL, FirstName varchar(255),
Address varchar(255), City varchar(255)
)
SQL UNIQUE Constraint on ALTER
TABLE
• To create a UNIQUE constraint on the "P_Id" column when the table
is
already created, use the following SQL:
• MySQL / SQL Server / Oracle / MS Access:
• ALTER TABLE Persons
ADD UNIQUE (P_Id)
To DROP a UNIQUE
Constraint
• To drop a UNIQUE constraint, use the following
SQL:
• SQL Server / Oracle / MS Access:
• ALTER TABLE Persons
DROP CONSTRAINT uc_PersonID
SQL INSERT INTO
Statement
• The INSERT INTO statement is used to insert new records in a table.
• Example
• INSERT INTO Customers (CustomerName, ContactName, Address, City,
PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
SQL PRIMARY KEY
Constraint
• The PRIMARY KEY constraint uniquely identifies each record in a
database table.
• Primary keys must contain unique values.
• A primary key column cannot contain NULL values.
• Most tables should have a primary key, and each table can have only
ONE primary key.
• PK_Primarykey(P_ID)—Last name
• Primary keys composed of two columns
• CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255), Address
varchar(255), City varchar(255)
)
SQL PRIMARY KEY Constraint on ALTER
TABLE
• ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)
To DROP a PRIMARY KEY
Constraint
• ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID
Difference between Primary Key and Unique Key
Wildcard Description
% A substitute for zero or more characters
_ A substitute for a single character
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second
position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at
least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at
least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends
with "o"
The IN
Operator
• The IN operator allows you to specify multiple values in a WHERE clause.
• SQL IN Syntax
• SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
• Example
• SELECT * FROM Customers WHERE City IN ('Paris','London');
SQL BETWEEN
Operator
• The BETWEEN operator selects values within a range. The values can be
numbers, text, or dates.
• Example
• SELECT ProductName, ROUND(Price,0) AS
RoundedPrice FROM Products;
SQL
• AnJoins
SQL JOIN clause is used to combine rows from two or more tables,
based on a common field between them.
OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
Example
INSERT INTO Customers (CustomerName,
Country) SELECT SupplierName, Country FROM
Suppliers WHERE Country='Germany';
ROWID