Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views25 pages

SQL Notes

Uploaded by

sindhuakka932
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

SQL Notes

Uploaded by

sindhuakka932
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Select Statement - It is used to select data from the database.

The data is
stored in a result table called the result set.

SELECT CustomerName, City FROM Customers;


SELECT * FROM Customers; /*Select Everything*/
SELECT DISTINCT Country FROM Customers;
SELECT COUNT(DISTINCT Country) FROM Customers;
SELECT * FROM Customers WHERE Country = 'Mexico'; /*cond*/

Operators in the Where Clause -

<> or != Not Equal

BETWEEN Between a certain range

LIKE Search for a pattern

IN Multiple possible values for a column

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


SELECT * FROM Customers Where City LIKE '%s';
SELECT * FROM Products WHERE Price BETWEEN 50 and 60;

AND, OR and NOT Operators in Where Clause -

SELECT * FROM Customers WHERE Country = 'A' and City = 'B';


SELECT * FROM Customers WHERE City = 'A' OR City = 'B';
SELECT * FROM Customers WHERE NOT Country = 'A';

ORDER BY Keyword - It is used to sort the result set in ascending or


descending order. It is set to ascending order by default. Order by DESC has
to be mentioned, order by multiple column

SELECT * FROM Customers ORDER BY Country ASC, Customer DESC;


SELECT * FROM Customers ORDER BY Country DESC;
INSERT INTO Statement - It inserts new records in a table. There are two
ways:

1. Specify both the column name and the values to be inserted.


INSERT INTO table_name (col1, col2) VALUES
(val1, val2);

2. If you add values in all the columns, then the name need not be
specified.
INSERT INTO table_name VALUES (val1, val2)

NULL Operators -

SELECT Name FROM Customers WHERE Address IS NULL;


SELECT Name FROM Customers WHERE Address IS NOT NULL;

UPDATE Statement - It is used to modify the existing records of a table.

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;

DELETE Command -

DELETE FROM Customers; /*Delete Everything*/


DELETE FROM Customers WHERE CustomerName = 'Alfreds';

SELECT TOP Clause - This specifies the number of records to return, it is


helpful on large tables because returning a large number of records can
impact performance.

SELECT TOP 3 * FROM Customers;


SELECT TOP 50 PERCENT * FROM Customers;
Basic Mathematics Function -

SELECT MAX(Price) AS LargestPrice FROM Products;


SELECT COUNT/ AVG/ SUM(Price) FROM OrderDetails;

Like Operator -
1. The % sign is used to represent 0, 1 or multiple characters.
2. The _ represents one, single character.

SELECT * FROM Customers WHERE CustomerName LIKE 'a%';

Wildcard Characters in SQL Server -

Symbol Description Example

% Represents 0 or more bl% finds bl, black, blue,


characters and blob

_ Represents a single H_t finds hot, hat, and


character hit

[] Represents a single h[oa]t finds hot and hat,


character within the but not hit
brackets

^ Represents any h[^oa]t finds hit, but not


character not in the hot and hat
brackets

- Represents any single C[a-b]t finds cat and cbt


character within the
specified range

SQL IN Operator - It allows specifying multiple values in a WHERE clause. It


is a shorthand for multiple OR conditions.

SELECT * FROM Customers


WHERE Country NOT IN ('Germany', 'France', 'UK')

SELECT * FROM Customers


WHERE Country IN (SELECT Country FROM Suppliers)
SQL BETWEEN Operator - It selects values within a given range. It is
inclusive, begin and end values are included.

SELECT * FROM Products


WHERE Price BETWEEN 10 and 20
AND CategoryID NOT IN (1, 2, 3);

SELECT * FROM Orders


WHERE OrderDate BETWEEN #07/01/1996# AND #07/31/1996#

SQL Aliases - They are used to give a table or column a temporary name.
They make the column name more readable. It exists only for the duration of
the query. It is created with the AS keyword.

/*Double quote or square brackets are required if the alias


name has spaces*/

SELECT CustomerName AS Customer, ContactName AS [Contact


Person]
FROM Customers;

/*You can combine multiple columns to make one alias*/

SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City


+ ', ' + Country AS Address
FROM Customers;

/*Naming tables as an alias to make the code shorter*/

SELECT o.OrderID, o.OrderDate, c.CustomerName


FROM Customers AS c, Orders AS o
WHERE c.CustomerName = 'Around the Horm' AND c.CustomerID =
o.CustoemrID

/*Same code without aliases*/

SELECT Orders.OrderID, Orders.OrderDate,


Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName='Around the Horn' AND
Customers.CustomerID=Orders.CustomerID;

SQL Join Clause - It is used to combine rows from two or more tables, based
on a related column between them.

SELECT Orders.OrderID, Customers.CustomerName,


Orders.OrderDate
FROM Orders
INNER JOIN Customers ON
Orders.CustomerID=Customers.CustomerID;

Union Operator - It is used to combine the result set of two or more SELECT
statements. There are 2 requirements:
1. Every SELECT statement with UNION must have the same number of
columns.
2. The columns must also have similar data types.
3. The columns in every SELECT statement must also be in the same
order.

SELECT column_name(s) FROM table1


UNION /*You can use UNION ALL to allow duplicate values*/
SELECT column-name(s) FROM table2;

SQL Group By - It groups rows that have the same value into summary rows,
like “find the number of customers in each country”. It is used with aggregate
functions like COUNT(), MAX() and SUM() etc.

SELECT COUNT(CustomerID), Country FROM Customers


GROUP BY Country ORDER BY COUNT(CustomerID) DESC;
Aggregate Functions - Special functions that operate on a set of values and
return a single computed value based on that set.

HAVING Clause - It was added to SQL because the WHERE keyword cannot
be used with aggregate functions.

SELECT Employees.LastName, COUNT(Orders.OrderID) AS


NumberOfOrders
FROM (Orders)
INNER JOIN Employees ON Orders.EmployeeID =
Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;

SQL Exists - It checks for the existence of a record in a subquery.

SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE
Products.SupplierID = Suppliers.supplierID AND Price < 20);

ANY Operator - It returns a boolean value as a result. It returns true if any of


the subquery values meet the condition.

SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);

ALL Operator - It returns a boolean value as a result. It returns TRUE if ALL


the subquery values meet the condition, it is used with SELECT, WHERE and
HAVING statements.

SELECT ALL ProductName


FROM Products WHERE TRUE;
SELECT INTO Statement - It copies data from one table into a new table.

SELECT Customers.CustomerName, Orders.OrderID


INTO CustomersOrderBackup2017
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

INSERT INTO SELECT Statement - This statement copies data from one
table and inserts it into another table, it requires that the data types in the
source and target tables match.

CASE Expression - It goes through conditions and returns a value when the
first condition is met. So, once a condition is true, it will stop reading and
return the result. If no conditions are true, it returns the value in the ELSE
clause.

If there is no ELSE part and no condition is true, it returns NULL.

SELECT OrderID, Quantity,


CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;

IFNULL() - It returns an alternative value if an expression is NULL, another


function COALESCE() also does the same.

SELECT ProductName, UnitPrice * (UnitsInStock +


IFNULL(UnitsOnOrder, 0))
FROM Products;

SELECT ProductName, UnitPrice * (Stock + COALESCE (Order, 0))


FROM Products;

Stored Procedure - It is a prepared SQL code that you can save, so the code
can be reused over and over again. You can also pass parameters to a stored
procedure so that the stored procedure can act based on the parameter
value(s) that is passed.

/*Create the procedure*/

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30),


@PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode =
@PostalCode
GO;

/*Execute the procedure*/

EXEC SelectAllCustomers @City = 'London', @PostalCode = 'WA1'

Comments in SQL -

--Single Line Comment


/*Multi Line
Comment*/

SQL Arithmetic Operators -

Operator Description

+ Add

- Subtract

* Multiply

/ Divide

% Modulo
SQL Bitwise Operators -

Operators Description

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

SQL Comparison Operators -

Operator Description

= Equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

<> Not equal to

SQL Compound Operators -

Operator Description

+= Add equals

-= Subtract equals

*= Multiply equals

/= Divide equals

%= Modulo equals

&= Bitwise AND equals

^-= Bitwise exclusive equals

|*= Bitwise OR equals


SQL Logical Operators -

Operator Description

ALL TRUE if all 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 comparison

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

CREATE DATABASE - This creates a new SQL database.

CREATE DATABASE testDB;

DROP DATABASE - It is used to drop an existing SQL database.

DROP DATABASE testDB;


BACKUP DATABASE -

BACKUP DATABASE testDB


TO DISK = 'D:\backups\testDB.bak';

BACKUP DATABASE testDB


TO DISK = 'D:\backups\testDB.bak'
WITH DIFFERENTIAL

Full Backup - The initial backup is a complete backup of the entire database,
capturing all the data and structures at that point in time.

Differential Backup - After the full backup, subsequent differential backups


only include the data that has changed since the last full backup. It means that
any modifications, additions, or deletions made to the database since the last
full backup will be included in the differential backup.

SQL Create Table -

CREATE TABLE Persons (


PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

PersonID LastName FirstName Address City

Create Table using another Table -

CREATE TABLE TestTable AS


SELECT customername, contact name
FROM customers;
DROP TABLE - Deletes the entire table.

DROP TABLE Shippers;

TRUNCATE TABLE - It deletes the data of the table and not the table.

TRUNCATE TABLE table_name;

ALTER TABLE - It is used to add, delete, or modify columns in an existing


table. It is also used to add and drop various constraints on an existing table.

ADD Column -

ALTER TABLE Customers


ADD Email varchar(255);

DROP Column -

ALTER TABLE Customers


DROP COLUMN Email;

RENAME COLUMN -

ALTER TABLE table_name


RENAME COLUMN old_name to new_name;

ALTER/ MODIFY Datatype -

ALTER TABLE table_name


ALTER COLUMN column_name datatype;

SQL CREATE Constraints - They can be specified when the table is created
with the CREATE TABLE statement, or after the table is created with the
ALTER TABLE statement.

They specify the rules for the data in a table. They limit the type of data that
can go into a table. They ensure the accuracy and reliability of the data in the
table. If there is any violation, the action is aborted. They can be column level
or table level.

Constraints Description

NOT NULL Ensures that a column cannot have


a null value

UNIQUE Ensures that all values in a column


are different

PRIMARY KEY A combination of a NOT NULL and


UNIQUE. Uniquely identifies each
row in a table

FOREIGN KEY Prevents actions that would destroy


links between tables

CHECK Ensures that the value in a column


satisfies a specific condition

DEFAULT Sets a default value for a column if


no value is specified

CREATE INDEX Used to create and retrieve data


from the database very quickly

SQL NOT NULL on CREATE TABLE -

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);

On Alter Table -

ALTER TABLE Persons


ALTER COLUMN Age int NOT NULL;
SQL UNIQUE Constraint - It ensures that all the values are different. Both the
UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness
for a column or set of columns.

A PRIMARY KEY constraint already has a UNIQUE constraint. You may have
multiple UNIQUE constraints per table, but only one PRIMARY KEY constraint
per table.

CREATE TABLE Persons (


ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);

ALTER TABLE Persons


ADD CONSTRAINT UC_Person UNIQUE (ID, LastName);

ALTER TABLE Persons


DROP INDEX UC_Person;

SQL PRIMARY KEY Constraint - It uniquely identifies each record in a table.


It has to be UNIQUE and NOT NULL. A table can have only one primary key,
the primary key can consist of single or multiple columns (fields).

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);

ALTER TABLE Persons


ADD PRIMARY KEY (ID);

ALTER TABLE Persons /*Remove Primary Key*/


DROP PRIMARY KEY;
SQL Foreign Key Constraint - It is used to prevent activities that would
destroy links between tables. It is a field in one table, that refers to the
PRIMARY KEY in another table.

The table with the foreign key is called the child table, and the table with the
primary key is called the referenced or parent table.

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

ALTER TABLE Orders


ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

ALTER TABLE Orders


DROP FOREIGN KEY FK_PersonOrder;

SQL CHECK Constraint - It is used to limit the value range that can be
placed in a column. If CHECK is defined for a column, it will allow only certain
values for the column.

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.

CREATE TABLE Persons (


Age int,
CHECK (Age>=18)
);

ALTER TABLE Persons


ADD CHECK (Age>=18);

ALTER TABLE Persons


DROP CONSTRAINT CHK_PersonAge;
DEFAULT Constraint - It is used to set a default value for a column, it will be
added to all new records, if no other value is specified.

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);

ALTER TABLE Persons


ALTER City SET DEFAULT 'Sandnes';

ALTER TABLE Persons


ALTER City DROP DEFAULT;

CREATE INDEX Statement - It is used to create indexes in tables. They are


used to retrieve data from the database more quickly than otherwise. The
users cannot see the indexes, they are just used to speed up searches/
queries.

CREATE INDEX idx_pname


ON Persons (LastName, FirstName);

ALTER TABLE table_name


DROP INDEX index_name;

SQL AUTO INCREMENT Field - It allows a unique number to be generated


automatically when a new record is inserted into a table. Often this is the
primary key field that we would like to be created automatically every time a
new record is inserted.

CREATE TABLE Persons (


Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);

ALTER TABLE Persons AUTO_INCREMENT=100;

INSERT INTO Persons (FirstName, LastName)


VALUES ('Lars', 'Monsen');

SQL Date Data Types -

Data Types Format

DATE YYYY-MM-DD

DATETIME YYYY-MM-DD HH:MI:SS

TIMESTAMP YYYY-MM-DD HH:MI:SS

YEAR YYYY or YY

SELECT * FROM Orders WHERE OrderDate='2008-11-11'

SQL CREATE VIEW Statement - In SQL, a view is a virtual table based on


the result set of an SQL statement. You can add SQL statements and
functions to a view and present the data as if the data were coming from one
single table.

CREATE VIEW [Brazil Customers] AS


SELECT CustomerName, ContactName
FROM Customers

CREATE OR REPLACE VIEW [Brazil Customers] AS /*Updating*/


SELECT CustomerName, ContactName, City
FROM Customers

DROP VIEW [Brazil Customers];


SQL Hosting - If you want your web site to be able to store and retrieve data
from a database, your web server should have access to a database system
that uses the SQL language.

If your web server is hosted by an ISP (Internet Service Provider), you will
have to look for SQL hosting plans.

The most common SQL hosting databases are MS SQL SERVER, ORACLE,
MySQL, and MS ACCESS.

Attribute

Tuple

Relation

MySQL Data Types (Version 8.0)

Data type Description

sql_variant Stores up to 8,000 bytes of data of


various data types, except text,
ntext, and timestamp

uniqueidentifier Stores a globally unique identifier


(GUID)

xml Stores XML formatted data.


Maximum 2GB

cursor Stores a reference to a cursor used


for database operations

table Stores a result-set for later


processing

Data type Description Storage

datetime From January 1, 1753, to December 8 bytes


31, 9999 with an accuracy of 3.33
milliseconds

datetime2 From January 1, 0001 to December 6 - 8 bytes


31, 9999 with an accuracy of 100
nanoseconds

smalldatetime From January 1, 1900, to June 6, 4 bytes


2079, with an accuracy of 1 minute

date Store a date only. From January 1, 3 bytes


0001 to December 31, 9999

time Store a time only to an accuracy of 3 - 5 bytes


100 nanoseconds

datetimeoffset The same as datetime2 with the 8 - 10 bytes


addition of a time zone offset

timestamp 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

Data type Description Storage

bit Integer that can be 0, 1,


or NULL

tinyint Allows whole numbers 1 byte


from 0 to 255

smallint Allows whole numbers 2 bytes


between -32,768 and
32,767

int Allows whole numbers 4 bytes


between
-2,147,483,648 and
2,147,483,647

bigint Allows whole numbers 8 bytes


between
-9,223,372,036,854,775
,808 and
9,223,372,036,854,775,
807
decimal(p, s) Fixed precision and 5 - 17 bytes
scale numbers.

Allows numbers from


-10^38 +1 to 10^38 –1.

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. The
default is 18.

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

numeric(p, s) Fixed precision and 5 - 17 bytes


scale numbers.

Allows numbers from


-10^38 + 1 to 10^38 - 1.

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

The s parameter
indicates the maximum
number of digits stored
to the right of the
decimal point, s must
be a value from 0 top.
The default value is 0.

smallmoney Monetary data from 4 bytes


-214,748.3648 to
214,748.3647

money Monetary data from 8 bytes


-922,337,203,685,477.5
808 to
922,337,203,685,477.5
807

float(n) Floating precision 4 or 8 bytes


number data from
-1.79E + 308 to 1.79E +
308.

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. The default
value of n is 53.

real Floating precision 4 bytes


number data from
-3.40E + 38 to 3.40E +
38

Data type Description

DATE A date. Format: YYYY-MM-DD. The


supported range is from '1000-01-01'
to '9999-12-31'

DATETIME(fsp) A date and time combination.


Format: YYYY-MM-DD hh:mm:ss.
The supported range is from
'1000-01-01 00:00:00' to '9999-12-31
23:59:59'. Adding DEFAULT and ON
UPDATE in the column definition to
get automatic initialization and
updating to the current date and time

TIMESTAMP(fsp) 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. The
supported range is from '1970-01-01
00:00:01' UTC to '2038-01-09
03:14:07' UTC. Automatic
initialization and updating to the
current date and time can be
specified using DEFAULT
CURRENT_TIMESTAMP and ON
UPDATE CURRENT_TIMESTAMP
in the column definition

TIME(fsp) A time. Format: hh:mm:ss. The


supported range is from '-838:59:59'
to '838:59:59'

YEAR A year in four-digit format. Values


allowed in the four-digit format: 1901
to 2155, and 0000.
MySQL 8.0 does not support year in
two-digit format.

Data type Description

BIT(size) A bit-value type. The number of bits per


value is specified in size. The size
parameter can hold a value from 1 to 64.
The default value for size is 1.

TINYINT(size) A very small integer. The signed range is


from -128 to 127. The unsigned range is
from 0 to 255. The size parameter specifies
the maximum display width (which is 255)

BOOL Zero is considered as false, and nonzero


values are considered as true.

BOOLEAN Equal to BOOL


SMALLINT(size) A small integer. The signed range is from
-32768 to 32767. The unsigned range is
from 0 to 65535. The size parameter
specifies the maximum display width
(which is 255)

MEDIUMINT(size) A medium integer. The signed range is


from -8388608 to 8388607. The unsigned
range is from 0 to 16777215. The size
parameter specifies the maximum display
width (which is 255)

INT(size) A medium integer. The signed range is


from -2147483648 to 2147483647. The
unsigned range is from 0 to 4294967295.
The size parameter specifies the maximum
display width (which is 255)

INTEGER(size) Equal to INT(size)

BIGINT(size) A large integer. The signed range is from


-9223372036854775808 to
9223372036854775807. The unsigned
range is from 0 to
18446744073709551615. The size
parameter specifies the maximum display
width (which is 255)

FLOAT(size, d) A floating point number. The total number


of digits is specified in size. The number of
digits after the decimal point is specified in
the d parameter. This syntax is deprecated
in MySQL 8.0.17, and it will be removed in
future MySQL versions

FLOAT(p) A floating point number. MySQL uses the


p-value to determine whether to use
FLOAT or DOUBLE for the resulting data
type. If p is from 0 to 24, the data type
becomes FLOAT(). If p is from 25 to 53, the
data type becomes DOUBLE()

DOUBLE(size, d) A normal-size floating point number. The


total number of digits is specified in size.
The number of digits after the decimal point
is specified in the d parameter
DOUBLE PRECISION(size, d) Data type for storing floating-point numbers
with double precision, where ’size’
represents the total number of digits and ‘d’
represents the number of decimal places. It
is commonly used for precise numeric data.

DECIMAL(size, d) An exact fixed-point number. The total


number of digits is specified in size. The
number of digits after the decimal point is
specified in the d parameter. The maximum
number for size is 65. The maximum
number for d is 30. The default value for
size is 10. The default value for d is 0. An
exact fixed-point number. The total number
of digits is specified in size. The number of
digits after the decimal point is specified in
the d parameter. The maximum number for
size is 65. The maximum number for d is
30. The default value for size is 10. The
default value for d is 0.

DEC(size, d) Equal to DECIMAL(size,d)

Data type Description

CHAR(size) A FIXED length string (can contain


letters, numbers, and special
characters). The size parameter
specifies the column length in
characters - can be from 0 to 255.
Default is 1

VARCHAR(size) A VARIABLE length string (can


contain letters, numbers, and special
characters). The size parameter
specifies the maximum string length
in characters - can be from 0 to
65535

BINARY(size) Equal to CHAR(), but stores binary


byte strings. The size parameter
specifies the column length in bytes.
Default is 1

VARBINARY(size) Equal to VARCHAR(), but stores


binary byte strings. The size
parameter specifies the maximum
column length in bytes.

TINYBLOB For BLOBs (Binary Large Objects).


Max length: 255 bytes

TINYTEXT Holds a string with a maximum


length of 255 characters

TEXT(size) Holds a string with a maximum


length of 65,535 bytes

BLOB(size) 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.

ENUM(val1, val2, val3, …) A string object that can have only


one value, chosen from 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. The
values are sorted in the order you
enter them

SET(val1, val2, val3, …) A string object that can have 0 or


more values, chosen from a list of
possible values. You can list up to 64
values in a SET list

You might also like