Vous êtes sur la page 1sur 26

Pocket Reference

Anuja Madushan | www.anujalk.info


Pocket Reference
| www.anujalk.info

1 | P a g e



2 | P a g e

CREATE DATABASE

CREATE DATABASE database_name

CREATE TABLE

CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)

Examples :
CREATE TABLE Persons
(
P_Id int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

P_Id LastName FirstName Address City


3 | P a g e

DROP DATABASE
The DROP DATABASE statement is used to delete a database.
DROP DATABASE database_name

DROP TABLE
The DROP TABLE statement is used to delete a table.
DROP TABLE table_name

TRUNCATE TABLE
What if we only want to delete the data inside the table, and
not the table itself?
TRUNCATE TABLE table_name



4 | P a g e

ALTER TABLE

ALTER TABLE table_name
ADD column_name datatype

Examples :
ALTER TABLE Persons
ADD DateOfBirth date

P_Id LastName FirstName Address City DateOfBirth
1 Hansen Ola Timoteivn Sandn
2 Svendson Tove Borgvn 23 Sandn


ALTER TABLE table_name
ALTER COLUMN column_name datatype


ALTER TABLE table_name
DROP COLUMN column_name



5 | P a g e

INSERT INTO

INSERT INTO table_name
VALUES (value1, value2, value3,...)

INSERT INTO table_name (column1, column2)
VALUES (value1, value2)

Examples :
INSERT INTO Persons (P_Id, LastName, FirstName)
VALUES (5, 'Tjessem', 'Jakob')


P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
5 Tjessem Jakob



6 | P a g e

UPDATE

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Examples :
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
5 Tjessem Jakob


UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
5 Tjessem Jakob Nissestien 67 Sandnes

7 | P a g e

DELETE

DELETE FROM table_name
WHERE some_column=some_value

Examples :
DELETE FROM Persons
WHERE LastName='Tjessem' AND FirstName='Jakob'


Delete All Rows

DELETE FROM table_name

or

DELETE * FROM table_name


8 | P a g e

SELECT
The SELECT statement is used to select data from a database.The
result is stored in a result table, called the result-set
SELECT column_name(s)
FROM table_name

SELECT * FROM table_name

Examples :
SELECT LastName,FirstName FROM Persons

LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari

SELECT * FROM Persons
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes


9 | P a g e

SELECT DISTINCT
In a table, some of the columns may contain duplicate values. This
is not a problem, however, sometimes you will want to list only the
different (distinct) values in a table.The DISTINCT keyword can be
used to return only distinct (different) values.
SELECT DISTINCT column_name(s)
FROM table_name
Examples :


SELECT DISTINCT City FROM Persons


City
Sandnes
Stavanger


WHERE

The WHERE clause is used to extract only those records that fulfill a
specified criterion.
SELECT column_name(s)
FROM table_name
WHERE column_name operator value


10 | P a g e


SELECT * FROM Persons
WHERE City='Sandnes'

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes

Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN If you know the exact value you want to return for at least one of
the columns

AND & OR

The AND operator displays a record if both the first condition and
the second condition is true.
The OR operator displays a record if either the first condition or the
second condition is true.
SELECT * FROM Persons

11 | P a g e

WHERE FirstName='Tove'
AND LastName='Svendson'

P_Id LastName FirstName Address City
2 Svendson Tove Borgvn 23 Sandnes

SELECT * FROM Persons
WHERE FirstName='Tove'
OR FirstName='Ola'

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes

SELECT * FROM Persons WHERE
LastName='Svendson'
AND (FirstName='Tove' OR FirstName='Ola')

P_Id LastName FirstName Address City
2 Svendson Tove Borgvn 23 Sandnes



12 | P a g e

ORDER BY

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

Examples :

SELECT * FROM Persons
ORDER BY LastName

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
4 Nilsen Tom Vingvn 23 Stavanger
3 Pettersen Kari Storgt 20 Stavanger
2 Svendson Tove Borgvn 23 Sandnes

SELECT * FROM Persons
ORDER BY LastName DESC

P_Id LastName FirstName Address City
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Tom Vingvn 23 Stavanger
1 Hansen Ola Timoteivn 10 Sandnes



13 | P a g e

TOP

The TOP clause is used to specify the number of records
to return.

SELECT TOP number|percent column_name(s)
FROM table_name

Examples :

SELECT TOP 2 * FROM Persons

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes




14 | P a g e

LIKE
The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column.

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern

Examples :

SELECT * FROM Persons
WHERE City LIKE 's%'

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger


SELECT * FROM Persons
WHERE City LIKE '%s'

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes


15 | P a g e


SELECT * FROM Persons
WHERE City LIKE '%tav%'

P_Id LastName FirstName Address City
3 Pettersen Kari Storgt 20 Stavanger


SELECT * FROM Persons
WHERE City NOT LIKE '%tav%'

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes


Wildcards

Wildcard Description
% A substitute for zero or more characters
_ A substitute for exactly one character
[charlist] Any single character in charlist
[^charlist]
or
[!charlist]
Any single character not in charlist


16 | P a g e

IN
The IN operator allows you to specify multiple values in a WHERE
clause.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)

Examples :

SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen')

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
3 Pettersen Kari Storgt 20 Stavanger


Alias

You can give a table or a column another name by using an alias.
This can be a good thing to do if you have very long or complex
table names or column names.

SQL Alias Syntax for Tables
SELECT column_name(s)
FROM table_name
AS alias_name

17 | P a g e

SQL Alias Syntax for Columns
SELECT column_name AS alias_name
FROM table_name



JOIN

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

The "Persons" table:
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15



18 | P a g e

INNER JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

LastName FirstName OrderNo
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678



LEFT JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

LastName FirstName OrderNo
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
Svendson Tove



19 | P a g e

RIGHT JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
RIGHT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

LastName FirstName OrderNo
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
34764


FULL JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
FULL JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

LastName FirstName OrderNo
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
Svendson Tove
34764



20 | P a g e

UNION

The UNION operator is used to combine the result-set of two or
more SELECT statements.

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2


Constraints

NOT NULL
UNIQUE

CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
)




21 | P a g e

PRIMARY KEY

CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)


PRIMARY KEY Constraint on ALTER TABLE

ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)


To DROP a PRIMARY KEY Constraint

ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID


22 | P a g e

FOREIGN KEY

CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)


SQL Server Date Functions

The following table lists the most important built-in date functions in
SQL Server:
Function Description
GETDATE() Returns the current date and time
DATEPART() Returns a single part of a date/time
DATEADD() Adds or subtracts a specified time interval from a date
DATEDIFF() Returns the time between two dates
CONVERT() Displays date/time data in different formats



23 | P a g e

SQL Server Data Types

Data type Description Storage
char(n) Fixed-length character string. Maximum 8,000
characters
n
varchar(n) Variable-length character string. Maximum
8,000 characters

varchar(max) Variable-length character string. Maximum
1,073,741,824 characters

text Variable-length character string. Maximum
2GB of text data


Data type Description Storage
tinyint Allows whole numbers from 0 to 255 1 byte
smallint Allows whole numbers between -32,768 and
32,767
2 bytes
int Allows whole numbers between -2,147,483,648
and 2,147,483,647
4 bytes
bigint Allows whole numbers between -
9,223,372,036,854,775,808 and
9,223,372,036,854,775,807
8 bytes
decimal(p,s) Fixed precision and scale numbers. 5-17
bytes
numeric(p,s) Fixed precision and scale numbers. 5-17
bytes
smallmoney Monetary data from -214,748.3648 to
214,748.3647
4 bytes
money Monetary data from -922,337,203,685,477.5808
to 922,337,203,685,477.5807
8 bytes
float(n) Floating precision number data from -1.79E +
308 to 1.79E + 308.
4 or 8
bytes
real Floating precision number data from -3.40E +
38 to 3.40E + 38
4 bytes



24 | P a g e

SQL Aggregate Functions

SQL aggregate functions return a single value, calculated from values in
a column.
Useful aggregate functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum

SQL Scalar functions

SQL scalar functions return a single value, based on the input value.
Useful scalar functions:
UCASE() - Converts a field to upper case
LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
LEN() - Returns the length of a text field
ROUND() - Rounds a numeric field to the number of decimals
specified
NOW() - Returns the current system date and time
FORMAT() - Formats how a field is to be displayed


25 | P a g e

Vous aimerez peut-être aussi