SQL Bsics
SQL Bsics
It divides larger tables to smaller tables and links them using relationships
-128 to 127 normal. 0 to 255 UNSIGNED*. The maximum number of digits may be specified in
TINYINT(size) parenthesis
-32768 to 32767 normal. 0 to 65535 UNSIGNED*. The maximum number of digits may be specified in
SMALLINT(size) parenthesis
-8388608 to 8388607 normal. 0 to 16777215 UNSIGNED*. The maximum number of digits may be
MEDIUMINT(size) specified in parenthesis
-2147483648 to 2147483647 normal. 0 to 4294967295 UNSIGNED*. The maximum number of digits
INT(size) may be specified in parenthesis
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
DECIMAL(size,d) specified in the d parameter
Text Data type Description
CHAR(size) 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
VARCHAR(size) 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
TINYTEXT Holds a string with a maximum length of 255 characters
TEXT Holds a string with a maximum length of 65,535 characters
BLOB For BLOBs (Binary Large Objects). Holds up to 65,535 bytes of data
MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters
MEDIUMBLOB For BLOBs (Binary Large Objects). Holds up to 16,777,215 bytes of data
LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters
LONGBLOB For BLOBs (Binary Large Objects). Holds up to 4,294,967,295 bytes of data
ENUM(x,y,z,etc.) 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.
Note: The values are sorted in the order you enter them.
You enter the possible values in this format: ENUM('X','Y','Z')
SET Similar to ENUM except that SET may contain up to 64 list items and can store more than one choice
String Data type Description Max size Storage
varchar(max) Variable width character string 1,073,741,824 characters 2 bytes + number of chars
text Variable width character string 2GB of text data 4 bytes + number of chars
nchar Fixed width Unicode string 4,000 characters Defined width x 2
nvarchar Variable width Unicode string 4,000 characters
nvarchar(max) Variable width Unicode string 536,870,912 characters
TIMESTAMP() *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:MI:SS
Note: The supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC
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
SQL Operators
• Arithmetic Operators
• Comparison Operators
• Logical Operators
• Bitwise Operators
• Compound Operators
Arithmetic Operators:
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulo
Comparison Operators:
Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
Logical Operators:
Operator Description
ALL TRUE if all of the subquery values meet the condition
AND TRUE if all the conditions separated by AND is TRUE
ANY TRUE if any of the subquery values meet the condition
BETWEEN TRUE if the operand is within the range of comparisons
EXISTS TRUE if the subquery returns one or more records
IN TRUE if the operand is equal to one of a list of expressions
LIKE TRUE if the operand matches a pattern
NOT Displays a record if the condition(s) is NOT TRUE
OR TRUE if any of the conditions separated by OR is TRUE
SOME TRUE if any of the subquery values meet the condition
Bitwise Operators:
Operator Description
SQL Constraints are rules used to limit the type of data that can go into a
table, to maintain the accuracy and integrity of the data inside table.
However, you can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.
SQL UNIQUE Constraint on CREATE TABLE
The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is created:
SQL Server / Oracle / MS Access:
CREATE TABLE Persons (
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
SQL PRIMARY KEY Constraint
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only one primary key, which may consist of single or multiple fields .
SQL PRIMARY KEY on CREATE TABLE
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is created:
SQL Server / Oracle / MS Access:
Example:
DROP TABLE Shippers;
SQL ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.
ALTER TABLE - ADD Column
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE - DROP COLUMN
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE – MODIFY COLUMN
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
SQL TRUNCATE TABLE
The Truncate Table commands deletes the data inside a table, but not
the table itself
The following SQL truncates the table "Categories“
Example:
Examples:
SELECT CustomerName, City FROM Customers;
SELECT * FROM Customers;
SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
Examples:
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt’
WHERE CustomerID = 1;
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
SQL DELETE Statement
DELETE Syntax
DELTE FROM table_name WHERE condition;
Example:
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste’;
Delete All Records
It is possible to delete all rows in a table without deleting the table. This means that
the table structure, attributes, and indexes will be intact:
Example:
DELETE FROM Customers;
Important SQL SELECT Statement
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
SELECT Syntax
SELECT column1, column2, ...
FROM table_name;
Examples:
SELECT * FROM Customers;
SELECT CustomerName, City FROM Customers;
SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different)
values.
Examples:
SELECT DISTINCT Country FROM Customers;
And Example:
SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin’;
OR Example:
SELECT * FROM Customers WHERE City='Berlin' OR City='München’;
NOT Example
SELECT * FROM Customers WHERE NOT Country='Germany';
Combining AND, OR and NOT
Examples:
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München’);
Examples:
SELECT COUNT(ProductID) FROM Products;
SELECT AVG(Price) FROM Products;
SELECT SUM(Quantity) FROM OrderDetails;
SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified
pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
• % - The percent sign represents zero, one, or multiple characters
• _ - The underscore represents a single character
SELECT * FROM Customers WHERE CustomerName LIKE 'a%';
SELECT * FROM Customers WHERE CustomerName LIKE '%a’;
SELECT * FROM Customers WHERE CustomerName LIKE '%or%’;
SELECT * FROM Customers WHERE CustomerName LIKE '_r%';
SELECT * FROM Customers WHERE CustomerName LIKE 'a_%_%’
SELECT * FROM Customers WHERE ContactName LIKE 'a%o’;
SELECT * FROM Customers WHERE CustomerName NOT LIKE 'a%';
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based
on a related column between them.
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
• INNER JOIN: Returns records that have matching values in both tables
• LEFT JOIN: Return all records from the left table, and the matched
records from the right table
• RIGHT JOIN: Return all records from the right table, and the matched
records from the left table
• FULL JOIN: Return all records when there is a match in either left or
right table
SQL INNER JOIN Keyword
The INNER JOIN keyword selects records that have matching values in
both tables.
Examples:
SELECT Orders.OrderID, Customers.CustomerName FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Example:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
SQL Self JOIN
A self JOIN is a regular join, but the table is joined with itself.
Example:
SELECT A.CustomerName AS CustomerName1,
B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;