INSY6112 SQL & MongoDB Syntax Cheat
Sheet
SQL Syntax
1. CREATE TABLE
```sql
CREATE TABLE TableName (
Column1 DataType Constraints,
Column2 DataType Constraints, ...
PRIMARY KEY (ColumnName)
);
EXAMPLE
CREATE TABLE Client (
ClientID INT PRIMARY KEY,
ClientName VARCHAR(50),
ClientSurname VARCHAR(50),
ClientAddress VARCHAR(100)
);
Insert Into
INSERT INTO TableName (Column1, Column2, ...)
VALUES (Value1, Value2, ...);
EXAMPLE
INSERT INTO Client (ClientID, ClientName, ClientSurname, ClientAddress)
VALUES (1, 'Debbie', 'Theart', '123 Long Road');
SELECT
SELECT Column1, Column2, ...
FROM TableName
WHERE Condition
ORDER BY ColumnName ASC|DESC;
EXAMPLE
SELECT ClientName, ClientSurname
FROM Client
WHERE ClientID = 1
ORDER BY ClientName ASC;
Create a View
CREATE VIEW ViewName AS
SELECT Column1, Column2, ...
FROM TableName
WHERE Condition;
EXAMPLE
CREATE VIEW ClientView AS
SELECT ClientName, ClientSurname, ClientAddress
FROM Client
WHERE ClientID IS NOT NULL;
MongoDB Syntax
Create Database
use databaseName;
EXAMPLE
use clients_s123456; // Replace with your student number
Insert Many
[Link]([
{ Field1: Value1, Field2: Value2, ... },
{ Field1: Value1, Field2: Value2, ... }
]);
EXAMPLE
[Link]([
{ ClientName: "Debbie", ClientSurname: "Theart", ClientDOB: "1980-03-17" },
{ ClientName: "Thomas", ClientSurname: "Duncan", ClientDOB: "1976-08-12" }
]);
Find
[Link]({ Query });
EXAMPLE
[Link]({}); // Get all documents in the clients collection
With Conditions:
[Link]({ ClientDOB: { $gt: "1979-01-12" } }); // Que
Excluded Fields:
[Link]({}, { FieldToExclude: 0 });
EXAMPLE:
[Link]({}, { ClientSurname: 0 }); // Excludes ClientSurname from the results
• The first {} is the query filter (empty means all documents).
• The second { ClientSurname: 0 } is the projection, where 0 indicates that the
ClientSurname field should be excluded from the results. You can include other
fields by specifying them with 1.