Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
65 views

MySQL Notes

This document provides an overview of MySQL commands and functions for creating databases and tables, inserting and querying data, modifying data through updates and deletes, and setting constraints. It covers topics like SELECT statements, JOINs, aggregate functions, and creating indexes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
65 views

MySQL Notes

This document provides an overview of MySQL commands and functions for creating databases and tables, inserting and querying data, modifying data through updates and deletes, and setting constraints. It covers topics like SELECT statements, JOINs, aggregate functions, and creating indexes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

MySQL Notes

Firstly we have to make/create a DATABASE by using following


command:
CREATE DATABASE database_name;
After created database use following command to use database:
use database_name;

To create a TABLE we use following command:


CREATE TABLE table_name;

To add columns in this table:


CREATE TABLE table_name (
Column1 datatype,
Column2 datatype,
Column3 datatype,
);

To INSERT data in table:

INSERT INTO table_name(column1,column2,column3,…)


VALUES (value1, value2, value3…);
For multiple data we use:
VALUES
(value1, value2, value3…),
(value1, value2, value3…),
(value1, value2, value3…);
Constraints: Meaning of constraints is restriction.

Types of constraints:
 Not Null
 UNIQUE
 DEFAULT
 CHECK
 FOREIGN KEY
 PRIMARY KEY

 Not Null constraints is used when user do not wanted to be


column null.
 UNIQUE constraints is used when user wanted some unique
value from the user. For ex. The phone numbers of each
customer is unique.
 DEFAULT constraints is used when user has to use same value
multiple times to avoid wastage of time.for ex. Many
employees has been form same city.
 CHECK constraints is used when user has to put restrictions on
some special columns. For ex. Age restrictions.

Use Of Constraints:

CREATE TABLE table_name (


Id INT NOT NULL UNIQUE,
Name VARCHAR (50) NOT NULL,
Age INT NOT NULL CHECK (age>=18),
Gender VARCHAR (10) NOT NULL,
Phone VARCHAR (10) NOT NULL UNIQUE,
City VARCHAR (10) NOT NULL DEFAULT ‘Agra’,
);

SELECT Command: to show the data from the table.

 To see particular column data then syntax is:


SELECT column1, column2, column3,…
FROM table_name;

 To see all the data from all the columns then the syntax is:
SELECT*
FROM table_name;
 To Rename the column name by using AS keyword:
SELECT column1 AS new_name, column2 AS new_name;

 SELECT data with where: To see condition based data.


SELECT column1, column2, column3,…
FROM table_name
WHERE condition;

All the conditional operators are as follows:


 To see all the data :
SELECT *FROM table_name
WHERE CONDITION ;(ex: gender=”f”;)

SELECT data with AND &OR Operators:


To check two condition like Boolen conditions.
SELECT *FROM table_name
WHERE condition1 AND condition2 AND condition3 ;
OR with AND:
SELECT *FROM table_name
WHERE (condition1 OR condition2 )AND condition3 ;
IN and NOT IN operator:
SELECT *FROM table_name
WHERE column_name IN (value1,value2,…);
Ex: SELECT *FROM table_name
WHERE age IN (18,21);
SELECT *FROM table_name
WHERE column_name NOT IN (value1,value2,…);

SELECT data with BETWEEN And NOT BETWEEN operators:


SELECT *FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Ex: SELECT *FROM table_name


WHERE DOB BETWEEN 1990 AND 2000;

SELECT data with LIKE and NOT LIKE operators:


SELECT *FROM table_name
WHERE column_name LIKE “%a”;

SELECT data with REGULAR EXPRESSION (REGEXP):


SELECT *FROM table_name
WHERE column_name REGEXP pattern;

SELECT *FROM table_name


WHERE name REGEXP ‘man’;
It will print those data name contain man word.

SELECT data with ORDER BY:


This will arrange all the names according to alphabates.
For Ascending Order: ASC
For Descending Order: DESC
SELECT column1,column2,…../to see table then use *
FROM table_name
ORDER BY column1,column2,…ASC/DESC;
SELECT data with DISTINCT: Syntax is different.
Do not repeat same data. ex. The students
from same city/student of same name ,etc.This will use for to see the
what age group students are learning in particular college.
SELECT DISTINCT column1,column2,….
FROM table_name;

SELECT data with IS NULL and IS NOT NULL:


NULL: will show the data which contains null columns.
SELECT *FROM table_name
WHERE column1 IS NULL;

SELECT *FROM table_name


WHERE column1 IS NOT NULL;

SELECT data with LIMIT and OFFSET:


To see the limited data ,like first three students
data, or anything .
SELECT *FROM table_name
LIMIT number;

SELECT *FROM table_name


WHERE condition
LIMIT number;
SELECT data with OFFSET:
It’s like range function, ex.3,5 i.e.(3 to 5)

SELECT *FROM table_name


WHERE condition
LIMIT offset, number;

SELECT data with Aggregate Functions:


There are many Aggregate Functions which are as follows:
Count(column_name): To calculate total no. of records.
MAX(column_name)
MIN(column_name)
SUM(column_name)
AVG(column_name): To calculate avg. of column.

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

UPDATE COMMAND:
How to update data in Tables with SQL? To update phone no./
name/ or anything.
UPDATE syntax:
UPDATE table_name
SET column1_name=value1,column2_name=value2,…
WHERE condition;
Ex.:
UPDATE personal
SET phone=”New_number”
WHERE id =1;
For multiple changes,
UPDATE personal
SET phone=”New_number”, age=21
WHERE id =1;

UPDATE personal
SET age=21
WHERE id IN (2,3);

COMMIT AND ROLLBACK COMMAND:


ROLLBACK: is nothing but undo.To revert the action. This will
do rollback of only three commands:
INSERT ,UPDATE ,DELETE COMMANDS
COMMIT: Hum jaha bhi commit use karenge rollback command
wahi tak chalega. Like maine bahut se updates kiye hai table
mai and I wanted to rollback my last update only then hum last
update se pehle commit command use karenge due to which
remaining updates remain same except last one.
To COMMIT the data:
To ROLLBACK the data:

DELETE COMMAND:
How to delete data from Tables with SQL?

DELETE syntax:
DELETE FROM table_name
WHERE condition;
Ex:
DELETE FROM personal
WHERE id=2;

PRIMARY KEY and FOREIGN KEY:


PRIMARY KEY:
 Primary key always has unique data.
 A primary key cannot have null value.
 A table can contain only one primary key constraint.

Syntax:
CREATE TABLE table_name (
Id INT NOT NULL AUTO_INCREMENT,
Name VARCHAR (50) NOT NULL,
Age INT NOT NULL,
City VARCHAR (10) NOT NULL,
PRIMARY KEY (Id)
);

If you have an table then to use primary key in that table :


ALTER TABLE table_name
ADD PRIMARY KEY(id);

FOREIGN KEY:
 A FOREIGN KEY is a key used to link two tables together.
 A FOREIGN key in one table used to point PRIMARY key in another
table.

Syntax:
CREATE TABLE table_name (
Id INT NOT NULL AUTO_INCREMENT,
Name VARCHAR (50) NOT NULL,
Age INT NOT NULL,
City VARCHAR (10) NOT NULL,
PRIMARY KEY (Id)
FOREIGN KEY(city) REFERENCES City(cid)
);

If you have an table then to use primary key in that table :


ALTER TABLE table_name
ADD FOREIGN KEY(city) REFERENCES City(cid);
INNER JOIN:
LEFT JOIN and RIGHT JOIN:
CROSS JOIN:
JOIN Multiple Tables:
GROUP BY and HAVING:
UNION and UNION ALL:
IF and CASE Statements:
Arithmetic Functions:
String Functions:
Data Functions:
Time Functions:
ALTER:
DROP and TRUNCATE:
VIEW:
INDEXES:

You might also like