SQL Reference Guide
Part 1: General Definitions
²² :Replace it and what inside it with the explanation.
‘ ’ :We use it when inserting a text value (we right inside it).
²AUTO_INCREMENT²: Automatically fills the attribute.
²NOT NULL²: Ensures the attribute cannot be left empty.
Conditions: Logical operators: `²AND²` / `²&&²`, `²OR²` / `²||²`, `²AND NOT²` / `²AND !²` ,
`²XOR²`.
Comparison Operators: `²=²`, `²<²`, `²>²`, `²<>²` (everything except the value before it).
²UNSIGNED²: Refers to uppercase letters only.
Order Types: `²DESC²` ("from Z to A"), `²ASC²` ("from A to Z").
Key Types: `²PRIMARY KEY(attribute)²`, `²FOREIGN KEY(attribute)²`.
Attribute Types: `²SMALLINT(number of characters)²`, `²INT(number of characters)²`,
`²VARCHAR(number of characters)²`, `²CHAR(number of characters)²`, `²TEXT(number of
characters)²`, `²DATE²`.
Join Types:
- ²INNER JOIN²: Returns only matching rows.
- ²LEFT JOIN²: Includes all rows from the left table, filling unmatched rows with `²NULL²` on
the right.
- ²RIGHT JOIN²: Includes all rows from the right table, filling unmatched rows with `²NULL²`
on the left.
- ²FULL JOIN²: Combines both left and right joins.
- ²NATURAL JOIN²: Automatically merges tables based on attributes with the same name.
Part 2: Database Creation and Management
Create a database: ____________________________________________________________________________________
CREATE DATABASE ²database name² CHARACTER SET 'UTF8';
Drop a database:
______________________________________________________________________________________
DROP DATABASE ²database name²;
Drop a database if it exists: __________________________________________________________________________
DROP DATABASE IF EXISTS ²database name²;
Use a specific database: ______________________________________________________________________________
USE ²database name²;
Show all warnings: ___________________________________________________________________________________
SHOW WARNINGS;
Part 3: Table Creation and Management
Create a table: ________________________________________________________________________________________
CREATE TABLE ²table name² (
²attribute name² ²attribute type² ²if it's not null² ²if it's auto_increment²,
²another attribute²,
²primary or foreign key²(²attribute name²)
) ENGINE=INNODB;
Show all tables: _______________________________________________________________________________________
SHOW TABLES;
Describe table structure: ____________________________________________________________________________
DESCRIBE ²table name²;
Drop a table: __________________________________________________________________________________________
DROP TABLE ²table name²;
Add a column: ________________________________________________________________________________________
ALTER TABLE ²table name² ADD COLUMN ²column name² ²attribute type² ²null or not²;
Drop a column: _______________________________________________________________________________________
ALTER TABLE ²table name² DROP COLUMN ²column name²;
Modify a column's properties: ______________________________________________________________________
ALTER TABLE ²table name² MODIFY ²attribute name² ²attribute type² ²null or not²;
Change a column's name and properties: __________________________________________________________
ALTER TABLE ²table name²
CHANGE ²old attribute name² ²new attribute name² ²attribute type² ²null or not²;
Part 4: Indexes and Keys
Create a primary key after table creation:
__________________________________________________________
ALTER TABLE ²table name² ADD PRIMARY KEY(²attribute name²);
Drop a primary key: __________________________________________________________________________________
ALTER TABLE ²table name² DROP PRIMARY KEY;
Create a foreign key: _________________________________________________________________________________
ALTER TABLE ²table name²
ADD CONSTRAINT ²foreign key name²
FOREIGN KEY (²attribute name²) “where to put the key (it should be filled)”
REFERENCES ²referenced table²(²referenced attribute²);
Drop a foreign key: ___________________________________________________________________________________
ALTER TABLE ²table name² DROP FOREIGN KEY ²foreign key name²;
Create an index: ______________________________________________________________________________________
CREATE INDEX ²index name² ON ²table name² (²attribute name²);
Create a unique index: _______________________________________________________________________________
CREATE UNIQUE INDEX ²index name² ON ²table name² (²attribute name²);
Part 5: Data Insertion and Manipulation
Insert data into a table: ______________________________________________________________________________
INSERT INTO ²table name² VALUES (²value1², ²value2², ...);
Insert specific attributes: ____________________________________________________________________________
INSERT INTO ²table name² (²attribute1², ²attribute2²)
VALUES (²value1², ²value2²);
Update data: __________________________________________________________________________________________
UPDATE ²table name²
SET ²attribute1² = ²value1²
WHERE ²condition²;
Delete data: ___________________________________________________________________________________________
DELETE FROM ²table name²
WHERE ²condition²;
Part 6: Querying Data
Select all data: ________________________________________________________________________________________
SELECT * FROM ²table name²;
Select specific attributes: ____________________________________________________________________________
SELECT ²attribute1², ²attribute2²
FROM ²table name²;
Apply conditions: _____________________________________________________________________________________
SELECT * FROM ²table name²
WHERE ²attribute name² ²sign² ²value²;
Order results: _________________________________________________________________________________________
SELECT * FROM ²table name²
ORDER BY ²attribute name² ASC|DESC;
Limit results:
__________________________________________________________________________________________
SELECT * FROM ²table name²
LIMIT ²number²
OFFSET ²skip²;
Part 7: Joins
Basic join: _____________________________________________________________________________________________
SELECT * FROM ²table1 name²
²join type² JOIN ²table2 name²
ON ²table1 name².²attribute² = ²table2 name².²attribute²;
Join with condition: __________________________________________________________________________________
SELECT * FROM ²table1 name²
²join type² JOIN ²table2 name²
ON ²table1 name².²attribute² = ²table2 name².²attribute²
WHERE ²condition²;