I have even uploaded the .sql file which you can download and directly run them in the sql prompt.
To run sql files
source <filename>.sql;
INT
TINYINT
SMALLINT
MEDIUMINT
BIGINT
FLOAT(M,D)
DOUBLE(M,D)
DECIMAL(M,D)
DATE -- Format - (YYYY-MM-DD)
DATETIME -- Format - (YYYY-MM-DD HH:MM:SS)
TIME -- Format - (HH:MM:SS)
CHAR(M)
VARCHAR(M)
BLOB or TEXT
/* Multi
line
comment */
# Single Line Comment
-- Single Line Comment
create database cheatsheet;
use cheatsheet;
show databases;
create table employee
(
employee_id int primary key, -- Setting primary key(1st method)
first_name varchar(50),
last_name varchar(50),
dept_number int,
age int,
salary real
);
create table department
(
dept_number int,
dept_name varchar(50),
dept_location varchar(50),
emp_id int,
primary key(dept_number) -- Setting primary key(2nd method)
);
show tables;
describe employee;
desc employee;
show columns in employee;
rename table employee to employee_table;
alter table employee_table rename to employee;
alter table employee change column employee_id emp_id int;
alter table employee change column first_name first_name varchar(50) not null;
alter table employee add column salary real;
alter table employee drop column salary;
alter table employee modify column salary int;
truncate employee;
drop table department;
drop database cheatsheet;
insert into employee (employee_id, first_name, last_name, dept_number, age, salary) values (1, "Anurag", "Peddi", 1, 20, 93425.63);
insert into employee values (2, "Anuhya", "Peddi", 2, 20, 83425.63);
insert into employee (employee_id, first_name) values (3, "Vageesh");
update employee set salary = 1.1 * salary;
update employee set salary = 1.2 * salary where employee_id = 1;
delete from employee where employee_id = 2;
delete from employee;
set foreign_key_checks = 1;
set foreign_key_checks = 0;
select * from employee;
select employee_id, first_name from employee;
select employee_id, first_name from employee where age > 25;
select * from employee where salary > 3100;
select * from employee where salary >= 3100;
select * from employee where salary < 4500;
select * from employee where salary <= 4350;
select * from employee where salary > 3000 and salary < 4000;
select * from employee where salary between 3000 and 4000;
select * from employee where salary = 3000 or salary = 4000;
select * from employee where salary is NULL;
select * from employee where salary is NOT NULL;
select * from employee ORDER BY salary DESC;
select * from employee where name like '%Jo%'; -- Similar to *Jo* in regrex
select * from employee where name like 'Jo_'; -- Similar to Jo. in regrex
create view personal_info as select first_name, last_name, age from employees;
select * from personal_info;
update personal_info set salary = 1.1 * salary;
delete from personal_info where age < 40;
drop view personal_info;
select e.fname, p.pname from employees as e inner join project as p on e.eid = p.eid;
-- or
select e.fname, p.pname from employees as e join project as p on e.eid = p.eid;
select e.fname, p.pname from employees as e left outer join project as p on e.eid = p.eid
union
select e.fname, p.pname from employees as e right outer join project as p on e.eid = p.eid;
select e.fname, p.pname from employees as e left outer join project as p on e.eid = p.eid;
select e.fname, p.pname from employees as e right outer join project as p on e.eid = p.eid;
select e.fname, p.pname from employees as e left outer join project as p on e.eid = p.eid where p.pname is null;
select e.fname, p.pname from employees as e right outer join project as p on e.eid = p.eid where e.fname is null;
select sum(population) from city group by population;
select avg(population) from city group by population;
select district, count(district) from city group by district;
select max(population) from city group by population;
select min(population) from city group by population;
select stddev(population) from city group by population;
select group_concat(population) from city group by population;
Only COUNT function considers NULL values
create procedure display_dbs()
show databases;
call display_dbs();
drop procedure display_dbs;
start transaction;
savepoint sv_pt;
delete from city; -- changing data in table
rollback to sv_pt;
release savepoint sv_pt;
commit;
create table emp_dup like employee;
create table emp_dup select * from employee;
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
the hostname part is set to localhost
, so the user will be able to connect to the MySQL server only from the localhost.
To grant access from another host, change the hostname part with the remote machine IP.
CREATE USER 'username'@'172.8.10.5' IDENTIFIED BY 'user_password';
To create a user that can connect from any host, '%' is used in the hostname part:
CREATE USER 'username'@'%' IDENTIFIED BY 'user_password';
GRANT ALL PRIVILEGES ON * . * TO 'username'@'localhost';
Asterisks(*) refers to the database and table names respectively.
By using asterisks we can give access of all the databases or tables to the user.
FLUSH PRIVILEGES
All the changes won't be in effect unless this query is fired.
GRANT type_of_permission ON database_name.table_name TO 'username'@'localhost';
type_of_permission
may have one of these value:
- ALL PRIVILEGES - Allows user full access to a designated database (or if no database is selected, global access across the system).
- CREATE - allows them to create new tables or databases.
- DROP - allows them to them to delete tables or databases.
- DELETE - allows them to delete rows from tables.
- INSERT - allows them to insert rows into tables.
- SELECT - allows them to use the
SELECT
command to read through databases. - UPDATE - allow them to update table rows.
- GRANT OPTION - allows them to grant or remove other users’ privileges.
Multiple permissions are given with commas.
REVOKE type_of_permission ON database_name.table_name FROM 'username'@'localhost';
SHOW GRANTS FOR 'username'@'localhost';
DROP USER 'username'@'localhost';
use mysql;
update user set authentication_string=PASSWORD("<new2-password>") where User='<user>';
flush privileges;
Stop MySQL service
sudo systemctl stop mysql
Restart MySQL service without loading grant tables
sudo mysqld_safe --skip-grant-tables &
The apersand (&) will cause the program to run in the background and --skip-grant-tables
enables everyone to to connect to the database server without a password and with all privileges granted.
Login to shell
mysql -u root
Set new password for root
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MY_NEW_PASSWORD';
FLUSH PRIVILEGES;
Stop and start the server once again
mysqladmin -u root -p shutdown
sudo systemctl start mysql
set @num = 10;
set @name = 'Anurag';
select @name;
set @n = 21;
select repeat("* ", @n := @n - 1) from information_schema.tables where @n > 0;
select round(3.141596, 3);
select repeat("* ", 20);
select rand();
select cast(23.01245 as signed);
select concat("Mahesh", " ", "Chandra", " ", "Duddu", "!");
select month("1998-12-30");
select year("1998-12-30");