SQL (pronounced "ess-que-el") stands for Structured Query Language and is used to communicate with databases. The SQL is a standard language for specifying accesses and modifications to relational databases. Originally standardized by the American National Standards Institute (ANSI) and the International Standards Organization (ISO) in 1986 as being the standard language for relational database manaement systems. Standards Organization (ISO) in 1986.
Most common relational database management systems use SQL, e.g.:
Relational databases store information within one or more tables which are
uniquely identifiable by their names. Tables are made from columns and rows,
where the columns have their own uniquely identifiable name within the table
(Note: columns do not need to be unique between tables, only within each
table). Each column is defined by it's name, it's data type and by constraints
it's creator decides to place on it. (see more about constraints in 'create
table bellow').
Rows on the other hand contain the records/information for
the database related to each column. Here is an example of a table where a
Table contains a State Name and an unique integer for refferencing it.
States | |
---|---|
State_id | State |
1 | Alabama |
2 | Alaska |
3 | Arizona |
4 | Arkansas |
5 | California |
6 | Colorado |
7 | Connecticut |
8 | Delaware |
9 | Florida |
10 | Georgia |
The general form of the CREATE statement is as follows:
CREATE TABLE table_name( columnName1 dataType constraints, columnName2 dataType constraints, ... columnNameN dataType constraints)
Example: creating a table to reference states by a number
CREATE TABLE States( State_id INTEGER PRIMARY KEY NOT NULL, State CHAR(20));
To add a record to a table, you use the INSERT statement. The general form
of the INSERT statement is as follows:
INSERT INTO table_name(column_name_1, column_name_2, ..., column_name_3) VALUES(value_1, value_2, ..., value_3);
Example: Inserting data into a table which holds data for Corvettes.
INSERT INTO Corvettes(Vette_id, Body_style, Miles, Year, State) VALUES (37, 'convertible', 25.5, 1986, 17);
To retrieve data which matches the criteria you specify, you use the select statement. The general form of the SELECT statement is as follows:
SELECT column names FROM table names [WHERE condition]When using the SELECT statement, the column names which follow it will be the columns which will be returned in the results (you can use * to select all of them). The tablename following FROM specifies the table which will be queried. If the WHERE statement is excluded, the query will return the contents of all the records within the specified tables (with the requested columns). When the WHERE statment is used, you have the following conditional selection:
Example: searching for body_style of Corvettes which were made after 1994:
SELECT Body_style FROM Corvettes WHERE Year > 1994;
To change data within pre-existing records which match a specified criteria, you use the UPDATE statement. The general form of the UPDATE statement is as follows:
UPDATE table_name SET column_name_1 = value_1, column_name_2 = value_2, ..., column_name_n = value_n WHERE column_name = value
Example: changing the year of a Corvette which had been wrongly typed in.
UPDATE Corvettes SET Year = 1996 WHERE Vette_id = 17;
To remove records from a table, you use the DELETE statement. The general form of the DELETE statement is as follows:
DELETE FROM table_name WHERE column_name = value
Example: Removing the record of a Corvette which is identified as nr. 17 (e.g. imagine this is a database for a car sale, and this car has just been sold and can be removed from the database)
DELETE FROM Corvettes WHERE Vette_id = 27