SQL - Structured Query Language¶
DQL - Data Query Language¶
SELECT¶
Create alias to table name and fields¶
SELECT p.code AS cod, p.description AS desc, p.price AS pri, p.product_code AS pd FROM products AS p;
Select in multiple tables¶
SELECT p.code AS Cod, p.description AS Desc, p.price AS Pri, pt.description AS Type
FROM products AS p, product_type AS pt
WHERE p.product_type_code = pt.code;
* Always use WHERE in a multiple table query.
DML - Data Manipulation Language¶
INSERT¶
- To insert a registry in an existing table
- Insert multiple values at time
UPDATE¶
To change the data values in one or more registries of a table
-
One value
example: UPDATE product_type set description = 'desktops' WHERE code = 5; -
Several values
example: UPDATE product set description = 'Notebook', price = 2.800 WHERE code = 20;
Warning
Always use WHERE when doing a UPDATE, if not ALL values will be updated
DELETE¶
- To remove the registries of a table
Warning
Always use WHERE when doing a DELETE, if not ALL values will be erased
DDL - Data Definition Language¶
CREATE¶
To create a database, table or other objects
Database¶
Table¶
ALTER¶
To alter a the table structure or other object inside a database
DROP¶
Used to erase a database, table, or other objects
Database¶
Table¶
DCL - Data Control Language¶
Aspects of data and users permission to control who has access to manipulated data within the database.
GRANT¶
Used to authorize an user to execute or set operations in the database
Select¶
REVOKE¶
Used to remove or restrain the capacity of a user execute operations
DTL - Data Transaction Language¶
BEGIN¶
Set the beginning of an transaction, this transaction can be completed or not.
CREATE TABLE 'product_type' (codigo INT PRIMARY KEY, description VARCHAR(50));
BEGIN TRANSACTION; -- start the transaction
INSERT INTO product_type VALUES ('Notebook');
INSERT INTO product_type VALUES ('Nobreak');
COMMIT; -- finish and record the transaction
COMMIT¶
Finishes a transaction.
CREATE TABLE 'product_type' (codigo INT PRIMARY KEY, description VARCHAR(50));
BEGIN TRANSACTION; -- start the transaction
INSERT INTO product_type VALUES ('Notebook');
INSERT INTO product_type VALUES ('Nobreak');
COMMIT; -- finish and record the transaction
ROLLBACK¶
Make all the changes since the last COMMIT be discarded
CREATE TABLE 'product_type' (codigo INT PRIMARY KEY, description VARCHAR(50));
BEGIN TRANSACTION; -- start the transaction
INSERT INTO product_type VALUES ('Notebook');
INSERT INTO product_type VALUES ('Nobreak');
ROLLBACK; -- all changes since the last BEGIN are undone