introduction to SQL
SQL :
SQL is a standard language for storing, manipulating and retrieving data in databases.
What is 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
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
Using SQL in Your Web Site
To build a web site that shows data from a database, you will need:
- An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
- To use a server-side scripting language, like PHP or ASP
- To use SQL to get the data you want
- To use HTML / CSS to style the page
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.
Every table is broken up into smaller entities called fields. The fields in the Customers table consist of CustomerID, CustomerName, ContactName, Address, City, PostalCode and Country. A field is a column in a table that is designed to maintain specific information about every record in the table.
A record, also called a row, is each individual entry that exists in a table. For example, there are 91 records in the above Customers table. A record is a horizontal entity in a table.
A column is a vertical entity in a table that contains all information associated with a specific field in a table.
The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
eg: SELECT CustomerName, City FROM Customers;
The SQL WHERE Clause
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
eg SELECT * FROM Customer
WHERE Country='Mexico';
Operators in The WHERE Clause
The following operators can be used in the WHERE clause:
| Operator | Description | |
|---|---|---|
| = | Equal | |
| > | Greater than | |
| < | Less than | |
| >= | Greater than or equal | |
| <= | Less than or equal | |
| != | Not equal. |
|
| BETWEEN | Between a certain range | |
| LIKE | Search for a pattern | |
| IN | To specify multiple possible values for a column |
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO Syntax
It is possible to write the INSERT INTO
statement in two ways:
1. Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...); eg :
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');The SQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
The SQL DELETE Statement
The DELETE statement is used to delete existing records in a table.
DELETE Syntax
DELETE FROM table_name WHERE condition;
Example
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
The SQL LIKE Operator
The LIKE operator is used in a
WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the
LIKE operator:
- The percent sign (%) represents zero, one, or multiple characters
- The underscore sign (_) represents one, single character
Example
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
The SQL IN Operator
The IN operator allows you to specify multiple values in a
WHERE clause.
The IN operator is a shorthand for multiple
OR conditions.
IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...); Example
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
The SQL AND, OR and NOT Operators
The WHERE clause can be combined with
AND, OR, and
NOT operators.
The AND and OR operators are used to filter records based on more than one
condition:
- The
ANDoperator displays a record if all the conditions separated byANDare TRUE. - The
ORoperator displays a record if any of the conditions separated byORis TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...; 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; Example
SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin'; Example
SELECT * FROM Customers
WHERE City='Berlin'
OR City='München'; SELECT * FROM Customers
WHERE
NOT Country='Germany'; Example
SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='München');
Comments
Post a Comment