Database syntax (by chatGPT)
Database syntax (by chatGPT)
Data Querying
• SELECT: Retrieve data from a table.
• GROUP BY: Group rows that have the same values in specified columns.
SELECT column1, COUNT(*) FROM table_name GROUP BY column1 HAVING COUNT(*) > 1;
• JOIN: Combine rows from two or more tables based on a related column.
○ INNER JOIN: Only return matching rows.
○ LEFT JOIN (or LEFT OUTER JOIN): Return all rows from the left table, and the matched rows
from the right table.
○ RIGHT JOIN (or RIGHT OUTER JOIN): Return all rows from the right table, and the matched
rows from the left table.
○ FULL JOIN (or FULL OUTER JOIN): Return all rows when there is a match in either left or
right table.
Database Page 1
FULL JOIN table2 ON table1.common_column = table2.common_column;
2. Data Manipulation
• INSERT INTO: Insert new data into a table.
3. Data Definition
• CREATE TABLE: Create a new table.
○ Drop a column:
• TRUNCATE TABLE: Remove all data from a table (but keep the structure).
4. Constraints
• PRIMARY KEY: Uniquely identifies each record in a table.
Database Page 2
• FOREIGN KEY: Ensures referential integrity by linking two tables.
5. Transactions
• START TRANSACTION: Begins a transaction.
START TRANSACTION;
COMMIT;
ROLLBACK;
6. Views
• CREATE VIEW: Create a virtual table based on the result of a SELECT query.
Database Page 3
CREATE VIEW view_name AS
SELECT column1, column2 FROM table_name WHERE condition;
7. Indexes
• CREATE INDEX: Create an index on a table for faster retrieval.
8. Aggregate Functions
• COUNT(): Returns the number of rows.
Database Page 4