SQL STATEMENTS
introduction
Introduction to SQL
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
SQL is an ANSI (American National Standards Institute) standard
Introduction to SQL (conti..)
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
Introduction to SQL (conti..)
SQL is a Standard - BUT....
Although SQL is an ANSI (American National Standards Institute) standard, there are
different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major
commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
SQL Syntax
Some of The Most Important SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
SQL CREATE DATABASE
SQL CREATE DATABASE Syntax:
CREATE DATABASE dbname;
SQL CREATE DATABASE Example:
CREATE DATABASE my_db;
SQL CREATE TABLE
SQL CREATE TABLE Syntax:
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
SQL CREATE TABLE Example:
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
SQL Constraints
SQL CREATE TABLE + CONSTRAINT Syntax:
CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);
SQL Constraints (conti..)
In SQL, we have the following constraints:
NOT NULL - Indicates that a column cannot store NULL value
UNIQUE - Ensures that each row for a column must have a unique value
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or
combination of two or more columns) have a unique identity which helps to find a
particular record in a table more easily and quickly
FOREIGN KEY - Ensure the referential integrity of the data in one table to match
values in another table
CHECK - Ensures that the value in a column meets a specific condition
DEFAULT - Specifies a default value for a column
SQL SELECT Statement
SQL SELECT Syntax:
SELECT column_name,column_name
FROM table_name;
Or
SELECT * FROM table_name;
SQL SELECT DISTINCT
SQL SELECT DISTINCT Syntax:
SELECT DISTINCT column_name,column_name
FROM table_name;
SQL WHERE Clause
SQL WHERE Syntax:
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Operators in The WHERE Clause
The following operators can be used in the WHERE clause:
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this
operator may be written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
SQL AND & OR Operators
The SQL AND & OR Operators
The AND operator displays a record if both the first condition AND the second condition
are true.
The OR operator displays a record if either the first condition OR the second condition is
true.
SQL ORDER BY Keyword
SQL ORDER BY Syntax:
SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;
SQL INSERT INTO Statement
SQL INSERT INTO Syntax:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
OR
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
SQL UPDATE Statement
SQL UPDATE Syntax:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
SQL DELETE Statement
SQL DELETE Syntax:
DELETE FROM table_name
WHERE some_column=some_value;
SQL LIKE Operator
SQL LIKE Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
SQL Wildcards
SQL Wildcard Characters
In SQL, wildcard characters are used with the SQL LIKE operator.
SQL wildcards are used to search for data within a table.
With SQL, the wildcards are:
Wildcard Description
% A substitute for zero or more characters
_ A substitute for a single character
[charlist] Sets and ranges of characters to match
[^charlist] Matches only a character NOT specified
or within the brackets
[!charlist]
SQL IN Operator
SQL IN Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
SQL BETWEEN Operator
SQL BETWEEN Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Source
http://www.w3schools.com/sql/