CREATE DATABASE {database name}; - to create a database
USE {database name}; - to use a database
DROP DATABASE {database name}; - to delete a database
ALTER DATABASE {database name} READ ONLY = 1; - make database read only
CREATE TABLE {table name}({colums} {datatypes}); - to create a table with columns
and their datatype
SELECT * FROM {table name}; - select everything in table
RENAME TABLE {original table name} TO {new table name}; - renaming a table
DROP TABLE {table name}; - delete table
ALTER TABLE {table name} - to alter
ways you can alter:
ADD {column} {data type}; - to add a new column
RENAME COLUMN {original column name} TO {new column name}; - to rename a column in
the table
MODIFY COLUMN {column name} {new data type}; - to change data type of column
MODIFY COLUMN {current column name} {data type} AFTER {another column}; - to move
the current column's position after some other column
MODIFY COLUMN {current column name} {data type} FIRST; - move a column to the first
position
DROP COLUMN {column name}; - to delete a column
INSERT INTO {table name} VALUES ({add values to table to all columns in order}); -
to add values to table(add more paranthesis to add values in rows)
Can do INSERT INTO {table name} (columns you want data for) VALUES (); - to add
data's of required columns
SELECT {columns to display}
FROM {table name}
WHERE {condition}; - to query and select certain data
UPDATE {table name}
SET {column name} = {value} (use commas between values to update multiple columns)
WHERE {column name} = {value}; - to update one of the values in a column in a table
DELETE FROM {table name] - to delete all values in table
WHERE {column name} = {value}; - to delete specific data
SET AUTOCOMMIT = OFF; - prevent saving transactions automatically
COMMIT; - create a savepoint
ROLLBACK; - go to previous savepoint
CURRENTDATE() - returns current date
CURRENTTIME() - returns current time
NOW() - returns current datetime
CURRENTDATA() + 1 - tomorrow's date
UNIQUE - primary key basically
ways to add unique constraint:
1. When making columns for a table
CREATE TABLE {table name} (
{column 1} {data type},
{column 2} {data type} UNIQUE
); - basically adds unique constraint to column 2
2. Altering the table later
ALTER TABLE {table name}
ADD CONSTRAINT
UNIQUE {column name}; - add unique constraint to a specific column
NOT NULL - another constraint to make sure values are not null in a column
ways to add it:
1. when making columns for a table (same as unique)
2. altering the table later
ALTER TABLE {table name}
MODIFY {column} {data type} NOT NULL;
CHECK - another thing to see if data entered matches conditions
can be added when creating table:
CREATE TABLE {table name} (
{column 1} {data type},
{column 2} {data type},
CHECK ({column} {boolean operator} {value}) - to check if the data in column
is adhering to the condition
);
can also give it a variable to drop it, or update it later
CREATE TABLE {table name} (
{column 1} {data type},
{column 2} {data type},
CONSTRAINT {var name} CHECK ({column} {boolean operator} {value})
);
or after creating table:
ALTER TABLE {table name}
ADD CONSTRAINT {var name} CHECK ({{column} {boolean operator} {value}); - to add
constraint to existing table
How to drop the check
ALTER TABLE {table name}
DROP CHECK {var name};
DEFAULT - assigns default value to unspecified column values when creating columns
can be made when creating table
CREATE TABLE {table name} (
{column 1} {data type},
{column 2} {data type} DEFAULT {value}
); - column 2 will have a default value of {value} when no data is entered
can be added after creating table
ALTER TABLE {table name}
ALTER {column} SET DEFAULT {value};
PRIMARY KEY - unique + not null
ways to add it:
1. at the start
2. altering
ALTER TABLE {table name}
ADD CONSTRAINT
PRIMARY KEY {column name};
ONLY 1 PRIMARY KEY PER TABLE
AUTO_INCREMENT - just like the name says, increments automatically whenever a value
is added
ways to add it:
1. At the start (column requires primary key)
CREATE TABLE {table name} (
{column 1} {data type},
{column 2} {data type} PRIMARY KEY AUTO_INCREMENT
);
2. Altering
ALTER TABLE {table name}
AUTO_INCREMENT = 1000; - starts incrementing from 1000
FOREIGN KEY - allows to link two tables (primary key of one table accessed by
foreign key of another table)
how to add it:
add it when creating table
CREATE TABLE {table name} (
{column 1} {data type},
{column 2} {data type} PRIMARY KEY AUTO_INCREMENT
FOREIGN KEY({column to take values from other table}) REFERENCES {table}
({column to be referenced})
);
how to drop foreign key
ALTER TABLE {table name}
DROP FOREIGN KEY {foreign key name}
How to give foreign key a unique name
ALTER TABLE {table name}
ADD CONSTRAINT {var name}
FOREIGN KEY({column to take values from other table}) REFERENCES {table}({column to
be referenced})
INNER JOIN - JOINS TWO TABLES BASED ON EVERYTHING THEY HAVE IN COMMON
SELECT *
FROM {left table name} INNER JOIN {right table name}
ON {left table name}.{foreign key column} = {right table name}.{primary key column}
LEFT JOIN - shows all values of the left table while still pulling in relevant
matching data from the right table
RIGHT JOIN - opposite of left join
SELECT COUNT({column name}) - to display number of rows in column
SELECT COUNT({column name}) AS "{name you want to display}" - to display result in
custom name
MAX
MIN
AVG
SUM
CONCAT
BETWEEN - operand for dates
IN - basically like OR for multiple statements
% any number of random characters
SELECT * FROM {table name}
WHERE {column} like "s%" - finds everything with letter s at start
_ represents one random letter
ORDER BY {column} - order rows based on this column
ORDER BY {column} {column 2} - if column 1 has same values, order by column 2
LIMIT - limit number of rows
can offset with limit
UNION - combines the result of two or more SELECT statements (both tables need same
number of columns)
SELECT * FROM {table 1 name}
UNION
SELECT * FROM {table 2 name};
UNION ALL - displays even duplicates
SELF JOIN - joins another copy of a table to itself
SELECT *
FROM {table 1} AS {var1}
INNER JOIN {table 1} AS {var2}
ON {var1}.{column} = {var2}.{column};
VIEWS - virtual table that is connected to real table
CREATE VIEW {view name} AS
SELECT {columns}
FROM {table}; - creates a view from the table's columns
INDEX - speeds up SELECT, slows down UPDATE
CREATE INDEX {index name}
ON {table name}({column name});
SHOW INDEXES FROM {table name};
ALTER TABLE {table name}
DROP INDEX {index name};
can make multicolumn index
GROUP BY - aggregate all rows by a specific column
WHERE BECOMES HAVING WHEN USING GROUP BY
ROLL UP - EXTENSION OF GROUP BY
PRODUCE ANOTHER ROW AND SHOWS GRAND TOTAL
SELECT SUM({column name}), {column name}
FROM {table name}
GROUP BY {column name} WITH ROLLUP;
ON DELETE SET NULL - WHEN FOREIGN KEY IS DELETED, REPLACE FOREIGN KEY WITH NULL
ON DELETE CASCADE - WHEN A FOREIGN KEY IS DELETED, DELETE ROW
delimiter - change ending symbol
triggers - keep track of events