Show databases; = to check default DB
Create database DB_name; = new database
Use DB_name; = select database
Create table TABLE_NAME (Col 1_name datatype constraint, col 2_name datatype constraint, col
3_name datatype constraint); = create new table
NOTE- For example= create table info(id int primary key, city varchar(50), age int);
Constraint for restriction: like Not null , unique and primary key (optional)
The basic difference between Char and Varchar is that: char stores only fixed-length character string
data types whereas varchar stores variable-length string where an upper limit of length is specified.
Desc TABLE_NAME; = description of table
Insert into TABLE_NAME ( col1, col2, col3) VALUES (data of col1, data of col2, data of col);
NOTE- For example= insert into (id, city, age) values (01, ‘delhi’, 20), (02, ‘mumbai’, 18);
Select * From TABLE_NAME; = to check details of table (to retrieve the details from table)
Select COL1_NAME from TABLE_NAME; = to retrieve the data from a particular column
WHERE CLAUSE – used for apply condition
Select * from TABLE_NAME WHERE id = 02
DDL works on structure
DQL works on value
DCL works on permission
Drop database DB_NAME; = delete database
IF NOT EXIST = displays warning instead of errors
Create database IF NOT EXIST DB_NAME;
LIKE CLAUSE = match the pattern we can use “ % and _ “ if didn’t know about exact data
Select COL1, COL2 from TABLE_NAME where COL3 LIKE “mum%”;
NOTE- for example: select name, id from cust where pay_mode like “net%”;
ADD NEW COLUMN (to change table structure)
Alter table TABLE_NAME ADD new_col_name datatype constraint;
DELETE EXISTING COLUMN
Alter table TABLE_NAME drop column COL_NAME;
TO MODIFY COL NAME
Alter table TABLE_NAME CHANGE COLUMN old_col_name new_col_name datatype constraint;
TO MODIFY DATATYPE
Alter table TABLE_NAME MODIFY COL_NAME DATATYPE;
We can use rename to modify table_name and modify to change datatype
TO CHANGE TABLE NAME
Alter table OLD_TABLE_NAME RENAME to NEW_TABLE_NAME;
TO ADD ON DATA INTO TABLE
Update TABLE_NAME SET COL_NAME = “DATA” where COL_NAME{to be identifty} = “DATA ALREADY
EXIST”;
TO DELETE ROW
Delete from TABLE_NAME where COL_NAME = “DATA”
ORDER BY ASC= Ascending DESC = descending
Select * from TABLE_NAME ORDER BY COL_NAME DESC;
TO DISPLAY LIMITED DATA
Select * from TABLE_NAME LIMIT “range = 2”;
Select * from TABLE _NAME order by COL_NAME desc LIMIT 2;
Function used to perform a particular task
Aggregate function: Count, average, sum, max, min, {first and last (use limit clause)}
FUNCTIONS
Select COUNT(COL1_NAME) from TABLE_NAME;
Select MAX(COL2_NAME) from TABLE_NAME;
Select MIN(COL1_NAME) from TABLE_NAME;
COMMENTS: A part that is not executes
For single line comments – use #
For multi-line comment – use /* */
GROUP BY CLAUSE
Group by clause apply on that column which have similar values
FOR GROUPING
Select COL1_NAME from TABLE_NAME GROUP BY COL1_NAME;
Select COL1_NAME, COUNT(COL2_NAME) from TABLE_NAME GROUP BY COL1_NAME;
Use having clause with group by instead of where
Select COL1_NAME, COUNT(COL2_NAME) from TABLE_NAME GROUP BY COL1_NAME HAVING
MIN(COL3_NAME) > “any condition”;
FOR CREATING NEW USER
Select Current_user; = to get update about the current localhost
TO CREATE NEW USER
Create user ‘NAME’@’localhost’ identified by ‘password’;
TO GET INFORMATION ABOUT ALL USER
select user from [Link];
TO DELETE ANY USER
Drop user ‘NAME’@’localhost’;
TO CHANGE PASSWORD
alter user ‘NAME’@’localhost’ identified by ‘password’;
TO PROVIDE ANY ACCESS/ REMOVE ANY ACCESS
Grant all on *.* to NAME@localhost;
Revoke all on *.* to NAME@localhost;
1. *is used for Data base you can use particular name of database
2. * is used for table you can add any particular table
3. * represents all database and table
Instead of all we can use any command like create, select, insert and other
COPY TABLES
Create table NEW_TABLE_NAME as Select * from
create table emp as select * from employee;
JOINS
1. CROSS JOIN
Select * from TABLE_NAME1 , TABLE_NAME2;
Select * form TABLE_NAME1 CROSS JOIN TABLE_NAME2;
(It will multiply/ combine the data of two different table)
It will join the data with another table or we can say it will do cross product
Select * from TABLE_NAME1 , TABLE_NAME2 where TABLE_NAME1.COLUMN_NAME =
TABLE_NAME2.COLUMN_NAME;
2. NATURAL JOIN
Select * from TABLE_NAME1 NATURAL JOIN TABLE_NAME2;
Select * from TABLE_NAME1 NATURAL JOIN TABLE_NAME2 where ANY CONDITION;
3. INNER JOIN
Select * from TABLE_NAME1 INNER JOIN TABLE_NAME2 ON TABLE_NAME1.COLUMN_NAME =
TABLE_NAME2.COLUMN_NAME;
4. LEFT OUTER JOIN
Select * from TABLE_NAME1 LEFT OUTER JOIN TABLE_NAME2 ON TABLE_NAME1.COLUMN_NAME =
TABLE_NAME2.COLUMN_NAME;
5. RIGHT OUTER JOIN
Select * from TABLE_NAME1 RIGHT OUTER JOIN TABLE_NAME2 ON TABLE_NAME1.COLUMN_NAME
= TABLE_NAME2.COLUMN_NAME;
SEQUENCE OF COMMAND
1. Select ---From ----where -----Group by ------ having -----order by ------ limit
SUB QUERY
Select COL_NAME2 from TABLE_NAME where COL_NAME2 = (Select MAX(COL_NAME2) from
TABLE_NAME);
For Example:
select name from emp where salary = (select max(salary) from emp);
select max(salary) from emp where salary <> (select max(salary) from emp); {<>means not equal to}
select emp_name from emp where salary = (select max(salary) from emp where salary<> (select
max(salary) from emp));
SUB QUERY WITH EXIST
Select COL_NAME1 from TABLE_NAME1 where EXIST (select COL_NAME1 from TABLE_NAME2
where TABLE_NAME1.COL_NAME = TABLE_NAME2.COL_NAME);
select emp_name from emp where exists (select emp_dep from test where
emp.emp_id=test.emp_id);
SUB QUERY WITH ANY
select salary from emp where salary= any (select salary from test);
select emp_dep, group_concat(emp_name) from test group by emp_dep;
INDEX (for the reference of column name to move easily)
Create INDEX NAME_INDEX on TABLE_NAME (COL_NAME1, COL_NAME2);
Example: create index emp_index on emp (emp_name, Salary);
Show INDEX from TABLE_NAME;
Example: show index from student;
Drop INDEX INDEX_NAME on TABLE_NAME;
drop index stu_ind on student;
foreign key
1st table is known as reference table 2nd is known as referencing table
Create table TABLE_NAME (COL1 Datatype, COL2 Datatype, FOREIGN KEY(COL_NAME) references
TABLE_NAME2(COL_NAME) delete on cascade update on cascade);
create table course1 (rollno int, name varchar(20), foreign key(rollno) references student(roll_no));
*NOTE – we can delete the data from referencing table (that have foreign key placed) but we cannot
insert and update new data
We can use insert, delete and update commands on reference table
Delete and update on cascade can be used for auto update and delete for maintain integrity
IF condition
select if(78>89,"yes","no");
select if(78<89,"yes","no");
select name,emp_id, if(age>22,"valid","invalid") as result from data;
select ifnull("hey","hello");
select ifnull(null,"hello");
select name,emp_id, ifnull(home_phone,office_phone) as phone_no from data;
Table
Lock table TABLE_NAME Read; {data is sharable, we can use only select command}
Lock table TABLE_NAME write; {it is exclusive lock, another user cannot access until the first user not
done, only one use can perform editing}
TCL Queries
1. Commit
2. Roll back
COMMIT-Both the queries are used for rough purpose, we can use the command of insert, delete
and update
Queries will not save automatically until we ran commit command
Start transaction;
Set autocommit = 0;
For example: Insert command / delete command / update command
Commit;
If you want to execute the command you need to run commit
VIEW
auto_increment – needs to maintain the primary key
create database new;
use new;
Create table TABLE_NAME(col_name1 datatype AUTO_INCREMENT, col_name2 datatype,
col_name3 datatype, primary key(col_name1));
Example:
create table stu(personid int not null auto_increment, name varchar(10),age int, primary
key(personid));
insert into stu(name, age) values ('jai', 20), ('karan',25),('isha',28);
select * from stu;
alter table stu1 auto_increment = 101;