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