0% found this document useful (0 votes)
6 views11 pages

SQL Syntax

The document provides an overview of SQL (Structured Query Language), detailing its purpose, commands, and data types used in relational databases. It explains the significance of SQL in managing data, including creating, modifying, and querying databases, as well as the various constraints that ensure data integrity. Additionally, it outlines the architecture of SQL processing and includes examples of SQL commands and syntax.

Uploaded by

seceh93562
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

SQL Syntax

The document provides an overview of SQL (Structured Query Language), detailing its purpose, commands, and data types used in relational databases. It explains the significance of SQL in managing data, including creating, modifying, and querying databases, as well as the various constraints that ensure data integrity. Additionally, it outlines the architecture of SQL processing and includes examples of SQL commands and syntax.

Uploaded by

seceh93562
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL Overview

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 describe the data.

 Allows users to define the data in database and manipulate that data.

 Allows to embed within other languages using SQL modules, libraries & pre-compilers.

 Allows users to create and drop databases and tables.

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

SQL Data Types

Exact Numeric Data Types:


 Bigint
 Int
 Smallint
 Tinyint
 Bit
 Decimal
 Numeric
 Money
 Smallmoney

Approximate Numeric Data Types:

 Float
 Real

Date and Time Data Types :


 Datetime
 Smalldatetime
 Date
 Time

Character Strings Data Types


 Char
 Varchar
 Varchar2()
 Text

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.

Following are commonly used constraints available in SQL:

 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.

1. NOT NULL Constraint:


By default, a column can hold NULL values. If you do not want a column to have a NULL
value, then you need to define such constraint on this column specifying that NULL is now
not allowed for that column.

Example-

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)
);

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-

CREATE TABLE CUSTOMERS(


ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2) DEFAULT 5000.00,
PRIMARY KEY (ID)
);

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)
);

4. PRIMARY Key or Constraint:


A primary key is a field in a table which uniquely identifies each row/record in a
database table. Primary keys must contain unique values. A primary key column cannot
have NULL values.
A table can have only one primary key, which may consist of single or multiple fields.
When multiple fields are used as a primary key, they are called a composite key.

Example 1-

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)
);

Example 2-

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, NAME)
);
5. FOREIGN Key or Reference Constraint:
A foreign key is a key used to link two tables together. This is sometimes called a referencing
key.
Foreign Key is a column or a combination of columns whose values match a Primary Key in a
different table.
The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign
Key in the second table.
If a table has a primary key defined on any field(s), then you can not have two records having
the same value of that field(s).

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 –

SQL CREATE TABLE Statement:

CREATE TABLE table_name(column1 datatype,


column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

Example :

----------------------------
SQL DESC Statement:

DESC table_name;

Example :

---------------------------
SQL ALTER TABLE Statement:

ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};

Example :

------------------------------------------------------------------
SQL DROP TABLE Statement:

DROP TABLE table_name;

Example :

------------------------------------------------------------------

ALTER TABLE Statement (Rename):


ALTER TABLE table_name RENAME TO new_table_name;
Example :

------------------------------------------------------------------

SQL INSERT INTO Statement:


INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);
Example :

------------------------------------------------------------------
SQL SELECT Statement:
SELECT column1, column2....columnN
FROM table_name;
Example :

------------------------------------------------------------------

SQL DISTINCT Clause:


SELECT DISTINCT column1, column2....columnN
FROM table_name;
Example :

------------------------------------------------------------------
SQL WHERE Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION;
Example :

------------------------------------------------------------------

SQL AND/OR Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION-1 {AND|OR} CONDITION-2;
Example :

------------------------------------------------------------------

SQL IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);
Example :

------------------------------------------------------------------

SQL BETWEEN Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE column_name BETWEEN val-1 AND val-2;
Example :

------------------------------------------------------------------

SQL LIKEClause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name LIKE { PATTERN };
Example :
------------------------------------------------------------------

SQL ORDER BY Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION
ORDER BY column_name {ASC|DESC};
Example :

------------------------------------------------------------------

SQL UPDATE Statement:


UPDATE table_name SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE CONDITION ];

Example :

------------------------------------------------------------------

SQL DELETE Statement:


DELETE FROM table_name
WHERE {CONDITION};
Example :

------------------------------------------------------------------

You might also like