Get Started. It's Free
or sign up with your email address
Rocket clouds
SQL by Mind Map: SQL

1. Terminology

1.1. RDBMS

1.1.1. Relational Database Management System

1.2. SQL

1.2.1. Structured Query Language

2. 1 - BASIC

2.1. INTRODUCTION

2.1.1. SQL stands for Structured Query Language

2.1.2. ANSI (American National Standards Institute) standard

2.1.3. lets you access and manipulate databases

2.1.4. The data in RDBMS is stored in database objects called tables.

2.1.4.1. A table is a collection of related data entries and it consists of columns and rows.

2.1.5. What

2.1.5.1. SQL can execute queries against a database

2.1.5.2. SQL can retrieve data from a database

2.1.5.3. SQL can insert records in a database

2.1.5.4. SQL can update records in a database

2.1.5.5. SQL can delete records from a database

2.1.5.6. SQL can create new databases

2.1.5.7. SQL can create new tables in a database

2.1.5.8. SQL can create stored procedures in a database

2.1.5.9. SQL can create views in a database

2.1.5.10. SQL can set permissions on tables, procedures, and views

2.2. SYNTAX

2.2.1. Database Tables

2.2.1.1. Contains one or more tables

2.2.1.2. Identified by Table "Name"

2.2.2. SQL Statements

2.2.2.1. Most actions need to perform are done with SQL Statements

2.2.2.2. NOT Case Sensitive

2.2.2.3. Some DB systems require SemiColon at end of each statement

2.2.3. Important Commands

2.2.3.1. SELECT - extracts data from a database

2.2.3.2. UPDATE - updates data in a database

2.2.3.3. DELETE - deletes data from a database

2.2.3.4. INSERT INTO - inserts new data into a database

2.2.3.5. CREATE DATABASE - creates a new database

2.2.3.6. ALTER DATABASE - modifies a database

2.2.3.7. CREATE TABLE - creates a new table

2.2.3.8. ALTER TABLE - modifies a table

2.2.3.9. DROP TABLE - deletes a table

2.2.3.10. CREATE INDEX - creates an index (search key)

2.2.3.11. DROP INDEX - deletes an index

2.3. SELECT

2.3.1. The SELECT statement is used to select data from a database

2.3.2. The result is stored in a result table, called the result-set.

2.3.3. Code Examples

2.3.3.1. SELECT * FROM table_name;

2.3.3.2. SELECT column_name,column_name FROM table_name;

2.4. SELECT DISTINCT

2.4.1. In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.

2.4.2. Code Examples

2.4.2.1. SELECT DISTINCT column_name,column_name FROM table_name;

2.5. WHERE

2.5.1. The WHERE clause is used to extract only those records that fulfill a specified criterion.

2.5.2. SQL requires single quotes around text values (most database systems will also allow double quotes).

2.5.3. However, numeric fields should not be enclosed in quotes:

2.5.3.1. Example

2.5.3.1.1. SELECT * FROM Customers WHERE CustomerID=1;

2.5.4. Code Examples

2.5.4.1. SELECT column_name,column_name FROM table_name WHERE column_name operator value;

2.5.5. Operators in WHERE Clause

2.5.5.1. =

2.5.5.1.1. Equal

2.5.5.2. <>

2.5.5.2.1. Not equal. Note: In some versions of SQL this operator may be written as !=

2.5.5.3. >

2.5.5.3.1. Greater than

2.5.5.4. <

2.5.5.4.1. Less than

2.5.5.5. >=

2.5.5.5.1. Greater than or equal

2.5.5.6. <=

2.5.5.6.1. Less than or equal

2.5.5.7. BETWEEN

2.5.5.7.1. Between an inclusive range

2.5.5.8. LIKE

2.5.5.8.1. Search for a pattern

2.5.5.9. IN

2.5.5.9.1. To specify multiple possible values for a column

2.6. AND & OR

2.6.1. The AND & OR operators are used to filter records based on more than one condition.

2.6.2. The AND operator displays a record if both the first condition AND the second condition are true.

2.6.2.1. Example

2.6.2.1.1. SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';

2.6.3. The OR operator displays a record if either the first condition OR the second condition is true.

2.6.3.1. Example

2.6.3.1.1. SELECT * FROM Customers WHERE City='Berlin' OR City='München';

2.6.4. Combined

2.6.4.1. Example

2.6.4.1.1. SELECT * FROM Customers WHERE Country='Germany' AND (City='Berlin' OR City='München');

2.7. ORDER BY

2.7.1. The ORDER BY keyword is used to sort the result-set by one or more columns.

2.7.2. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword.

2.7.3. Code Example

2.7.3.1. SELECT column_name,column_name FROM table_name ORDER BY column_name,column_name ASC|DESC;

2.7.3.2. ORDER BY Several Columns Example

2.7.3.2.1. The following SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the "CustomerName" column:

2.8. INSERT INTO

2.8.1. The INSERT INTO statement is used to insert new records in a table.

2.8.2. Code Examples

2.8.2.1. The first form does not specify the column names where the data will be inserted, only their values:

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

2.8.2.2. The second form specifies both the column names and the values to be inserted:

2.8.2.2.1. INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);

2.9. UPDATE

2.9.1. The UPDATE statement is used to update records in a table.

2.9.2. Code Examples

2.9.2.1. UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;

2.9.3. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

2.10. DELETE

2.10.1. The DELETE statement is used to delete rows in a table.

2.10.2. Code Examples

2.10.2.1. DELETE FROM table_name WHERE some_column=some_value;

2.10.3. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

2.10.4. Delete All Data

2.10.4.1. DELETE FROM table_name;

2.10.4.2. or

2.10.4.3. DELETE * FROM table_name;

2.11. INJECTION

2.11.1. SQL in Web Pages

2.11.1.1. The example above, creates a select statement by adding a variable (txtUserId) to a select string. The variable is fetched from the user input (Request) to the page.

2.11.1.2. Code Example

2.11.1.2.1. txtUserId = getRequestString("UserId"); txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;

2.11.1.3. Since SQL statements are text only, it is easy, with a little piece of computer code, to dynamically change SQL statements to provide the user with selected data:

2.11.1.4. When SQL is used to display data on a web page, it is common to let web users input their own search values.

2.11.2. SQL Injection

2.11.2.1. SQL injection is a technique where malicious users can inject SQL commands into an SQL statements, via web page input.

2.11.2.2. Injected SQL commands can alter SQL statement and compromises the security of a web application.

2.11.2.3. Let's say that the original purpose of the code was to create an SQL statement to select a user with a given user id. If there is nothing to prevent a user from entering "wrong" input, the user can enter some "smart" input like this: 105 or 1=1

2.11.3. The only proven way to protect a web site from SQL injection attacks, is to use SQL parameters.

2.11.3.1. .

3. 2 - ADVANCED

3.1. SQL SELECT TOP

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

3.1.2. The SELECT TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.

3.1.3. Not all database systems support the SELECT TOP clause.

3.1.3.1. SQL Server / MS Access Syntax

3.1.3.1.1. SELECT TOP number|percent column_name(s) FROM table_name;

3.1.3.2. MySQL Syntax

3.1.3.2.1. SELECT column_name(s) FROM table_name LIMIT number;

3.1.3.3. Oracle Syntax

3.1.3.3.1. SELECT column_name(s) FROM table_name WHERE ROWNUM <= number;

3.2. SQL LIKE

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

3.2.2. Code Examples

3.2.2.1. The following SQL statement selects all customers with a City starting with the letter "s":

3.2.2.1.1. SELECT * FROM Customers WHERE City LIKE 's%';

3.2.2.2. Tip: The "%" sign is used to define wildcards (missing letters) both before and after the pattern.

3.3. SQL Wildcards

3.3.1. %

3.3.1.1. A substitute for zero or more characters

3.3.2. _

3.3.2.1. A substitute for a single character

3.3.3. [charlist]

3.3.3.1. Sets and ranges of characters to match

3.3.3.1.1. The following SQL statement selects all customers with a City starting with "b", "s", or "p":

3.3.4. [^charlist] OR [!charlist]

3.3.4.1. Matches only a character NOT specified within the brackets

3.3.4.1.1. The following SQL statement selects all customers with a City NOT starting with "b", "s", or "p":

3.4. SQL IN

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

3.4.2. Code Examples

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

3.4.2.2. The following SQL statement selects all customers with a City of "Paris" or "London":

3.4.2.2.1. SELECT * FROM Customers WHERE City IN ('Paris','London');

3.5. SQL BETWEEN

3.5.1. The BETWEEN operator is used to select values within a range.

3.5.1.1. The values can be numbers, text, or dates.

3.5.2. Code Examples

3.5.2.1. SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;

3.5.2.2. SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;

3.5.2.3. NOT BETWEEN Operator Example To display the products outside the range of the previous example, use NOT BETWEEN:

3.5.2.3.1. SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20;

3.5.2.4. The following SQL statement selects all products with a price BETWEEN 10 and 20, but products with a CategoryID of 1,2, or 3 should not be displayed:

3.5.2.4.1. SELECT * FROM Products WHERE (Price BETWEEN 10 AND 20) AND NOT CategoryID IN (1,2,3);

3.5.2.5. BETWEEN Operator with Date Value Example

3.5.2.5.1. SELECT * FROM Orders WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#;

3.5.3. Tips

3.5.3.1. Notice that the BETWEEN operator can produce different result in different databases!

3.5.3.2. In some databases, BETWEEN selects fields that are between and excluding the test values.

3.5.3.3. In other databases, BETWEEN selects fields that are between and including the test values.

3.5.3.4. And in other databases, BETWEEN selects fields between the test values, including the first test value and excluding the last test value.

3.6. SQL Aliases

3.6.1. SQL aliases are used to give a database table, or a column in a table, a temporary name.

3.6.1.1. Basically aliases are created to make column names more readable.

3.6.2. Code Examples

3.6.2.1. SQL Alias Syntax for Columns

3.6.2.1.1. SELECT column_name AS alias_name FROM table_name;

3.6.2.2. SQL Alias Syntax for Tables

3.6.2.2.1. SELECT column_name(s) FROM table_name AS alias_name;

3.7. SQL Joins

3.7.1. An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.

3.7.2. The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all rows from multiple tables where the join condition is met.

3.7.3. .

3.7.3.1. .

3.7.3.2. .

3.8. SQL INNER JOIN

3.8.1. The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.

3.8.2. Code Examples

3.8.2.1. SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name;

3.8.2.2. SELECT column_name(s) FROM table1 JOIN table2 ON table1.column_name=table2.column_name;

3.8.3. Tips

3.8.3.1. INNER JOIN is the same as JOIN.

3.9. SQL LEFT JOIN

3.9.1. The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

3.9.2. Code Examples

3.9.2.1. SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;

3.9.2.2. SELECT column_name(s) FROM table1 LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name;

3.10. SQL RIGHT JOIN

3.10.1. The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

3.10.2. Code Examples

3.10.2.1. SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;

3.10.2.2. SELECT column_name(s) FROM table1 RIGHT OUTER JOIN table2 ON table1.column_name=table2.column_name;

3.11. SQL FULL JOIN

3.11.1. The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2)

3.11.2. The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

3.11.3. Code Examples

3.11.3.1. SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;

3.12. SQL UNION

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

3.12.2. Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

3.12.3. Code Examples

3.12.3.1. SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;

3.12.4. UNION ALL

3.12.4.1. Code Examples

3.12.4.1.1. SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM table2;

3.12.5. Tips

3.12.5.1. The UNION operator selects only distinct values by default. To allow duplicate values, use the ALL keyword with UNION.

3.13. SQL SELECT INTO

3.13.1. The SELECT INTO statement selects data from one table and inserts it into a new table.

3.13.2. Code Examples

3.13.2.1. SELECT * INTO newtable [IN externaldb] FROM table1;

3.13.2.2. SELECT column_name(s) INTO newtable [IN externaldb] FROM table1;

3.13.2.3. More Examples

3.13.2.3.1. Copy only a few columns into the new table:

3.13.2.3.2. Use the IN clause to copy the table into another database:

3.13.2.3.3. Create a backup copy of Customers:

3.13.2.3.4. Copy only the German customers into the new table:

3.13.2.3.5. Copy data from more than one table into the new table:

3.13.2.3.6. Tip: The SELECT INTO statement can also be used to create a new, empty table using the schema of another. Just add a WHERE clause that causes the query to return no data:

3.14. SQL INSERT INTO SELECT

3.14.1. The INSERT INTO SELECT statement selects data from one table and inserts it into an existing table. Any existing rows in the target table are unaffected.

3.14.2. Code Examples

3.14.2.1. We can copy all columns from one table to another, existing table:

3.14.2.1.1. INSERT INTO table2 SELECT * FROM table1;

3.14.2.2. Or we can copy only the columns we want to into another, existing table:

3.14.2.2.1. INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;

3.15. SQL CREATE DB

3.15.1. The CREATE DATABASE statement is used to create a database.

3.15.2. Code Examples

3.15.2.1. SQL CREATE DATABASE Syntax

3.15.2.1.1. CREATE DATABASE dbname;

3.15.2.2. The following SQL statement creates a database called "my_db":

3.15.2.2.1. CREATE DATABASE my_db;

3.16. SQL CREATE TABLE

3.16.1. The CREATE TABLE statement is used to create a table in a database.

3.16.2. Tables are organized into rows and columns; and each table must have a name.

3.16.3. Code Examples

3.16.3.1. CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), .... );

3.16.3.2. The data_type parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal, date, etc.).

3.17. SQL Constraints

3.17.1. SQL constraints are used to specify rules for the data in a table.

3.17.2. If there is any violation between the constraint and the data action, the action is aborted by the constraint.

3.17.3. Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the table is created (inside the ALTER TABLE statement).

3.17.4. Code Examples

3.17.4.1. SQL CREATE TABLE + CONSTRAINT Syntax

3.17.4.1.1. CREATE TABLE table_name ( column_name1 data_type(size) constraint_name, column_name2 data_type(size) constraint_name, column_name3 data_type(size) constraint_name, .... );

3.17.5. Constraint List

3.17.5.1. NOT NULL

3.17.5.1.1. Indicates that a column cannot store NULL value

3.17.5.2. UNIQUE

3.17.5.2.1. Ensures that each row for a column must have a unique value

3.17.5.3. PRIMARY KEY

3.17.5.3.1. A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have an unique identity which helps to find a particular record in a table more easily and quickly

3.17.5.4. FOREIGN KEY

3.17.5.4.1. Ensure the referential integrity of the data in one table to match values in another table

3.17.5.5. CHECK

3.17.5.5.1. Ensures that the value in a column meets a specific condition

3.17.5.6. DEFAULT

3.17.5.6.1. Specifies a default value when specified none for this column

3.18. SQL NOT NULL

3.18.1. By default, a table column can hold NULL values.

3.18.2. The NOT NULL constraint enforces a column to NOT accept NULL values.

3.18.3. The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field.

3.18.4. Code Examples

3.18.4.1. The following SQL enforces the "P_Id" column and the "LastName" column to not accept NULL values:

3.18.4.1.1. CREATE TABLE PersonsNotNull ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )

3.19. SQL UNIQUE

3.19.1. The UNIQUE constraint uniquely identifies each record in a database table.

3.19.2. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.

3.19.3. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

3.19.4. Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

3.19.5. SQL UNIQUE Constraint on CREATE TABLE

3.19.5.1. Code Examples The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table is created:

3.19.5.1.1. SQL Server / Oracle / MS Access:

3.19.5.1.2. MySQL:

3.19.5.1.3. MySQL / SQL Server / Oracle / MS Access:

3.19.6. SQL UNIQUE Constraint on ALTER TABLE

3.19.6.1. To create a UNIQUE constraint on the "P_Id" column when the table is already created, use the following SQL:

3.19.6.1.1. MySQL / SQL Server / Oracle / MS Access:

3.19.7. DROP Unique Constraint

3.19.7.1. MySQL

3.19.7.1.1. ALTER TABLE Persons DROP INDEX uc_PersonID

3.19.7.2. SQL Server / Oracle / MS Access:

3.19.7.2.1. ALTER TABLE Persons DROP CONSTRAINT uc_PersonID

3.20. SQL PRIMARY KEY

3.20.1. The PRIMARY KEY constraint uniquely identifies each record in a database table.

3.20.2. Primary keys must contain unique values.

3.20.3. A primary key column cannot contain NULL values.

3.20.4. Each table should have a primary key, and each table can have only ONE primary key.

3.20.5. SQL PRIMARY KEY Constraint on CREATE TABLE

3.20.5.1. The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Persons" table is created:

3.20.5.1.1. MySQL:

3.20.5.1.2. SQL Server / Oracle / MS Access:

3.20.5.1.3. MySQL / SQL Server / Oracle / MS Access:

3.20.6. SQL PRIMARY KEY Constraint on ALTER TABLE

3.20.6.1. To create a PRIMARY KEY constraint on the "P_Id" column when the table is already created, use the following SQL:

3.20.6.1.1. MySQL / SQL Server / Oracle / MS Access:

3.20.6.2. Note: If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created).

3.20.7. To DROP a PRIMARY KEY Constraint

3.20.7.1. MySQL:

3.20.7.1.1. ALTER TABLE Persons DROP PRIMARY KEY

3.20.7.1.2. ALTER TABLE Persons DROP CONSTRAINT pk_PersonID

3.21. SQL FOREIGN KEY

3.21.1. A FOREIGN KEY in one table points to a PRIMARY KEY in another table.

3.21.2. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.

3.21.3. The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to.

3.21.4. Code Examples

3.21.4.1. SQL FOREIGN KEY Constraint on CREATE TABLE - The following SQL creates a FOREIGN KEY on the "P_Id" column when the "Orders" table is created:

3.21.4.1.1. MySQL:

3.21.4.1.2. SQL Server / Oracle / MS Access:

3.21.4.1.3. MySQL / SQL Server / Oracle / MS Access:

3.21.4.2. SQL FOREIGN KEY Constraint on ALTER TABLE To create a FOREIGN KEY constraint on the "P_Id" column when the "Orders" table is already created, use the following SQL:

3.21.4.2.1. MySQL / SQL Server / Oracle / MS Access:

3.21.4.3. To DROP a FOREIGN KEY Constraint

3.21.4.3.1. To drop a FOREIGN KEY constraint, use the following SQL:

3.22. SQL CHECK

3.22.1. The CHECK constraint is used to limit the value range that can be placed in a column.

3.22.2. If you define a CHECK constraint on a single column it allows only certain values for this column.

3.22.3. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

3.22.4. Code Examples

3.22.4.1. SQL CHECK Constraint on CREATE TABLE

3.22.4.1.1. The following SQL creates a CHECK constraint on the "P_Id" column when the "Persons" table is created. The CHECK constraint specifies that the column "P_Id" must only include integers greater than 0.

3.22.4.2. SQL CHECK Constraint on ALTER TABLE

3.22.4.2.1. To create a CHECK constraint on the "P_Id" column when the table is already created, use the following SQL:

3.22.4.3. To DROP a CHECK Constraint

3.22.4.3.1. To drop a CHECK constraint, use the following SQL:

3.23. SQL DEFAULT

3.23.1. The DEFAULT constraint is used to insert a default value into a column.

3.23.2. The default value will be added to all new records, if no other value is specified.

3.23.3. Code Examples

3.23.3.1. SQL DEFAULT Constraint on CREATE TABLE

3.23.3.1.1. The following SQL creates a DEFAULT constraint on the "City" column when the "Persons" table is created:

3.23.3.2. SQL DEFAULT Constraint on ALTER TABLE

3.23.3.2.1. To create a DEFAULT constraint on the "City" column when the table is already created, use the following SQL:

3.23.3.3. To DROP a DEFAULT Constraint

3.23.3.3.1. MySQL:

3.23.3.3.2. SQL Server / Oracle / MS Access:

3.24. SQL CREATE INDEX

3.24.1. An index can be created in a table to find data more quickly and efficiently.

3.24.2. The users cannot see the indexes, they are just used to speed up searches/queries.

3.24.3. Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So you should only create indexes on columns (and tables) that will be frequently searched against.

3.24.4. Code Examples

3.24.4.1. Creates an index on a table. Duplicate values are allowed:

3.24.4.1.1. CREATE INDEX index_name ON table_name (column_name)

3.24.4.2. Creates a unique index on a table. Duplicate values are not allowed:

3.24.4.2.1. CREATE UNIQUE INDEX index_name ON table_name (column_name)

3.24.4.3. CREATE INDEX Example

3.24.4.3.1. The SQL statement below creates an index named "PIndex" on the "LastName" column in the "Persons" table:

3.24.4.3.2. If you want to create an index on a combination of columns, you can list the column names within the parentheses, separated by commas:

3.25. SQL DROP

3.25.1. The DROP INDEX statement is used to delete an index in a table.

3.25.2. Syntax

3.25.2.1. DROP INDEX Syntax for MS Access:

3.25.2.1.1. DROP INDEX index_name ON table_name

3.25.2.2. DROP INDEX Syntax for MS SQL Server:

3.25.2.2.1. DROP INDEX table_name.index_name

3.25.2.3. DROP INDEX Syntax for DB2/Oracle:

3.25.2.3.1. DROP INDEX index_name

3.25.2.4. DROP INDEX Syntax for MySQL:

3.25.2.4.1. ALTER TABLE table_name DROP INDEX index_name

3.25.3. The DROP TABLE Statement

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

3.25.3.1.1. DROP TABLE table_name

3.25.4. The DROP DATABASE Statement

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

3.25.4.1.1. DROP DATABASE database_name

3.25.5. The TRUNCATE TABLE Statement

3.25.5.1. What if we only want to delete the data inside the table, and not the table itself?

3.25.5.2. Then, use the TRUNCATE TABLE statement:

3.25.5.2.1. TRUNCATE TABLE table_name

3.26. SQL ALTER

3.26.1. The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

3.26.2. Code Examples

3.26.2.1. To add a column in a table, use the following syntax:

3.26.2.1.1. ALTER TABLE table_name ADD column_name datatype

3.26.2.2. To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):

3.26.2.2.1. ALTER TABLE table_name DROP COLUMN column_name

3.26.2.3. To change the data type of a column in a table, use the following syntax:

3.26.2.3.1. SQL Server / MS Access:

3.26.2.3.2. My SQL / Oracle:

3.26.2.4. Now we want to change the data type of the column named "DateOfBirth" in the "Persons" table.

3.26.2.4.1. We use the following SQL statement:

3.26.2.5. Next, we want to delete the column named "DateOfBirth" in the "Persons" table.

3.26.2.5.1. We use the following SQL statement:

3.27. SQL Auto Increment

3.27.1. Very often we would like the value of the primary key field to be created automatically every time a new record is inserted.

3.27.2. We would like to create an auto-increment field in a table.

3.27.3. The following SQL statement defines the "ID" column to be an auto-increment primary key field in the "Persons" table:

3.27.3.1. Code Examples

3.27.3.1.1. CREATE TABLE Persons ( ID int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (ID) )

3.27.3.1.2. MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature.

3.27.3.1.3. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.

3.27.3.1.4. To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:

3.27.3.1.5. To insert a new record into the "Persons" table, we will NOT have to specify a value for the "ID" column (a unique value will be added automatically):

3.27.3.2. Syntax for SQL Server

3.27.3.2.1. The following SQL statement defines the "ID" column to be an auto-increment primary key field in the "Persons" table:

3.27.3.2.2. The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.

3.27.3.2.3. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.

3.27.3.2.4. Tip: To specify that the "ID" column should start at value 10 and increment by 5, change it to IDENTITY(10,5).

3.27.3.2.5. To insert a new record into the "Persons" table, we will NOT have to specify a value for the "ID" column (a unique value will be added automatically):

3.27.3.3. Syntax for Access

3.27.3.3.1. The following SQL statement defines the "ID" column to be an auto-increment primary key field in the "Persons" table:

3.27.3.3.2. The MS Access uses the AUTOINCREMENT keyword to perform an auto-increment feature.

3.27.3.3.3. By default, the starting value for AUTOINCREMENT is 1, and it will increment by 1 for each new record.

3.27.3.3.4. Tip: To specify that the "ID" column should start at value 10 and increment by 5, change the autoincrement to AUTOINCREMENT(10,5).

3.27.3.3.5. To insert a new record into the "Persons" table, we will NOT have to specify a value for the "ID" column (a unique value will be added automatically):

3.27.3.4. Syntax for Oracle

3.27.3.4.1. In Oracle the code is a little bit more tricky.

3.27.3.4.2. You will have to create an auto-increment field with the sequence object (this object generates a number sequence).

3.27.3.4.3. Use the following CREATE SEQUENCE syntax:

3.27.3.4.4. The code above creates a sequence object called seq_person, that starts with 1 and will increment by 1. It will also cache up to 10 values for performance. The cache option specifies how many sequence values will be stored in memory for faster access. To insert a new record into the "Persons" table, we will have to use the nextval function (this function retrieves the next value from seq_person sequence):

3.28. SQL Views

3.28.1. In SQL, a view is a virtual table based on the result-set of an SQL statement.

3.28.2. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.

3.28.3. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from one single table.

3.28.4. SQL CREATE VIEW Syntax

3.28.4.1. CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

3.28.5. A view always shows up-to-date data! The database engine recreates the data, using the view's SQL statement, every time a user queries a view.

3.28.6. SQL CREATE VIEW Examples

3.28.6.1. The view "Current Product List" lists all active products (products that are not discontinued) from the "Products" table. The view is created with the following SQL:

3.28.6.1.1. CREATE VIEW [Current Product List] AS SELECT ProductID,ProductName FROM Products WHERE Discontinued=No

3.28.6.2. We can query the view above as follows:

3.28.6.2.1. SELECT * FROM [Current Product List]

3.28.6.3. Another view in the Northwind sample database selects every product in the "Products" table with a unit price higher than the average unit price:

3.28.6.3.1. CREATE VIEW [Products Above Average Price] AS SELECT ProductName,UnitPrice FROM Products WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)

3.28.6.4. We can query the view above as follows:

3.28.6.4.1. SELECT * FROM [Products Above Average Price]

3.28.6.5. Another view in the Northwind database calculates the total sale for each category in 1997. Note that this view selects its data from another view called "Product Sales for 1997":

3.28.6.5.1. CREATE VIEW [Category Sales For 1997] AS SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales FROM [Product Sales for 1997] GROUP BY CategoryName

3.28.6.6. We can query the view above as follows:

3.28.6.6.1. SELECT * FROM [Category Sales For 1997]

3.28.6.7. We can also add a condition to the query. Now we want to see the total sale only for the category "Beverages":

3.28.6.7.1. SELECT * FROM [Category Sales For 1997] WHERE CategoryName='Beverages'

3.28.7. SQL Updating a View

3.28.7.1. SQL CREATE OR REPLACE VIEW Syntax

3.28.7.1.1. CREATE OR REPLACE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

3.28.7.2. Now we want to add the "Category" column to the "Current Product List" view. We will update the view with the following SQL:

3.28.7.2.1. CREATE VIEW [Current Product List] AS SELECT ProductID,ProductName,Category FROM Products WHERE Discontinued=No

3.28.8. SQL Dropping a View

3.28.8.1. DROP VIEW view_name

3.29. SQL Dates

3.29.1. MySQL Date Functions

3.29.1.1. NOW()

3.29.1.1.1. Returns the current date and time

3.29.1.2. CURDATE()

3.29.1.2.1. Returns the current date

3.29.1.3. CURTIME()

3.29.1.3.1. Returns the current time

3.29.1.4. DATE()

3.29.1.4.1. Extracts the date part of a date or date/time expression

3.29.1.5. EXTRACT()

3.29.1.5.1. Returns a single part of a date/time

3.29.1.6. DATE_ADD()

3.29.1.6.1. Adds a specified time interval to a date

3.29.1.7. DATE_SUB()

3.29.1.7.1. Subtracts a specified time interval from a date

3.29.1.8. DATEDIFF()

3.29.1.8.1. Returns the number of days between two dates

3.29.1.9. DATE_FORMAT()

3.29.1.9.1. Displays date/time data in different formats

3.29.2. SQL Server Date Functions

3.29.2.1. GETDATE()

3.29.2.1.1. Returns the current date and time

3.29.2.2. DATEPART()

3.29.2.2.1. Returns a single part of a date/time

3.29.2.3. DATEADD()

3.29.2.3.1. Adds or subtracts a specified time interval from a date

3.29.2.4. DATEDIFF()

3.29.2.4.1. Returns the time between two dates

3.29.2.5. CONVERT()

3.29.2.5.1. Displays date/time data in different formats

3.29.3. .

3.30. SQL NULL Values

3.30.1. If a column in a table is optional, we can insert a new record or update an existing record without adding a value to this column. This means that the field will be saved with a NULL value.

3.30.2. NULL values are treated differently from other values.

3.30.3. NULL is used as a placeholder for unknown or inapplicable values.

3.30.4. Note Note: It is not possible to compare NULL and 0; they are not equivalent.

3.30.5. Code Examples

3.30.5.1. SQL IS NULL

3.30.5.1.1. SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NULL

3.30.5.2. SQL IS NOT NULL

3.30.5.2.1. SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NOT NULL

3.31. SQL NULL Functions

3.31.1. Used to specify values for NULL cells as to not interrupt calculations

3.31.2. Code Examples

3.31.2.1. SQL Server / MS Access

3.31.2.1.1. SELECT ProductName,UnitPrice*(UnitsInStock+ISNULL(UnitsOnOrder,0)) FROM Products

3.31.2.2. Oracle

3.31.2.2.1. SELECT ProductName,UnitPrice*(UnitsInStock+NVL(UnitsOnOrder,0)) FROM Products

3.31.2.3. MySQL

3.31.2.3.1. SELECT ProductName,UnitPrice*(UnitsInStock+IFNULL(UnitsOnOrder,0)) FROM Products

3.31.2.3.2. SELECT ProductName,UnitPrice*(UnitsInStock+COALESCE(UnitsOnOrder,0)) FROM Products

3.32. SQL General Data Types

3.32.1. .

4. 3 - FUNCTIONS

4.1. Overview

4.1.1. SQL Aggregate Functions

4.1.1.1. SQL aggregate functions return a single value, calculated from values in a column.

4.1.1.1.1. AVG() - Returns the average value

4.1.1.1.2. COUNT() - Returns the number of rows

4.1.1.1.3. FIRST() - Returns the first value

4.1.1.1.4. LAST() - Returns the last value

4.1.1.1.5. MAX() - Returns the largest value

4.1.1.1.6. MIN() - Returns the smallest value

4.1.1.1.7. SUM() - Returns the sum

4.1.2. SQL Scalar functions

4.1.2.1. SQL scalar functions return a single value, based on the input value.

4.1.2.1.1. UCASE() - Converts a field to upper case

4.1.2.1.2. LCASE() - Converts a field to lower case

4.1.2.1.3. MID() - Extract characters from a text field

4.1.2.1.4. LEN() - Returns the length of a text field

4.1.2.1.5. ROUND() - Rounds a numeric field to the number of decimals specified

4.1.2.1.6. NOW() - Returns the current system date and time

4.1.2.1.7. FORMAT() - Formats how a field is to be displayed

4.2. Functions

4.2.1. SQL AVG()

4.2.1.1. The AVG() function returns the average value of a numeric column.

4.2.1.2. Syntax

4.2.1.2.1. SELECT AVG(column_name) FROM table_name

4.2.1.2.2. SELECT ProductName, Price FROM Products WHERE Price>(SELECT AVG(Price) FROM Products);

4.2.2. SQL COUNT()

4.2.2.1. The COUNT() function returns the number of rows that matches a specified criteria.

4.2.2.2. Syntax

4.2.2.2.1. SQL COUNT(column_name) Syntax

4.2.2.2.2. SQL COUNT(*) Syntax

4.2.2.2.3. SQL COUNT(DISTINCT column_name) Syntax

4.2.3. SQL FIRST()

4.2.3.1. The FIRST() function returns the first value of the selected column.

4.2.3.2. Syntax

4.2.3.2.1. SQL FIRST() Syntax

4.2.3.2.2. SQL FIRST() Workaround in SQL Server, MySQL and Oracle

4.2.4. SQL LAST()

4.2.4.1. The LAST() function returns the last value of the selected column.

4.2.4.2. Syntax

4.2.4.2.1. SQL LAST() Syntax

4.2.4.2.2. SQL LAST() Workaround in SQL Server, MySQL and Oracle

4.2.5. SQL MAX()

4.2.5.1. The MAX() function returns the largest value of the selected column.

4.2.5.2. Syntax

4.2.5.2.1. SQL MAX() Syntax

4.2.6. SQL MIN()

4.2.6.1. The MIN() function returns the smallest value of the selected column.

4.2.6.2. Syntax

4.2.6.2.1. SQL MIN() Syntax

4.2.7. SQL SUM()

4.2.7.1. The SUM() function returns the total sum of a numeric column.

4.2.7.2. Syntax

4.2.7.2.1. SQL SUM() Syntax

4.2.8. SQL GROUP BY

4.2.8.1. The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

4.2.8.2. Syntax

4.2.8.2.1. SQL GROUP BY Syntax

4.2.8.2.2. .

4.2.9. SQL HAVING

4.2.9.1. The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

4.2.9.2. Syntax

4.2.9.2.1. SQL HAVING Syntax

4.2.9.2.2. .

4.2.10. SQL UCASE()

4.2.10.1. The UCASE() function converts the value of a field to uppercase.

4.2.10.2. Syntax

4.2.10.2.1. SQL UCASE() Syntax

4.2.10.2.2. Syntax for SQL Server

4.2.11. SQL LCASE()

4.2.11.1. The LCASE() function converts the value of a field to lowercase.

4.2.11.2. Syntax

4.2.11.2.1. SQL LCASE() Syntax

4.2.11.2.2. Syntax for SQL Server

4.2.12. SQL MID()

4.2.12.1. The MID() function is used to extract characters from a text field.

4.2.12.2. Syntax

4.2.12.2.1. SQL MID() Syntax

4.2.12.2.2. Parameter

4.2.12.2.3. Example

4.2.13. SQL LEN()

4.2.13.1. The LEN() function returns the length of the value in a text field.

4.2.13.2. Syntax

4.2.13.2.1. SQL LEN() Syntax

4.2.13.2.2. Example

4.2.14. SQL ROUND()

4.2.14.1. The ROUND() function is used to round a numeric field to the number of decimals specified.

4.2.14.2. Syntax

4.2.14.2.1. SQL ROUND() Syntax

4.2.14.2.2. Parameters

4.2.15. SQL NOW()

4.2.15.1. The NOW() function returns the current system date and time.

4.2.15.2. Syntax

4.2.15.2.1. SQL NOW() Syntax

4.2.16. SQL FORMAT()

4.2.16.1. The FORMAT() function is used to format how a field is to be displayed.

4.2.16.2. Syntax

4.2.16.2.1. SQL FORMAT() Syntax

4.2.16.3. Parameters

4.2.16.3.1. column_name

4.2.16.3.2. format

4.2.16.3.3. Example

5. DATA TYPES

5.1. Microsoft Access Data Types

5.1.1. Text

5.1.1.1. Use for text or combinations of text and numbers. 255 characters maximum

5.1.2. Memo

5.1.2.1. Memo is used for larger amounts of text. Stores up to 65,536 characters.Note: You cannot sort a memo field. However, they are searchable

5.1.3. Byte

5.1.3.1. Allows whole numbers from 0 to 255

5.1.3.2. 1 byte

5.1.4. Integer

5.1.4.1. Allows whole numbers between -32,768 and 32,767

5.1.4.2. 2 bytes

5.1.5. Long

5.1.5.1. Allows whole numbers between -2,147,483,648 and 2,147,483,647

5.1.5.2. 4 bytes

5.1.6. Single

5.1.6.1. Single precision floating-point. Will handle most decimals

5.1.6.2. 4 bytes

5.1.7. Double

5.1.7.1. Double precision floating-point. Will handle most decimals

5.1.7.2. 8 bytes

5.1.8. Currency

5.1.8.1. Use for currency. Holds up to 15 digits of whole dollars, plus 4 decimal places.Tip: You can choose which country's currency to use

5.1.8.2. 8 bytes

5.1.9. AutoNumber

5.1.9.1. AutoNumber fields automatically give each record its own number, usually starting at 1

5.1.9.2. 4 bytes

5.1.10. Date/Time

5.1.10.1. Use for dates and times

5.1.10.2. 8 bytes

5.1.11. Yes/No

5.1.11.1. A logical field can be displayed as Yes/No, True/False, or On/Off. In code, use the constants True and False (equivalent to -1 and 0). Note: Null values are not allowed in Yes/No fields

5.1.11.2. 1 bit

5.1.12. Ole Object

5.1.12.1. Can store pictures, audio, video, or other BLOBs (Binary Large OBjects)

5.1.12.2. up to 1GB

5.1.13. Hyperlink

5.1.13.1. Contain links to other files, including web pages

5.1.14. Lookup Wizard

5.1.14.1. Let you type a list of options, which can then be chosen from a drop-down list

5.1.14.2. 4 bytes

5.2. MySQL Data Types

5.2.1. Text Types

5.2.1.1. SET

5.2.1.1.1. Similar to ENUM except that SET may contain up to 64 list items and can store more than one choice

5.2.1.2. VARCHAR(size)

5.2.1.2.1. Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis. Can store up to 255 characters. Note: If you put a greater value than 255 it will be converted to a TEXT type

5.2.1.3. TINYTEXT

5.2.1.3.1. Holds a string with a maximum length of 255 characters

5.2.1.4. TEXT

5.2.1.4.1. Holds a string with a maximum length of 65,535 characters

5.2.1.5. BLOB

5.2.1.5.1. For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data

5.2.1.6. MEDIUMTEXT

5.2.1.6.1. Holds a string with a maximum length of 16,777,215 characters

5.2.1.7. MEDIUMBLOB

5.2.1.7.1. For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data

5.2.1.8. LONGTEXT

5.2.1.8.1. Holds a string with a maximum length of 4,294,967,295 characters

5.2.1.9. LONGBLOB

5.2.1.9.1. For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data

5.2.1.10. ENUM(x,y,z,etc.)

5.2.1.10.1. Let you enter a list of possible values. You can list up to 65535 values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted.

5.2.1.10.2. Note: The values are sorted in the order you enter them.

5.2.1.10.3. You enter the possible values in this format: ENUM('X','Y','Z')

5.2.1.11. CHAR(size)

5.2.1.11.1. Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis. Can store up to 255 characters

5.2.2. Number Types

5.2.2.1. TINYINT(size)

5.2.2.1.1. -128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits may be specified in parenthesis

5.2.2.2. SMALLINT(size)

5.2.2.2.1. -32768 to 32767 normal. 0 to 65535 UNSIGNED*. The maximum number of digits may be specified in parenthesis

5.2.2.3. MEDIUMINT(size)

5.2.2.3.1. -8388608 to 8388607 normal. 0 to 16777215 UNSIGNED*. The maximum number of digits may be specified in parenthesis

5.2.2.4. INT(size)

5.2.2.4.1. -2147483648 to 2147483647 normal. 0 to 4294967295 UNSIGNED*. The maximum number of digits may be specified in parenthesis

5.2.2.5. BIGINT(size)

5.2.2.5.1. -9223372036854775808 to 9223372036854775807 normal. 0 to 18446744073709551615 UNSIGNED*. The maximum number of digits may be specified in parenthesis

5.2.2.6. FLOAT(size,d)

5.2.2.6.1. A small number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter

5.2.2.7. DOUBLE(size,d)

5.2.2.7.1. A large number with a floating decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter

5.2.2.8. DECIMAL(size,d)

5.2.2.8.1. A DOUBLE stored as a string , allowing for a fixed decimal point. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter

5.2.3. Date Types

5.2.3.1. DATE()

5.2.3.1.1. A date. Format: YYYY-MM-DD

5.2.3.1.2. Note: The supported range is from '1000-01-01' to '9999-12-31'

5.2.3.2. DATETIME()

5.2.3.2.1. *A date and time combination. Format: YYYY-MM-DD HH:MM:SS

5.2.3.2.2. Note: The supported range is from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'

5.2.3.3. TIMESTAMP()

5.2.3.3.1. *A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch ('1970-01-01 00:00:00' UTC). Format: YYYY-MM-DD HH:MM:SS

5.2.3.3.2. Note: The supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC

5.2.3.4. TIME()

5.2.3.4.1. A time. Format: HH:MM:SS

5.2.3.4.2. Note: The supported range is from '-838:59:59' to '838:59:59'

5.2.3.5. YEAR()

5.2.3.5.1. A year in two-digit or four-digit format.

5.2.3.5.2. Note: Values allowed in four-digit format: 1901 to 2155. Values allowed in two-digit format: 70 to 69, representing years from 1970 to 2069

5.3. SQL Server Data Types

5.3.1. String Types

5.3.1.1. char(n)

5.3.1.1.1. Fixed width character string. Maximum 8,000 characters

5.3.1.1.2. Defined width

5.3.1.2. varchar(n)

5.3.1.2.1. Variable width character string. Maximum 8,000 characters

5.3.1.2.2. 2 bytes + number of chars

5.3.1.3. varchar(max)

5.3.1.3.1. Variable width character string. Maximum 1,073,741,824 characters

5.3.1.3.2. 2 bytes + number of chars

5.3.1.4. text

5.3.1.4.1. Variable width character string. Maximum 2GB of text data

5.3.1.4.2. 4 bytes + number of chars

5.3.1.5. nchar

5.3.1.5.1. Fixed width Unicode string. Maximum 4,000 characters

5.3.1.5.2. Defined width x 2

5.3.1.6. nvarchar

5.3.1.6.1. Variable width Unicode string. Maximum 4,000 characters

5.3.1.7. nvarchar(max)

5.3.1.7.1. Variable width Unicode string. Maximum 536,870,912 characters

5.3.1.8. ntext

5.3.1.8.1. Variable width Unicode string. Maximum 2GB of text data

5.3.1.9. bit

5.3.1.9.1. Allows 0, 1, or NULL

5.3.1.10. binary(n)

5.3.1.10.1. Fixed width binary string. Maximum 8,000 bytes

5.3.1.11. varbinary

5.3.1.11.1. Variable width binary string. Maximum 8,000 bytes

5.3.1.12. varbinary(max)

5.3.1.12.1. Variable width binary string. Maximum 2GB

5.3.1.13. image

5.3.1.13.1. Variable width binary string. Maximum 2GB

5.3.2. Number Types

5.3.2.1. tinyint

5.3.2.1.1. Allows whole numbers from 0 to 255

5.3.2.1.2. 1 byte

5.3.2.2. smallint

5.3.2.2.1. Allows whole numbers between -32,768 and 32,767

5.3.2.2.2. 2 bytes

5.3.2.3. int

5.3.2.3.1. Allows whole numbers between -2,147,483,648 and 2,147,483,647

5.3.2.3.2. 4 bytes

5.3.2.4. bigint

5.3.2.4.1. Allows whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807

5.3.2.4.2. 8 bytes

5.3.2.5. decimal(p,s)

5.3.2.5.1. Fixed precision and scale numbers.

5.3.2.5.2. Allows numbers from -10^38 +1 to 10^38 –1.

5.3.2.5.3. The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the decimal point). p must be a value from 1 to 38. Default is 18.

5.3.2.5.4. The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p. Default value is 0

5.3.2.5.5. 5-17 bytes

5.3.2.6. numeric(p,s)

5.3.2.6.1. Fixed precision and scale numbers.

5.3.2.6.2. Allows numbers from -10^38 +1 to 10^38 –1.

5.3.2.6.3. The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the decimal point). p must be a value from 1 to 38. Default is 18.

5.3.2.6.4. The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p. Default value is 0

5.3.2.6.5. 5-17 bytes

5.3.2.7. smallmoney

5.3.2.7.1. Monetary data from -214,748.3648 to 214,748.3647

5.3.2.7.2. 4 bytes

5.3.2.8. money

5.3.2.8.1. Monetary data from -922,337,203,685,477.5808 to 922,337,203,685,477.5807

5.3.2.8.2. 8 bytes

5.3.2.9. float(n)

5.3.2.9.1. Floating precision number data from -1.79E + 308 to 1.79E + 308.

5.3.2.9.2. The n parameter indicates whether the field should hold 4 or 8 bytes. float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default value of n is 53.

5.3.2.9.3. 4 or 8 bytes

5.3.2.10. real

5.3.2.10.1. Floating precision number data from -3.40E + 38 to 3.40E + 38

5.3.2.10.2. 4 bytes

5.3.3. Date Types

5.3.3.1. datetime

5.3.3.1.1. From January 1, 1753 to December 31, 9999 with an accuracy of 3.33 milliseconds

5.3.3.1.2. 8 bytes

5.3.3.2. datetime2

5.3.3.2.1. From January 1, 0001 to December 31, 9999 with an accuracy of 100 nanoseconds

5.3.3.2.2. 6-8 bytes

5.3.3.3. smalldatetime

5.3.3.3.1. From January 1, 1900 to June 6, 2079 with an accuracy of 1 minute

5.3.3.3.2. 4 bytes

5.3.3.4. date

5.3.3.4.1. Store a date only. From January 1, 0001 to December 31, 9999

5.3.3.4.2. 3 bytes

5.3.3.5. time

5.3.3.5.1. Store a time only to an accuracy of 100 nanoseconds

5.3.3.5.2. 3-5 bytes

5.3.3.6. datetimeoffset

5.3.3.6.1. The same as datetime2 with the addition of a time zone offset

5.3.3.6.2. 8-10 bytes

5.3.3.7. timestamp

5.3.3.7.1. Stores a unique number that gets updated every time a row gets created or modified. The timestamp value is based upon an internal clock and does not correspond to real time. Each table may have only one timestamp variable

5.3.4. Other data types

5.3.4.1. sql_variant

5.3.4.1.1. Stores up to 8,000 bytes of data of various data types, except text, ntext, and timestamp

5.3.4.2. uniqueidentifier

5.3.4.2.1. Stores a globally unique identifier (GUID)

5.3.4.3. xml

5.3.4.3.1. Stores XML formatted data. Maximum 2GB

5.3.4.4. cursor

5.3.4.4.1. Stores a reference to a cursor used for database operations

5.3.4.5. table

5.3.4.5.1. Stores a result-set for later processing