MYSQL
MYSQL
Database:
A database is a structured collection of data, commonly stored and accessed
electronically from a computer system. In other words, databases support storage
and manipulation of data.
1. Types of Data
a. Quantitative
i. Numerical form
b. Qualitative
i. Descriptive, but not numerical.
DATATYPE Description
DATATYPE Description
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS
TIMESTAMP YYYYMMDDHHMMSS
TIME HH:MM:SS
BOOLEAN 0/1
1. Types of SQL commands:
1. DDL (data definition language): defining relation schema.
1. SELECT
3. DML (data modification language): use to perform modifications in the DB
SHOW DATABASES;
USE <Database Name>;
Use databasename;
MODULE 2
AND
AND Operator is used to show the records if the conditions which are separated
by AND are true.
SELECT <COLUMN NAMES> FROM <TABLE NAME> WHERE <CONDITION 1> AND
<CONDITION 2>...;
OR Operator is used to show the records if any of the conditions which are separated
by OR are true.
SELECT <COLUMN NAMES> FROM <TABLE NAME> WHERE <CONDITION 1> OR
<CONDITION 2>...;
Ex: SELECT FIRSTNAME FROM EMPLOYEE WHERE EMPLOYEEID=110 OR
LASTNAME='Kumar';
NOT Operator is used to show the records if the conditions are not true.
SELECT <COLUMN NAMES> FROM <TABLE NAME> WHERE NOT<CONDITION>;
Ex: SELECT FIRSTNAME FROM EMPLOYEE WHERE NOT LASTNAME='Kumar';
MODULE 3
ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending
order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
Arrangement of records while displaying the result- ORDER BY SYNTAX
SELECT * FROM <TABLE NAME> ORDER BY <COLUMN NAME> ASC|DESC;
EX: SELECT * FROM EMPLOYEE ORDER BY FIRSTNAME;
IS NULL Syntax
SELECT * FROM <Table Name> WHERE <Column Name> IS NULL;
IS NOT NULL Syntax is used to show the records which are not null values.
SELECT * FROM <Table Name> WHERE <Column Name> IS NOT NULL;
SELECT FROM EMPLOYEE WHERE EMPLOYEEID IS NOT NULL;
MIN Syntax
SELECT MIN(COLUMN NAME) FROM <TABLE NAME> WHERE <CONDITION>;
MAX()
To return the largest record in the column - MAX() Syntax
SELECT MAX(<COLUMN NAME>) FROM <TABLE NAME> WHERE <CONDITION>;
COUNT()
To check the number of records/rows which satisfies a specific criterion
COUNT() Syntax--
SELECT COUNT<Column Name> FROM<Table Name> WHERE <Condition>;
AVG()
To check the average value of the numeric column
AVG() Syntax--
SELECT AVG<Column Name> FROM<Table Name>;
SUM()
To get the sum of the numeric column
SUM() Syntax--
SELECT SUM<Column Name> FROM <Table Name>;
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 sign (_) represents one, single character
The percent sign and the underscore can also be used in combinations!
To fetch names which starts with ‘D' and ends with 'I' - 'A%N' Syntax
SELECT * FROM <Table Name> WHERE <Column Name> LIKE ‘D%N';
ALIAS
Aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of that query.
An alias is created with the AS keyword.
ALIAS is of two types:
• Alias Column
• Alias Table
3. UNIQUE
1. Unique, can be null, table can have multiple unique attributes.
2. CREATE TABLE customer (
…
email VARCHAR(1024) UNIQUE,
…
);
4. CHECK
1. CREATE TABLE customer (
…
CONSTRAINT age_check CHECK (age > 12),
…
);
2. “age_check”, can also avoid this, MySQL generates name of constraint automatically.
5. DEFAULT
1. Set default value of the column.
CREATE TABLE account (
…
saving-rate DOUBLE NOT NULL DEFAULT 4.25,
…
);
6. ALTER OPERATIONS
1. Changes schema
2. ADD
1. Add new column.
2. ALTER TABLE table_name ADD new_col_name datatype ADD new_col_name_2 datatype;
3. e.g., ALTER TABLE customer ADD age INT NOT NULL;
3. MODIFY
1. Change datatype of an attribute.
2. ALTER TABLE table-name MODIFY col-name col-datatype;
3. E.g., VARCHAR TO CHAR
ALTER TABLE customer MODIFY name CHAR(1024);
4. CHANGE COLUMN
1. Rename column name.
2. ALTER TABLE table-name CHANGE COLUMN old-col-name new-col-name new-col-
datatype;
3. e.g., ALTER TABLE customer CHANGE COLUMN name customer-name
VARCHAR(1024);
5. DROP COLUMN
1. Drop a column completely.
2. ALTER TABLE table-name DROP COLUMN col-name;
3. e.g., ALTER TABLE customer DROP COLUMN middle-name;
6. RENAME
1. Rename table name itself.
2. ALTER TABLE table-name RENAME TO new-table-name;
e.g., ALTER TABLE customer RENAME TO customer-details
Having clause
Syntax : -
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column
HAVING condition;
MODULE 7
JOIN
To combine rows from two or more tables - JOIN Syntax.
There are six types of joins:
Inner Join - to fetch matching records of both the tables.
Left Join - to fetch all the records from left table and matching records from
the right table.
Right Join to fetch all the records from right table - and matching records from
the left table.
Full Join - to fetch all the records when there is a match in records.
Self-Join – A join where a table is joined with itself to compare rows within a
same table.
Cross Join – combining every row from first table with every row from the
second.
Inner Join:
Retrieves records from both tables where there is a match between the
department_id column in the Departments table and the department_id column in
the Employees table.
Syntax
SELECT <COLUMN NAME> FROM TABLE1 INNER JOIN TABLE2 ON <TABLE1.COLUMN
NAME>= <TABLE2.COLUMN NAME>
LEFT JOIN:
Retrieves all records from the Employees table and the matching records from the
Departments table based on the department_id column. If there is no match, the
result will contain NULL values for the columns from the Departments table.
Left Join Syntax
SELECT <COLUMN NAME> FROM TABLE1 LEFT JOIN
TABLE2 ON <TABLE1.COLUMN NAME>=
<TABLE2.COLUMN NAME>
RIGHT JOIN:
Retrieves all records from the Departments table and the matching records from the
Employees table based on the department_id column. If there is no match, the result
will contain NULL values for the columns from the Employees table.
Right Join Syntax
SELECT <COLUMN NAME> FROM TABLE1 RIGHT JOIN TABLE2 ON <TABLE1.COLUMN
NAME>= <TABLE2.COLUMN NAME>
FULL OUTER JOIN:
Retrieves all records from both the Employees and Departments tables. If there is a
match, it includes the matched records. If there is no match, it includes NULL values
for the columns from the other table.
Full Outer Join Syntax
SELECT <COLUMN NAME> FROM TABLE1 FULL OUTER JOIN TABLE2 ON
<TABLE1.COLUMN NAME>=<TABLE2.COLUMN NAME>
Self-Join –
Table data ---
CREATE TABLE employees (
employee_name VARCHAR(50),
manager_id INT
);
Cross join –
Product and Colors.
Table Data –
product_name VARCHAR(50)
);
color_name VARCHAR(50)
);
INSERT INTO products (product_id, product_name) VALUES
(1, 'T-shirt'),
(2, 'Jeans');
(1, 'Red'),
(2, 'Blue');