SQL Tutorial
SQL Tutorial
is a database computer language designed for the retrieval and management of data in a relational databases like
MySQL, MS Access, SQL Server, MS Access, Oracle, Sybase, Informix, Postgres etc. SQL stands for Structured Query
Language. SQL was developed in the 1970s by IBM Computer Scientists.
This SQL tutorial helps you learn SQL in simple and easy steps so that you can start your database programming quickly. It
covers most of the important concepts related to SQL for a basic to advanced understanding of SQL and to get a feel of how
SQL works.
Applications of SQL
SQL is one of the most widely used Query Language over the databases. SQL provides following functionality to the
database programmers:
What is SQL?
SQL is a language to operate databases; it includes Database Creation, Database Deletion, Fetching Data Rows, Modifying
& Deleting Data rows, etc.
SQL stands for Structured Query Language which is a computer language for storing, manipulating and retrieving data
stored in a relational database. SQL was developed in the 1970s by IBM Computer Scientists and became a standard of the
American National Standards Institute (ANSI) in 1986, and the International Organization for Standardization (ISO) in
1987.
Though SQL is an ANSI (American National Standards Institute) standard language, but there are many different dialects of
the SQL language like MS SQL Server is using T-SQL and Oracle is using PL/SQL.
SQL is the standard language to communicate with Relational Database Systems. All the Relational Database Management
Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their Standard
Database Language.
Why SQL?
SQL is widely popular because it offers the following advantages −
Allows users to access data in the relational database management systems.
Allows users to describe the data.
Allows users to define the data in a 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.
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures and views.
Query Dispatcher
Optimization Engines
Classic Query Engine
SQL Query Engine, etc.
A classic query engine handles all the non-SQL queries, but a SQL query engine won't handle logical files. Following is a
simple diagram showing the SQL Architecture –
SQL Basic Commands
The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE, DELETE
and DROP. These commands can be classified into the following groups based on their nature −
DDL - Data Definition Language
S.N. Command & Description
1 CREATE - Creates a new table, a view of a table, or other object in the database.
3 DROP - Deletes an entire table, a view of a table or other objects in the database.
What is a table?
The data in an RDBMS is stored in database objects which are called as tables. This table is basically a collection of related
data entries and it consists of numerous columns and rows.
Remember, a table is the most common and simplest form of data storage in a relational database. Following is an example
of a CUSTOMERS table which stores customer's ID, Name, Age, Salary, City and Country.−
What is a field?
Every table is broken up into smaller entities called fields. For example our CUSTOMERS table consist of different fields
which are ID, Name, Age, Salary, City and Country.
A field is a column in a table that is designed to maintain specific information about every record in the table.
What is a Record or a Row?
A record is also called as a row of data is each individual entry that exists in a table. For example, there are 7 records in the
above CUSTOMERS table. Following is a single row of data or record in the CUSTOMERS table −
What is a column?
A column is a vertical entity in a table that contains all information associated with a specific field in a table.
For example, our CUSTOMERS table have different columns to represent ID, Name, Age, Salary, City and Country.
SQL Constraints
Constraints are the rules enforced on data columns on a 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 can either be column level or table level. Column level constraints are applied only to one column whereas, table
level constraints are applied to the entire table.
Following are some of the most commonly used constraints available in SQL −
S.N. Constraints
1 NOT NULL Constraint - Ensures that a column cannot have a NULL value.
2 DEFAULT Constraint - Provides a default value for a column when none is specified.
3 UNIQUE Constraint - Ensures that all the values in a column are different.
7 INDEX - Used to create and retrieve data from the database very quickly.
Data Integrity
The following categories of data integrity exist with each RDBMS −
Entity Integrity − This ensures that there are no duplicate rows in a table.
Domain Integrity − Enforces valid entries for a given column by restricting the type, the format, or the range
of values.
Referential integrity − Rows cannot be deleted, which are used by other records.
User-Defined Integrity − Enforces some specific business rules that do not fall into entity, domain or
referential integrity.
Database Normalization
Database normalization is the process of efficiently organizing data in a database. There are two reasons of this
normalization process −
Eliminating redundant data, for example, storing the same data in more than one table.
Ensuring data dependencies make sense.
Both these reasons are worthy goals as they reduce the amount of space a database consumes and ensures that data is
logically stored. Normalization consists of a series of guidelines that help guide you in creating a good database structure.
Normalization guidelines are divided into normal forms; think of a form as the format or the way a database structure is laid
out. The aim of normal forms is to organize the database structure, so that it complies with the rules of first normal form,
then second normal form and finally the third normal form.
It is your choice to take it further and go to the Fourth Normal Form, Fifth Normal Form and so on, but in general, the Third
Normal Form is more than enough for a normal Database Application.
First Normal Form (1NF)
Second Normal Form (2NF)
Third Normal Form (3NF)