SQL
Short for Structured Query Language, SQL, originally known as SEQUEL (structured English query language), was developed by Dr. Edgar F. Codd at the IBM research center in 1974. Today, SQL has become the de facto standard database language. SQL statements tell the database server to perform an action or retrieve data.
The first commercial version of SQL was introduced in 1979 by Oracle. Today, there are three standards: SQL89 (SQL1), SQL92 (SQL2), and SQL99 (SQL3).
How do you pronounce SQL?
Although originally known as SEQUEL, the acronym was later changed to the "SQL" abbreviation because "SEQUEL" was a trademark of the UK-based Hawker Siddeley aircraft company. Because of this change, there is a lot of confusion if SQL should be pronounced as "es-cue-el" or "sequel." Although both pronunciations are widely used, we follow the Microsoft Manual of Style, which states if you are referring to the structured query language, it's pronounced as "es-cue-el." However, when you are referring to a product or the server running the product, SQL is pronounced as "sequel."
Realize that some types of SQL databases, like MySQL, do have official pronunciations.
What are SQL commands?
SQL has three types of commands - DDL (Data Definition Language statements, DML (Data Manipulation Language) statements, and control statements. DDL statements affect the database design (schema), while DML statements add, update, delete, and query data. Control statements affect how the database server performs its job (transactions, sessions, etc.). The syntax of the statements depends on what database engine is used.
Common SQL statements
DDL | DML | Control |
---|---|---|
ALTER CREATE DROP GRANT |
SELECT INSERT UPDATE DELETE |
START/BEGIN TRANSACTION COMMIT ROLLBACK SAVEPOINT/SAVE TRANSACTION RELEASE |
SQLite3 example
The following is an example of using SQL with SQLite, a free and open-source RDBMS (relational database management system).
In SQL, lines starting with a double-dash ("--") are comments, and are not executed. Individual statements may span multiple lines, and each statement ends in a semicolon (";").
-- DDL Example CREATE TABLE myTable ( myTableID INTEGER PRIMARY KEY AUTOINCREMENT, txtName CHAR(30) NOT NULL ); -- DML Examples INSERT INTO myTable (txtName) VALUES ('Computerhope Happy'); INSERT INTO myTable (txtName) VALUES ('Computerhope Joyful'); INSERT INTO myTable (txtName) VALUES ('Computerhope Knowledgeable'); SELECT myTableID, txtName FROM myTable; DELETE FROM myTable WHERE txtName='Computerhope Joyful'; -- * means all fields SELECT * FROM myTable;
In the above sequence of SQL statements, the following actions would be taken on whatever database was currently loaded:
- The CREATE TABLE statement creates a new table in the database. The new table is named myTable. This table has two columns:
- myTableId, with the following constraints:
- It can contain only INTEGER (whole-number numeric) values.
- It is the PRIMARY KEY of the table: its value must be unique in the column, and it cannot be null.
- Its values will AUTOINCREMENT: If a value for myTableId is not specified, the lowest unique integer greater than every other existing value is automatically used. For example, if the column already contains the values 1, 3, and 4, and a new row is added, its myTableId will be set to 5.
- txtName, with the following constraints:
- It contains strings whose length cannot exceed 30 characters.
- Its value cannot be null.
- The INSERT INTO statement inserts three rows into the myTable table, and sets their txtName values. The myTableId values for these rows are implicitly set as 1, 2, and 3.
- The SELECT statement returns the myTableId and txtName values of every row in myTable.
- The DELETE statement deletes the row whose txtName value is "Computerhope Joyful".
- The SELECT statement returns the values of all columns of every row in myTable.
Executing SQL statements from a file
If the above sequence of SQL statements was saved in a text file named example.sql, you could execute those commands on a local database file named data.db like this:
sqlite3 data.db
SQLite version 3.37.0 2021-12-09 01:34:53 Enter ".help" for usage hints.
sqlite> .read example.sql
1|Computerhope Happy 2|Computerhope Joyful 3|Computerhope Knowledgeable 1|Computerhope Happy 3|Computerhope Knowledgeable
sqlite3> .quit
Executing SQL statements directly in the SQLite 3 shell
The following example shows how you can execute the above commands directly in the SQLite 3 interactive command-line shell, on a database file named data2.db.
sqlite3 data2.db
SQLite version 3.37.0 2021-12-09 01:34:53 Enter ".help" for usage hints.
sqlite> CREATE TABLE myTable ( ...> myTableID INTEGER PRIMARY KEY AUTOINCREMENT, ...> txtName CHAR(30) NOT NULL ...> ); sqlite> INSERT INTO myTable (txtName) VALUES ('Computerhope Happy'); sqlite> INSERT INTO myTable (txtName) VALUES ('Computerhope Joyful'); sqlite> INSERT INTO myTable (txtName) VALUES ('Computerhope Knowledgeable'); sqlite> SELECT myTableID, txtName FROM myTable;
1|Computerhope Happy 2|Computerhope Joyful 3|Computerhope Knowledgeable
sqlite> DELETE FROM myTable WHERE txtName='Computerhope Joyful'; sqlite> SELECT * FROM myTable;
1|Computerhope Happy 3|Computerhope Knowledgeable
sqlite> .quit
Should I use "a" or "an" in front of SQL?
The answer to this question depends on how you're pronouncing "SQL." If it is being pronounced as "es-cue-el," because it starts with an "es" sound, use "an" in front of SQL. However, if you're pronouncing "SQL" as "sequel," then use "a" in front of SQL.
Computer abbreviations, Database, Database terms, DDL, DML, Foreign key, LIKE, MySQL, Oracle database, Primary key, Relationship, SQL injection, SQL Server, Synonym, TLA