SQL Commands and Syntax
SQL Commands and Syntax
SQL Commands
Basic SQL
Each record has a unique identifier or primary key. SQL, which stands for Structured Query Language, is used to
communicate with a database. Through SQL one can create and delete tables. Here are some commands:
CREATE TABLE - creates a new database table
ALTER TABLE - alters a database table
DROP TABLE - deletes a database table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
SQL also has syntax to update, insert, and delete records.
SELECT - get data from a database table
UPDATE - change data in a database table
DELETE - remove data from a database table
INSERT INTO - insert new data in a database table
SELECT
The SELECT is used to query the database and retrieve selected data that match the specific criteria that you
specify:
CREATE TABLE
The CREATE TABLE statement is used to create a new table. The format is:
CREATE TABLE tablename
(column1 data type,
column2 data type,
column3 data type);
char(size): Fixed length character string.
varchar(size): Variable-length character string. Max size is specified in parenthesis.
number(size): Number value with a max number of columns specified in parenthesis
date: Date value
number(size,d): A number with a maximum number of digits of "size" and a maximum number of "d" digits to the
right of the decimal
INSERT VALUES
Once a table has been created data can be inserted using INSERT INTO command.
UPDATE
To change the data values in a pre existing table, the UPDATE command can be used.
UPDATE tablename
SET colX = valX [, colY = valY, ...]
WHERE condition
DELETE
The DELETE command can be used to remove a record(s) from a table.
To delete all the records from a table without deleting the table do
DROP
To remove an entire table from the database use the DROP command.
ORDER BY
ORDER BY clause can order column name in either ascending (ASC) or descending (DESC) order.
AND / OR
AND and OR can join two or more conditions in a WHERE clause. AND will return data when all the conditions are
true. OR will return data when any one of the conditions is true.
IN
IN operator is used when you know the exact value you want to return for at least one of the columns
SQL Syntax
SQL is followed by unique set of rules and guidelines called Syntax. This tutorial gives you a quick start with SQL by
listing all the basic SQL Syntax:
All the SQL statements start with any of the keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE,
USE, SHOW and all the statements end with a semicolon (;).
Important point to be noted is that SQL is case insensitive, which means SELECT and select have same meaning in
SQL statements, but MySQL makes difference in table names. So if you are working with MySQL, then you need to
give table names as they exist in the database.
SQL IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);