SQL
w SQL Server : Overview
w SQL : Overview
w Types of SQL
w Database : Creation
w Tables : Creation & Manipulation
w Data : Creation & Manipulation
w Data : Retrieving using SQL
SQL : Overview
w Is the standard command set used to
communicate with the relational database
management systems
w Can do Creating databases, Creating tables,
Querying and Manipulating data and
granting access to the users
w English like structure
w is by nature flexible
Types of SQL Commands
w SQL statements are divided into the
following categories
w Data Definition Language (DDL)
w Data Manipulation Language (DML)
w Data Query Language (DQL)
w Data Control Language (DCL)
w Data Administration Statements (DAS)
w Transaction Control Statements (TCS)
Data Definition Language
w Is used to create, alter and delete database
objects
w The commands used are
w CREATE
w ALTER
w DROP
Data Manipulation Language
w Used to insert data into the database,
modify and delete the data in the database
w Three DML statements
w INSERT
w UPDATE
w DELETE
Data Query Language
w This statement enables you to query one or
more tables to get the information
w commonly used SQL statements
w SQL has only one data query statement
w SELECT
Data Control Language
w The DCL consists of commands that
control the user¶s access to the database
objects
w The DCL is mainly related to the security
issues
w The DCL commands are
w GRANT - Giving access to the data
w REVOKE - Denying access to the data
Data Administration Statements
w DASs allow the user to perform audits and
analysis on operations within the database.
w Used to analyze the performance of the
system
w Data Administration commands are
w START AUDIT
w STOP AUDIT
Transaction Control Statements
w TCSs are statements, which manage all the
changes made by the DML statements
w Some of the TCSs are
w COMMIT
w ROLLBACK
w SAVEPOINT
w SET TRANSACTION
Databases
w Collection of related data and manipulation
of that data
w Can create database using SQL command
w CREATE DATABASE databasename
Tables
w Are the basic building blocks in any
RDBMS
w contains rows and columns of data
w using DDL commands, we can create , alter
and delete tables
w Creation of table includes the properties of
the columns
Create statement
CREATE TABLE table-name
(column-1-definition
[,column-2-definition] «..
[,column-n-definition]
[,primary key (column name)]
[.alternate key (column name)]
[,Foreign key (column name) ]);
Column definition
w columnName data-type [NULL | NOT NULL
[WITH DEFAULT | UNIQUE]]
w NULL - RDBMS insert a null in that column if
the user does not specify a value
w NOT NULL - column should have a value
w WITH DEFAULT - the RDBMS will substitute
the default values
w UNIQUE - no duplicate values will be allowed
Data types
w char(n) - represents a fixed length of string of µn¶
characters where n>0 and is an integer
w varchar(n) - varying length string whose max
length is µn¶
w bit(n) - represents a fixed length string of exactly
µn¶ bits
w decimal(p, q) - represents a decimal number, µp¶
digits and with decimal point µq¶ digits from
right
Data Types
w float(n) - represents the floating point
number
w int - represents a signed integer
w datetime - represents the date/time
w money - represents the currency
2nd form of CREATE
w CREATE TABLE new-table-name LIKE
table-name
w when a table is created from an existing
table only the structure is copied; the
primary, alternate and foreign key
definitions is not inherited
Modifying a Table
w An existing table can be modified by using
the ALTER TABLE statement
w ALTER TABLE table-name
ADD column definition
w ALTER TABLE table-name
Add CONSTRAINT constraint name
Primary key (column name)
Deleting a table
w An existing table can be deleted at any time
by using the DROP TABLE statement
w DROP TABLE table-name
w specified table is deleted from the system
w all the data for that table also will be
deleted
Inserting rows into a table
w INSERT INTO table-name
[[column [,column]«.]]
values [literal[,literal]«]];
w a single row is inserted into the table, having
specified columns
w INSERT INTO table-name
[[column [,column]«.]]
subquery;
w the subquery is evaluated first and a copy of the
result(usually multiple rows) is inserted into the table
Updating fields in a row
w UPDATE table-name
SET column-name = expr
[WHERE condition]
w table-name : table for the data to be updated
w SET clause : the set of new values to be set
w WHERE clause : condition will be checked
and particular record gets updated
Deleting of data from the table
w DELETE FROM table-name
WHERE condition
w Depending on the condition the record will
be deleted from the table
SELECT statements
w SELECT - A keyword that tells the
database this command is a query. All
queries begin with this word followed by a
space
w the select command simply instructs the
database to retrieve information from a
table
Different features applied to a simple
statement
w All columns
w Qualified Retrieval
w Eliminating Duplicates
w Using Boolean(IN, BETWEEN, LIKE)
w Using Escape clause
w Computed values
w Involving nulls
w All Columns
w SELECT * FROM Table-name
w Qualified Retrieval
w SELECT * FROM table-name
WHERE condition
w can use all comparision operators (=, <>, <, >, <=,
>=) in the WHERE clause
w can contain multiple comparison with AND, OR,
NOT
w Eliminating Duplicates
w SELECT DISTINCT column-name FROM table-
name
w Using Boolean Operators
w IN
w SELECT * FROM table-name
WHERE column-name IN (val1, val2, val3«);
w BETWEEN
w SELECT * FROM table-name
WHERE column-name BETWEEN val1 and val2
w between is an inclusive operator
w values matching either of the boundary values
cause the predicate to be true
w NOT BETWEEN
w SELECT * FROM table-name
WHERE column-name NOT BETWEEN val1 and
val2
w LIKE
w SELECT * FROM table-name
WHERE column-name LIKE µstring%¶
w LIKE µ_%¶
w Escape Sequence
w SELECT * FROM table-name
WHERE column-name LIKE µ%\_%¶
w Computed Values
w SELECT column1, column2Expression
FROM table-name
WHERE condition
w NULLS
w SELECT * FROM table-name
WHERE column-name IS NULL
w ORDER BY
w SELECT * FROM table-name
ORDER BY column-name DESC