0% found this document useful (0 votes)
92 views4 pages

INSY6112 SQL & MongoDB Syntax Cheat Sheet

This cheat sheet provides essential SQL and MongoDB syntax for creating tables, inserting data, selecting records, and creating views in SQL, as well as database creation and data insertion in MongoDB. It includes examples for each command to illustrate usage. The document serves as a quick reference for developers working with these database technologies.

Uploaded by

moekadi.molefe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views4 pages

INSY6112 SQL & MongoDB Syntax Cheat Sheet

This cheat sheet provides essential SQL and MongoDB syntax for creating tables, inserting data, selecting records, and creating views in SQL, as well as database creation and data insertion in MongoDB. It includes examples for each command to illustrate usage. The document serves as a quick reference for developers working with these database technologies.

Uploaded by

moekadi.molefe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

You might also like