DBMS & MySQL Full Course - Syntax & Notes
Create Database
CREATE DATABASE dbname;
Creates a new database with the name 'dbname'.
Create Table
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
PRIMARY KEY (column1)
);
Defines a new table with columns and their respective data types. Includes a primary key.
Insert Data
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
Inserts a new record into the specified columns of the table.
Select Query
SELECT column1, column2 FROM table_name WHERE condition;
Fetches records that meet the condition. '*' can be used to select all columns.
Update Data
UPDATE table_name SET column1 = value1 WHERE condition;
Modifies existing data in the table that matches the condition.
Delete Data
DELETE FROM table_name WHERE condition;
Removes rows that satisfy the specified condition from the table.
Add Column
ALTER TABLE table_name ADD column_name datatype;
Adds a new column to an existing table.
Modify Column
ALTER TABLE table_name MODIFY COLUMN column_name new_datatype;
Changes the datatype of a specified column.
Drop Column
ALTER TABLE table_name DROP COLUMN column_name;
Removes a column from the table.
Drop Table
DROP TABLE table_name;
Deletes an entire table from the database.
Primary Key
PRIMARY KEY (column_name);
Uniquely identifies each record in a table. Cannot have NULL or duplicate values.
Foreign Key
FOREIGN KEY (column_name) REFERENCES other_table(column_name);
Establishes a relationship between two tables.
Where Clause
SELECT * FROM table_name WHERE column = value;
Filters records based on the given condition.
Aggregate Functions
SELECT COUNT(*), AVG(column), SUM(column) FROM table_name;
Performs calculations on multiple values: count, average, sum, min, max.
Inner Join
SELECT * FROM A INNER JOIN B ON A.id = B.id;
Returns records that have matching values in both tables.
Left Join
SELECT * FROM A LEFT JOIN B ON A.id = B.id;
Returns all records from the left table and matched records from the right.
Right Join
SELECT * FROM A RIGHT JOIN B ON A.id = B.id;
Returns all records from the right table and matched ones from the left.
Full Join
SELECT * FROM A FULL OUTER JOIN B ON A.id = B.id;
Returns all records when there is a match in either table.
Group By
SELECT column, COUNT(*) FROM table GROUP BY column;
Groups rows sharing a property so aggregate functions can be applied.
Having Clause
SELECT column, COUNT(*) FROM table GROUP BY column HAVING COUNT(*) > 1;
Used to filter grouped data, similar to WHERE but after aggregation.
Subquery
SELECT * FROM table WHERE column = (SELECT column FROM table WHERE condition);
A query nested inside another query for complex filtering.
Normalization - 1NF
Eliminate repeating groups; each field contains only atomic values.
1NF ensures that the values in each column of a table are atomic (indivisible).
Normalization - 2NF
Ensure that all non-key attributes are fully functional dependent on the primary
key.
Removes partial dependencies.
Normalization - 3NF
Ensure no transitive dependency exists.
Removes columns not dependent on the primary key.
Stored Procedure
DELIMITER //
CREATE PROCEDURE procedure_name()
BEGIN
SQL statements;
END //
DELIMITER ;
A stored block of SQL code that can be reused and executed with a single call.
View
CREATE VIEW view_name AS SELECT columns FROM table WHERE condition;
Creates a virtual table based on a SELECT query.
Index
CREATE INDEX index_name ON table(column);
Improves data retrieval speed but requires more space and update time.