SQL Syntax
SQL Syntax
SQL tutorial gives unique learning on Structured Query Language and it helps to make
practice on SQL commands which provides immediate results. SQL is a language of database,
it includes database creation, deletion, fetching rows and modifying rows etc.
SQL is an ANSI (American National Standards Institute) standard, but there are many
different versions of the SQL language.
What is SQL?
SQL is Structured Query Language, which is a computer language for storing, manipulating
and retrieving data stored in relational database.
SQL is the standard language for Relation Database System. All relational database
management systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres and SQL
Server use SQL as standard database language.
SQL is case insensitive
Why SQL?
Allows users to access data in relational database management systems.
Allows users to define the data in database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-compilers.
SQL Process:
When you are executing an SQL command for any RDBMS, the system determines the best
way to carry out your request and SQL engine figures out how to interpret the task. There are
various components included in the process. These components are Query Dispatcher,
Optimization Engines, Classic Query Engine and SQL Query Engine, etc. Classic query
engine handles all non-SQL queries, but SQL query engine won't handle logical files.
Following is a simple diagram showing SQL Architecture:
SQL Commands:
The standard SQL commands to interact with relational databases are CREATE, SELECT,
INSERT, UPDATE, DELETE and DROP. These commands can be classified into groups
based on their nature:
Examples:
SQL Terminologies –
Table
Field or Column
Record or Row
Float
Real
SQL Constraints:
Constraints are the rules enforced on data columns on table. These are used to limit the type of
data that can go into a table. This ensures the accuracy and reliability of the data in the
database.
Constraints could be column level or table level. Column level constraints are applied only to
one column, whereas table level constraints are applied to the whole table.
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is specified.
UNIQUE Constraint: Ensures that all values in a column are different.
PRIMARY Key: Uniquely identified each rows/records in a database table.
FOREIGN Key: Uniquely identified a rows/records in any another database table.
CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy
certain conditions.
Example-
2. DEFAULT Constraint:
The DEFAULT constraint provides a default value to a column when the INSERT INTO
statement does not provide a specific value.
Example-
3. UNIQUE Constraint:
The UNIQUE Constraint prevents two records from having identical values in a
particular column. In the CUSTOMERS table, for example, you might want to prevent
two or more people from having identical age.
Example-
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL UNIQUE,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
Example 1-
Example 2-
Example-
CUSTOMERS table:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
ORDERS table:
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references CUSTOMERS(ID),
AMOUNT double,
PRIMARY KEY (ID)
);
6. CHECK Constraint:
The CHECK Constraint enables a condition to check the value being entered into a
record. If the condition evaluates to false, the record violates the constraint and isn’t entered
into the table.
Example-
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT CHECK (AGE >= 18),
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
SQL Syntax –
Example :
----------------------------
SQL DESC Statement:
DESC table_name;
Example :
---------------------------
SQL ALTER TABLE Statement:
Example :
------------------------------------------------------------------
SQL DROP TABLE Statement:
Example :
------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
SQL SELECT Statement:
SELECT column1, column2....columnN
FROM table_name;
Example :
------------------------------------------------------------------
------------------------------------------------------------------
SQL WHERE Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION;
Example :
------------------------------------------------------------------
------------------------------------------------------------------
SQL IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);
Example :
------------------------------------------------------------------
------------------------------------------------------------------
SQL LIKEClause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name LIKE { PATTERN };
Example :
------------------------------------------------------------------
------------------------------------------------------------------
Example :
------------------------------------------------------------------
------------------------------------------------------------------