MySQL (AutoRecovered)
MySQL (AutoRecovered)
A data-type is a property that specifies the type of data you can put in your table.
Each field has a data-type and all values for a field have the same data-type.
Data-types:
Char - Any number of characters we give also the memory size is fixed i.e.,. 60 characters
Varchar -
Text - To store large amount of text
Constraints:
Constraint-types:
Not NULL - To make sure field gives some value in table for any row
Default - To populate default value when miss giving any value in column
Unique - Makes sure all values entered are different, prevents duplicate values and
can store NULL value
Primary key - A column with unique but not NULL value
Foreign key - Foreign keys link data in one table to the data in another table
Check constraint - Server to ensure that all values in a column or a group of columns satisfy a
specified condition. This condition can be any Boolean expression that
evaluates to true or false.
1. Creating database
CREATE DATABASE DBNAME;
2. Using database
USE DBNAME;
3. Drop database or delete created database
DROP DATABASE DBNAME;
4. Creating table - Name the table Define the columnsMention data-types of columns
USE HELL;
CREATE TABLE EMPLOYEES
(
ID INT PRIMARY KEY,
EMPNAME VARCHAR(30),
DOB DATETIME,
EMAIL VARCHAR(40),
SALARY INT
);
5. Describing table after adding
DESCRIBE TABLENAME;
6. Creating tables from other tables
USE HELL;
CREATE TABLE MALEEMPLOYEES
AS
SELECT ID, EMPNAME, DOB, EMAIL
FROM EMPLOYEES;
7. Delete or drop tables
DROP TABLE MALEEMPLOYEES;
8. Inserting data into tables
INSERT INTO EMPLOYEES
(ID, EMPNAME, DOB, EMAIL, SALARY)
VALUES( 0238, 'MOHAMMED', '1989-02-08', 'zak0238@gmail.com', 65000);
Operators:
Generally used with WHERE clause - Used to limit the no. of rows obtained in output
Addition - +
Subtraction - -
Multiplication - *
Division - /
Modular division - %
Comparison operators:
Types
Equal to - =
Not equal to - <>
Greater than - >
Less than - <
Greater than or equal to - >=
Less than or equal to - <=
SELECT *
FROM EMPLOYEES
WHERE SALARY < 45000;
SELECT EMPNAME
FROM EMPLOYEES
WHERE SALARY >= 25000;
Logical operators:
Types:
All
And
Between
In
Like
Or
Is NULL
SELECT *
FROM EMPLOYEES
WHERE SALARY IS NULL;
SELECT *
FROM EMPLOYEES
WHERE EMPNAME LIKE 'M%';
SELECT *
FROM EMPLOYEES
WHERE ID IN (238, 241, 248);
SELECT *
FROM EMPLOYEES
WHERE SALARY BETWEEN 50000 AND 100000;
SELECT *
FROM EMPLOYEES
WHERE ID > ALL (SELECT ID FROM EMPLOYEES WHERE SALARY > 50000);
14. WHERE
To include a condition while fetching data from tables.
Also used to join multiple tables.
Not only used with SELECT but also used with UPDATE and DELETE queries.
SELECT *
FROM EMPLOYEES
WHERE EMPNAME = 'RAZAK';
15. UPDATE
To update rows in a table.
Generally used with WHERE clause.
UPDATE EMPLOYEES
SET EMAIL = 'zak0238@yahoomail.com'
WHERE ID = 240;
16. DELETE
Used to delete records in a table.
Mostly used with WHERE clause.
DELETE
FROM EMPLOYEES
WHERE ID = 245;
17. LIKE
Used to compare a value to similar values in a table in db.
Used with Wildcard characters.
Wildcard characters:
SELECT *
FROM EMPLOYEES
WHERE EMAIL LIKE '%YAHOOMAIL%';
SELECT *
FROM EMPLOYEES
WHERE ID LIKE '23_';
SELECT *
FROM EMPLOYEES
WHERE EMPNAME LIKE 'M%';
SELECT *
FROM EMPLOYEES
WHERE EMPNAME LIKE '_A%';
18. TOP and LIMIT claused
Used to fetch top N number of records from a table.
Not all db’s support TOP.
ORACLE supports ROWNUM and MYSQL supports LIMIT.
SELECT *
FROM EMPLOYEES
LIMIT 6;
19. ORDER BY clause
To sort data fetched from a table
SELECT *
FROM EMPLOYEES
ORDER BY EMPNAME;
SELECT *
FROM EMPLOYEES
ORDER BY ID DESC;
Types:
Inner Join
Natural Join
Left (Outer) Join
Right (Outer) Join
(Full) Outer Join
Left (Outer) Join Excluding Inner Join
Right (Outer) Join Excluding Inner Join
(Full) Outer Join Excluding Inner Join
Cross Join
Equi-Join
INNER JOIN:
Inner join produces only the set of records that match in both Table A and Table B
SELECT *
FROM EMPLOYEES
INNER JOIN MALEMPLOYEES
ON EMPLOYEES.ID = MALEMPLOYEES.ID;