Introduction to SQL
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute)
standard
What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
SQL is a Standard - BUT....
Although SQL is an ANSI (American National Standards
Institute) standard, there are many different versions of the
SQL language.
However, to be compliant with the ANSI standard, they all
support at least the major commands (such as SELECT,
UPDATE, DELETE, INSERT, WHERE) in a similar manner.
SQL DML and DDL
SQL can be divided into two parts: The Data Manipulation
Language (DML) and the Data Definition Language (DDL).
The query and update commands form the DML part of SQL:
1. SELECT - extracts data from a database
2. UPDATE - updates data in a database
3. DELETE - deletes data from a database
4. INSERT INTO - inserts new data into a database
The DDL part of SQL permits database tables to be
created or deleted.
It also define indexes (keys), specify links between tables,
and impose constraints between tables.
The most important DDL statements in SQL are:
1. CREATE DATABASE - creates a new database
2. ALTER DATABASE - modifies a database
3. CREATE TABLE - creates a new table
4. ALTER TABLE - modifies a table
5. DROP TABLE - deletes a table
6. CREATE INDEX - creates an index (search key)
7. DROP INDEX - deletes an index
SQL CREATE DATABASE Statement
The CREATE DATABASE statement is used to create a
database.
SQL CREATE DATABASE Syntax
CREATE DATABASE database_name
SQL CREATE TABLE Statement
The CREATE TABLE statement is used to create a table in a
database.
SQL CREATE TABLE Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records
in a table.
It is possible to write the INSERT INTO statement in two
ways.
The first way specifies both the column names and the
values to be inserted:
1. INSERT INTO table_name (column1, column2, column3,
...) VALUES (value1, value2, value3, ...);
2. INSERT INTO table_name VALUES (value1, value2, value3,
...);
SQL SELECT Statement
The SELECT statement is used to select data from a
database.
The result is stored in a result table, called the result-set.
SQL SELECT Syntax
SELECT column_name(s) FROM table_name
SELECT * FROM table_name
An SQL SELECT Example
The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger