0% found this document useful (0 votes)
10 views

MySQL (AutoRecovered)

Uploaded by

zak_0238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

MySQL (AutoRecovered)

Uploaded by

zak_0238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Data-type:

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:

For numeric fields:

 Bigint – Really big values


 Int - Big values
 Smallint - -32,768 to 32,767
 Tinyint - 0 to 255
For Fractions or decimals
 Decimal - m,d
 Numeric - same as decimal
 Float - m,d

For Date and Time data-types:

 Datetime - YYYY-MM-DD HH:MM:SS


 Date - YYYY-MM-DD
 Time - HH:MM:SS
 Year - YYYY

For character 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

For storing images or video files:

 BLOB - Binary Large Object

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 columnsMention 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', '[email protected]', 65000);

INSERT INTO EMPLOYEES


VALUES(241, 'NISHATH', '1988-11-06', '[email protected]', 12500);
9. To view the table
SELECT * FROM EMPLOYEES;
10. Populating one table using data in another table
INSERT INTO MALEMPLOYEES
(ID, EMPNAME, DOB, EMAIL, SALARY)
SELECT ID, EMPNAME, DOB, EMAIL, SALARY
FROM EMPLOYEES;
11. SELECT query - Used to get data from tables
SELECT EMPNAME, DOB FROM EMPLOYEES;

Operators:

Symbols (+ - * / < > =)

Generally used with WHERE clause - Used to limit the no. of rows obtained in output

 Addition - +
 Subtraction - -
 Multiplication - *
 Division - /
 Modular division - %

Comparison operators:

To compare data present in columns to data specified in conditions.

Types

 Equal to - =
 Not equal to - <>
 Greater than - >
 Less than - <
 Greater than or equal to - >=
 Less than or equal to - <=

12. Comparison operators

SELECT *
FROM EMPLOYEES
WHERE SALARY < 45000;

SELECT EMPNAME
FROM EMPLOYEES
WHERE SALARY >= 25000;

Logical operators:

To create complex query conditions.

Types:

 All
 And
 Between
 In
 Like
 Or
 Is NULL

13. Logical operators


SELECT EMPNAME
FROM EMPLOYEES
WHERE SALARY >= 25000 AND ID >= 240;

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 = '[email protected]'
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:

 % - Substitution for 0, 1 or many characters.


 _ - Stands for one character or number.

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;

20. The GROUP BY clause


 To arrange identical data in groups.
 Used with SELECT query.
 Must follow WHERE clause and precede ORDER BY clause.
21. SQL JOINS
 Combines data or rows from two or more tables based on a common field between them.

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;

You might also like