SQL Syntax
Database Tables
A database typically contains one or more tables. Each table is identified by a name (e.g., "Websites"), and it contains records (rows) with data.
In this tutorial, we created the Websites table in the tutorialpro database of MySQL to store website records.
We can view the data of the "Websites" table with the following commands:
mysql> use tutorialpro;
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 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | tutorialpro.org | http://www.tutorialpro.org/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
5 rows in set (0.01 sec)
Explanation
use tutorialpro; command is used to select the database.
set names utf8; command is used to set the character set.
SELECT * FROM Websites; reads information from the data table.
The table above contains five records (each corresponding to a website's information) and five columns (id, name, url, alexa, and country).
SQL Statements
Most of the actions you need to perform on a database are done with SQL statements.
The following SQL statement selects all records from the "Websites" table:
Example
In this tutorial, we will explain various different SQL statements.
Remember...
- SQL is case insensitive: SELECT is the same as select.
Semicolon after SQL Statements?
Some database systems require a semicolon at the end of each SQL statement.
The semicolon is a standard method to separate each SQL statement in database systems, allowing multiple SQL statements 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
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 - Changes (modifies) a database table
DROP TABLE - Deletes a table
CREATE INDEX - Creates an index (search key)
DROP INDEX - Deletes an index