SQL Commands
SQL Commands
SQL Commands
Examples:
The following SQLstatement selects the "CustomerName" and "City" columns from
the "Customers" table:
The following SQLstatement selects all the columns from the "Customers"table:
Examples:
The following SQLstatement selects all the customers from the country "Mexico", in the
"Customers" table:
SELECT* FROMCustomers
WHERECountry='Mexico’;
SELECT* FROMCustomers
WHERE CustomerID=1;
WHERE Clause Operators
Operator Description
= Equal
<> Not Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than orequal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN Tospecify multiple possible values for a column
AND CommandStatement
• The AND and ORoperators are used to filter records based on more than
one condition:
• The AND operator displays a record if all the conditions separated by AND isTRUE:
Example:
The following SQLstatement selects all fields from "Customers"where
country is "Germany" AND city is"Berlin":
Examples:
The following SQLstatement selects all fields from "Customers" where cityis
"Berlin" OR"München":
Examples:
The following SQLstatement selects all fields from "Customers" where
country is NOT"Germany":
SELECT* FROMCustomers
WHERENOTCountry='Germany';
Combining AND, OR and NOT operators
• You can also combine the AND, ORand NOToperators:
Examples:
The following SQLstatement selects all fields from "Customers" where country is
"Germany" AND city must be "Berlin" OR"München":
The following SQLstatement selects all fields from "Customers" where country is
NOT"Germany" and NOT"USA":
Syntax:
INSERTINTO table_name (column1, column2, column3, ...)
VALUES(value1, value2, value3, ...);
Examples:
The following SQLstatement inserts a new record in the "Customers" table:
Examples:
The following SQLstatement will insert a new record, but only insert
data in the "CustomerName", "City", and "Country" columns:
Note:
A NULL value is different from a zero value or a field that contains
spaces. A field with a NULL value is one that has been left blank during
record creation.
UPDATE Command Statement
• The UPDATEstatement is used to modify the existing records in a table.
Example:
The following SQLstatement updates the first customer (CustomerID = 1)
with a new contact person and a new city (Alfred and Frankfurt are new
values):
UPDATECustomers
SETContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERECustomerID =1;
UPDATE Multiple Records
• It is the WHEREclause that determines how many records that will be
updated:
Example:
The following SQLstatement will update the contactname to"Juan" for all
records where country is"Mexico":
UPDATECustomers
SETContactName='Juan'
WHERECountry='Mexico’;
Note: Missing out the WHERE statement will alter every value under SET
DELETE Command Statement
• The DELETEstatement is used to delete existing records in atable.
Example:
The following SQLstatement deletes the customer “John Smith" from the
"Customers" table:
DELETE FROM
DCustomers
WWHERECustomerName=‘John
Smith’;
Note: To delete all from a table, use:
DELETE* FROMtable_name;
CREATE DATABASE Command Statement
• The CREATEDATABASEstatement is used to create a new SQL
database:
Syntax:
CREATEDATABASE databasename;
Example:
CREATEDATABASEtestDB;
DROP DATABASE CommandStatement
• The DROPDATABASEstatement is used to drop an existing SQLdatabase:
Syntax:
DROPDATABASEdatabasename;
Example:
DROPDATABASEtestDB;
CREATE TABLE Command Statement
• The CREATETABLEstatement is used to create a new table in a
database:
Syntax:
CREATETABLEtable_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
CREATE TABLEcontinued…
Example:
CREATETABLEtesttable (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
DROP TABLE CommandStatement
• The DROPTABLEstatement is used to drop an existing table in a
database.
Syntax:
DROPTABLEtable_name;
Example:
DROPTABLEtesttable;
TRUNCATE TABLE Command Statement
• The TRUNCATETABLEstatement is used to delete the data inside a
table, but not the tableitself.
Syntax:
TRUNCATETABLEtable_name;
Example:
TRUNCATETABLEtesttable;
ALTER TABLE Command Statement
• The ALTERTABLEstatement is used to add, delete, or modify columns
in an existing table.
• The ALTERTABLEstatement is also used to add and drop various
constraints on an existing table.
Syntax:
To add a column in a table, use the followingsyntax:
ALTERTABLEtable_name
ADD column_namedatatype;
ALTER TABLEcontinued…
Syntax:
To delete a column in a table, use the following syntax:
ALTERTABLEtable_name
DROPCOLUMNcolumn_name;
Examples:
ALTERTABLEtesttable
ADD DateOfBirth date;
ALTER TABLEcontinued…
Examples:
The following deletes the column named "DateOfBirth" in the “testtable"
table:
ALTERTABLEPersons
DROPCOLUMNDateOfBirth;
The following changes the data type of the column named "DateOfBirth" in
the “testtable" table (date to year):
ALTERTABLEPersons
ALTERCOLUMN DateOfBirth year;
SQL Constraints Examples
• SQLconstraints are used to specify rules for the data in atable: