0 évaluation0% ont trouvé ce document utile (0 vote)
9 vues26 pages
Pocket Reference Anuja Madushan www.anujalk.info 1 P a g e 2 DROP DATABASE statement is used to delete a database. DROP TABLE statement is used if we only want to delete the data inside the table, and not the table itself. INSERT INTO table_name VALUES (5, 'Tjessem', 'Jakob') P_Id LastName FirstName Address City DateO
Pocket Reference Anuja Madushan www.anujalk.info 1 P a g e 2 DROP DATABASE statement is used to delete a database. DROP TABLE statement is used if we only want to delete the data inside the table, and not the table itself. INSERT INTO table_name VALUES (5, 'Tjessem', 'Jakob') P_Id LastName FirstName Address City DateO
Pocket Reference Anuja Madushan www.anujalk.info 1 P a g e 2 DROP DATABASE statement is used to delete a database. DROP TABLE statement is used if we only want to delete the data inside the table, and not the table itself. INSERT INTO table_name VALUES (5, 'Tjessem', 'Jakob') P_Id LastName FirstName Address City DateO
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
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
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