Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL Syntax

SQL Syntax

SQL (Structured Query Language) is a standard language for managing and manipulating relational databases, including functions such as data query, data insertion, data update, data deletion, and database structure creation and modification.


Database Tables

A database usually contains one or more tables. Each table is identified by a name (for example: "Websites"), and a table contains records (rows) with data.

In this tutorial, we created the Websites table in the RUNOOB database in MySQL to store website records.

We can view the data in the "Websites" table with the following command:


mysql> use RUNOOB;
Database changed

mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM Websites;
+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | Runoob Tutorial      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+
5 rows in set (0.01 sec)

Parsing


SQL statements

Most of the work you need to do on a database is accomplished with SQL statements.

The following SQL statement selects all records from the "Websites" table:

Example

SELECT

FROM

Websites

;

In this tutorial, we will explain various SQL statements to you.


Remember...


Semicolon after SQL statements?

Some database systems require a semicolon at the end of each SQL statement.

A semicolon is the standard way to separate each SQL statement in a database system, allowing more than one SQL statement to be executed in the same request to the server.

In this tutorial, we will use a semicolon at the end of each SQL statement.


Some of the Most Important SQL Commands

The following are some commonly used SQL statements and syntax:

SELECT: Used to query data from a database.

SELECT column_name(s)
FROM table_name
WHERE condition
ORDER BY column_name [ASC|DESC]

INSERT INTO: Used to insert new data into a database table.


INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)

UPDATE: Used to update existing data in a database table.


UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition

DELETE: Used to delete data from a database table.


DELETE FROM table_name
WHERE condition

CREATE TABLE: Used to create a new database table.


CREATE TABLE table_name (
    column1 data_type constraint,
    column2 data_type constraint,
    ...
)

ALTER TABLE: Used to modify the structure of an existing database table.


ALTER TABLE table_name
ADD column_name data_type

Or:


ALTER TABLE table_name
DROP COLUMN column_name

DROP TABLE: Used to delete a database table.


DROP TABLE table_name

CREATE INDEX: Used to create an index to speed up queries.


CREATE INDEX index_name
ON table_name (column_name)

DROP INDEX: Used to delete an index.


DROP INDEX index_name
ON table_name

WHERE: Used to specify filter conditions.


SELECT column_name(s)
FROM table_name
WHERE condition

ORDER BY: Used to sort a result set.


SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]

GROUP BY: Used to group a result set by one or more columns.


SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name(s)

HAVING: Used to filter a grouped result set.


SELECT column_name(s), aggregate_function(column_name)
FROM table_name
GROUP BY column_name(s)
HAVING condition

JOIN: Used to combine records from two or more tables.


SELECT column_name(s)
FROM table_name1
JOIN table_name2
ON table_name1.column_name = table_name2.column_name

DISTINCT: Used to return only distinct values.


SELECT DISTINCT column_name(s)
FROM table_name