SQL Cheet Sheet
SQL Cheet Sheet
SHOW DATABASES;
USE myDB;
SHOW TABLES;
DESCRIBE employee;
/** SQL is case insensitive both will retun the values **/
/** Auto commit commit and rollback , autocommit is on by default , if we set to off then we can revert
the check point
to save use COMMIT and when we rollback it will revert to the previous commit**/
COMMIT;
ROLLBACK;
CREATE TABLE products ( pid INT, pname VARCHAR(40) UNIQUE,price DECIMAL(4,2) DEFAULT 0.00);
DESCRIBE products;
CREATE TABLE transactions( tid INT, amount DECIMAL(5,2), tdate DATETIME DEFAULT NOW());
/* PRIMARY KEY */
CREATE TABLE transactions( tid INT PRIMARY KEY, amount DECIMAL(5,2), tdate DATETIME DEFAULT
NOW());
DESCRIBE transactions;
CREATE TABLE transactions( tid INT PRIMARY KEY AUTO_INCREMENT , amount DECIMAL(5,2), tdate
DATETIME DEFAULT NOW());
CREATE TABLE transactions( tid INT PRIMARY KEY, amount DECIMAL(5,2), tdate DATETIME DEFAULT
NOW());
/* foreign key */
/* PK in one table used as a referene in another table , THERE WILL BE AL INK CREATED BTETWEEN 2
TABLES */
VALUES("tom", "SC"),
("dick", "AN"),
("harry", "DE");
VALUES("pintu", "SC");
CREATE TABLE transactions( tid INT PRIMARY KEY AUTO_INCREMENT , amount DECIMAL(5,2), cid INT,
FOREIGN KEY(cid) REFERENCES customers(cid));
/*ALTER TABLE transactions DROP CONSTRAINT FOREIGN KEY nameof key from ;*/
VALUES(36,2);
VALUES(13,NULL);
/**** JOINS */
/* INNER JOIN */
/* FUNCTIONS */
/* logical operators */
SELECT * FROM employee where hiredate > "2021-01-01" AND NOT job="cook";
SELECT * FROM employee where first_name LIKE "s%"; /* starting from s*/
SELECT * FROM employee where first_name LIKE "%r";/* ending from r*/
SELECT * FROM employee where job LIKE "_ook"; /* looks for 1 character folloed by 1 letter */
SELECT * FROM employee ORDER BY first_name DESC LIMIT 2, 3; /* skips first 2 records in reverse
order ,fetchs the record starting from 3 and display 3,4, and 5 records */
SELECT DISTINCT amount,cid from transactions ORDER BY amount DESC LIMIT 1,1; /* second largest
salary */
/* UNION operator combies the results of the 2 sSELECT * FROM transactions ORDER BY amount
DESC;elect statement UNION DOES NOT ALLOW DUPLICATES , UNIION ALLL DISPLAYES DUPLICATES
UNION
UNION ALL
-- views
-- views are virtul tables based on the result set of an SQL statemet
-- the fields are from the one or more real tables in the DB
-- they are not tabels, but can be interacted with as same as tables
-- if any changes to the mail table then the view gets automatically updated
-- list index
-- droppping index
-- sub queries
SELECT first_name,last_name from (SELECT DISTINCT cid FROM transactions WHERE cid IS NOT NULL);
SELECT first_name,last_name FROM customers where cid IN ( SELECT cid FROM transactions WHERE
cid IS NOT NULL);
-- group by
-- ofetn used with aggregate function such as SUM(), MAX(), MIN (), AVG(),COUNT()
-- produces another row and shows the GRAND TOTAL ( super - aggregate value)
SELECT SUM(AMOUNT), cid FROM transactions GROUP BY cid WITH ROLLUP ;
-- check SET foreign_key_checks = 0 ; will allow to delete wvwn if there is a FK reference . 1 will not
allow to delete
CREATE TABLE transactions( tid INT PRIMARY KEY AUTO_INCREMENT , amount DECIMAL(5,2), cid
INT, FOREIGN KEY(cid) REFERENCES customers(cid) ON DELETE SET NULL);
ALTER TABLE transactions ADD CONSTRAINT fk_cid FOREIGN KEY(cid) REFERENCES customers(cid)
ON DELETE CASCADE;
-- stored procedure
-- is a prepared SQL code that you can save if the query is used often .
-- increases perfromance
DELIMITER $$
BEGIN
END $$
DELIMITER ;
CALL get_customers();
DELIMITER $$
BEGIN
END $$
DELIMITER ;
CALL find_customer(2);
DELIMITER $$
BEGIN
END $$
DELIMITER ;
CALL fname_customer("teddy","jerry");
SHOW TRIGGERS;
SHOW TRIGGERS;
UPDATE expenses
SHOW TRIGGERS;
UPDATE expenses
UPDATE expenses