Manuel SQL
Manuel SQL
database is and why do we need SQL. If you want to learn this offline, you can
download the SQL basics cheat sheet at any time.
Data is a collection of facts related to any object. For example: - Your name,
number, birthday, phone number, email address, etc. is a collection of facts
about you.
Think of an RDBMS as a tool that allows you to play with your data and
generate insights or value from the database.
In this SQL Cheat Sheet, we would be looking at both SQL and MySQL, which
would help clarify the difference.
Using SQL, you create Databases, and inside a database, you create various
TABLES in which you can add all your data. Using SQL, you can:
and tons of other cool stuff. So, let's see how we can harness this power.
There are various RDBMS that you can use, and it doesn't matter (much)
what system you choose as long as it's working for you. Some of the most
common RDBMS are:
1. MySQL
2. Oracle
3. Microsoft SQL Server
4. PostgreSQL
5. Heidi SQL
Just install any of the RDBMS that you like from their official website and you
should be able to create a database server by simply following their
instructions. Once you have a database server ready, you can get access to
a Query Editor where you can type all your SQL queries.
Now, let's get started with our cheat sheet and learn some SQL basics and
SQL syntax to get the ball rolling.
1. Customers
2. Orders
3. Menu Items
4. Receipts
5. Combox
etc. Each table would contain a specific type of data and various tables could
have different types of relations. Using SQL relations, we can combine values
from different tables to fetch the data that we require. (More on relations in a
later section)
To create a table, we would require two things. Firstly, we would need all the
fields that we want to store in a table. Secondly, we want to define the type
of data that would enter into a table.
To create this table, we would use the CREATE SQL Command followed by
the fields as follows:
name varchar(50),
phone varchar(15),
postalCode INT
);
VARCHAR(size) Variable-length string similar to CHAR(), but with a length from 0 to 65535.
TINYBLOB Used for BLOBs (Binary Large Objects). Has a max length of 255 bytes.
A string object that can have only one value is chosen from a list of
ENUM(val1,val2,…) possible values of up to 65535 values in an ENUM list. If the value inserted
is not in the list, a blank value will be inserted.
A string object that can have 0 or more values, chosen from a list of
SET(val1,val2,…)
possible values. You can list up to 64 values in a SET list.
A bit-value type. The size parameter can hold a value from 1 to 64. The
BIT(size)
default value for size is 1.
A very small integer. The signed range is from -128 to 127. The unsigned
TINYINT(size)
range is from 0 to 255.
A small integer. The signed range is from -32768 to 32767. The unsigned
SMALLINT(size)
range is from 0 to 65535.
Zero values are considered as FALSE and non-zero values are considered
BOOL/BOOLEAN
as TRUE.
A floating-point number value where the total digits are set by the size
parameter,
DOUBLE(size,d) and the number of digits after the decimal point is set by the d
parameter.
DEC(size,d)/ An exact fixed-point number with the total number of digits set by the size
Note: All the numeric data types may have an extra option: UNSIGNED or
ZEROFILL. If you add the UNSIGNED/ZEROFILL option, MySQL disallows
negative values for the column.
1. INSERT (Create)
2. SELECT (Read)
3. UPDATE (Update)
4. DELETE (Delete)
INSERT
To insert data into any table, we use the INSERT INTO statement. The
general syntax for insert is:
VALUES(val1,val2,...);
To insert data into our customer's table, we would use the following
statement:
VALUES(1,'Alice','+123456789',123456);
If we wanted to select the name and phone number of a customer from our
table, we would use:
Also, to read all the columns from our table, we can replace the column
names with * as follows:
UPDATE
To update specific column(s) of specific row(s), we make use of the Update
statement. The general syntax for an update statement is:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE conditions...;
UPDATE customers
SET phone='+2223334445'
WHEREID=2;
We can update multiple columns by adding them to the SET statement and
we can target multiple rows by adding them to the WHERE statement. We
will look at WHERE in detail in later sections of this SQL commands cheat
sheet.
DELETE
If we wanted to remove some rows from a table, we would use the delete
statement. The general syntax is:
Let's say we want to remove all the customers who live in a particular area.
So, we simply delete those rows that have a specific area code:
WHERE postalCode=223344;
Keyword Description
Add a new column to an existing table. Eg: ALTER TABLE customers ADD
ADD
email_address VARCHAR(255);
AS Renames a table or column with an alias value that only exists for the duration of
Adds a constraint that limits the value which can be added to a column. Eg:
CHECK
CREATE TABLE Users(firstName varchar(255),age INT, CHECK(age>10));
CREATE
Creates a new database. Eg: CREATE DATABASE my website;
DATABASE
CREATE Creates a new table. Eg: CREATE TABLE users (id int,firsr_name varchar(255),
TABLE surname varchar(255), address varchar(255), contact_number int);
Set the default value for a column. Eg: CREATE TABLE products(ID int, name
DEFAULT
varchar(255) DEFAULT 'Username', from date DEFAULT GETDATE());
DELETE Delete values from a table. DELETE FROM users WHERE user_id= 674;
DROP Deletes a column from a table. ALTER TABLE users DROP COLUMN
COLUMN first_name;
DROP Deletes a complete database along with all the tables and data inside. Eg: DROP
DATABASE DATABASE my website;
DROP Removes a default value for a column. Eg: ALTER TABLE products ALTER
DEFAULT COLUMN name DROP DEFAULT;
DROP
Delete a table from a database. Eg: DROP TABLE customers;
TABLE
Specifies which table to select or delete data from. Eg: SELECT * FROM
FROM
customers;
Tests for empty (NULL) values. Eg: SELECT * FROM users WHERE phone IS
IS NULL
NULL;
IS NOT
Opposite of IS NULL. Tests for values that are not null.
NULL
Returns true if the operand value matches a pattern. SELECT * FROM users
LIKE
WHERE first_name LIKE '%son';
ORDER BY Used to sort the resultant data in ascending (default) or descending order.
Used alongside SELECT to return a set number of records from a table. Eg:
TOP
SELECT TOP 5 * FROM customers;
Used alongside the INSERT INTO keyword to add new values to a table. Eg:
VALUES
INSERT INTO cars (name, model, year) VALUES ('Ford', 'Fiesta', 2010);
Filters result only includes data that meets the given condition. SELECT * FROM
WHERE
orders WHERE quantity > 1;
Operators in SQL
SQL has various operators that allow you to manipulate and compare
multiple values. These are very useful and handy while creating queries.
Operator Description
+ Addition
- Subtraction
* Multiply
/ Divide
| Bitwise OR
^ Bitwise Exclusive OR
Operator Description
= Equal to
Operator Description
+= Add Equals
-= Subtract Equals
*= Multiply Equals
/= Divide Equals
%= Modulo Equals
Operator Description
SQL Keys
In a database, different tables store different values and these values are
related to each other. To identify each row uniquely, we make use of SQL
keys. An SQL key is either a single column (or attribute) or a group of
columns that can uniquely identify rows in a table. SQL Keys ensures that
there aren't any rows with duplicate values.
Primary Key
It is a key that uniquely identifies a single row in a table. For example, in a
customer's table, the ID key can be used as a primary key to uniquely
identify a single customer. This key can then be used to fetch data from
multiple tables that have data related to the customer.
Key Points:
Typically, the primary key in a table is the ID column and is usually paired
with the AUTO_INCREMENT keyword to uniquely identify the row. To mark a
column as the primary key, we use the PRIMARY KEY keyword followed by
the column/columns that consist of the primary key. For Example: -
The table that contains the foreign key is known as the child table, while the
table containing the primary key for the foreign key is known as the parent
table.
SQL Joins
Once you understand Primary Key and Foreign Key, you can use joins to
fetch data by combining multiple tables. Let's take the orders, customers,
and products table as an example.
1 Burger 10
2 Sandwich 15
Customers:
1 Alice alice@alice.com
2 Bob bob@bob.com
Orders:
1 1 1
2 1 2
3 2 1
We can join the orders table with customers and products table to get only
the information that we require. Let's say we want to see all the orders with
the customer's name, product name, and product price as follows:
2 Sandwich Alice 15
3 Burger Bob 10
1. INNER JOIN - Returns any records which have matching values in both tables.
2. LEFT JOIN - Returns all of the records from the first table, along with any
matching records from the second table
3. RIGHT JOIN - Returns all of the records from the second table, along with any
matching records from the first
4. FULL JOIN - Returns all records from both tables when there is a match
Note: You can use ASC for Ascending Order or DESC for descending order. If
nothing is specified, sorting is done in Ascending order(ASC).
Note:- We can use any type of join that we want. The condition via which we
want to join the tables needs to be specified after the ON keyword.
6. Rename a Table
ALTER TABLE Users RENAME TO Customers;
7. Rename a Column
ALTER TABLE Users RENAME userName to name;