Web Based Information Systems Flashcards
What is a SQL Schema? How would you write one?
A schema is a diagram of a database that showcases the tables, and their relationships
What is a one to many or many to many relationship?
In a database schema, the connections between tables specify the relationships. These are represented like 1* or 1.1 or .
What is the generic structure of a SQL query?
SELECT (item1, item2, item3)
FROM table
WHERE condition;
Remember always end a query with ;
How would you select all items from a table?
SELECT * FROM table;
How would you delete all items from a table?
DELETE FROM table;
How would you update a table?
UPDATE table
SET value1 = ‘Hello World’ WHERE id=’2’
How would you select a specific item from a table to delete?
DELETE FROM table WHERE item1 =’Hello World’;
How would you order results in ascending or descending order?
SELECT * FROM table
ORDER BY item1 ASC
SELECT * FROM table
ORDER BY item1 DESC
How would you order results alphabetically, In both directions?
SELECT * FROM table
ORDER BY item1;
SELECT * FROM table
ORDER BY item1 DESC;
What is a Primary Key?
A key that can be used to uniquely identify an element from a given table.
Here we are setting a primary key to be an id that is auto incrementing.
id INT NOT NULL AUTO_INCREMENT;
PRIMARY KEY id;
What is a Composite Key?
A composite key is made up of more than one field to uniquely identify an item from a given table.
…
name VARCHAR(50),
surname VARCHAR(50),
PRIMARY KEY (name, surname);
What are the main datatypes in SQL?
- VARCHAR (variable string length)
- INT (Integer)
- FLOAT (Floating point number)
- DECIMAL(10, 2) (Creates a fixed decimal point number)
- BOOL (Boolean, 0 is false)
- TEXT (Holds a large text block)
- CHAR (Fixed length string)
Consider the business problem of a supermarket tracking products. Buyers require the following information on every product to process procurement and to support marketing: product name, in-stock, price (the price per unit), supplier ID and outstanding (amounting owed to the supplier). Sketch out a SQL schema with the information above. You need to clearly identify the entity (or entities), attributes, data types and key (or keys).
- Write a query that returns the name of the products with in-stock values exceeding the outstanding debt to the supplier.
- Write a query that prints the money owed per supplier.
We need to break down the question first.
- product
- in-stock
- price
- supplier ID
- outstanding
So we need to first need to define our tables.
1. Product
2. Supplier
CREATE TABLE Supplier (
sID INT UNIQUE NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
outstanding DECIMAL(10, 2) NOT NULL,
PRIMARY KEY(sID)
);
CREATE TABLE Product (
pID INT UNIQUE NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
in_stock INT NOT NULL,
PRIMARY KEY (pID),
FOREIGN KEY (sID) REFERENCES Supplier(sID) ON DELETE CASCADE
);
SELECT p.name
FROM Product p
JOIN Supplier s ON p.sID = s.sID
WHERE p.in_stock > s.outstanding
SELECT s.sID s.name, SUM(p.price * p.in_stock) AS total_money_owed
FROM Supplier s
JOIN Product p ON s.sID = p.sID
GROUP BY s.sID, s.name;
What does JOIN mean in SQL?
We are able to connect multiple tables together using the JOIN method, depending on if the tables both share a key and foreign key.
SELECT p.name
FROM Product p
JOIN Supplier s ON p.sID = s.sID
What does it mean to use variables in SQL, how do they work and how would you use them?
If we have a table of Product and Supplier and we have a query, then we can simplify the query by using a variable name to represent a generic value from a table.
SELECT s.SID, s.name, SUM(p.price * p.in_stock) AS total_money_owed
This is taking two values of s and p, to represent the supplier and product, then these values were used to calculate the sum of the price times the amount in stock.
Define SUM and why would you use in in SQL?
SUM will return the total sum of a numeric column.
SELECT a.aID, SUM(a.value) + SUM(b.value) AS total_value
FROM ATable a
JOIN BTable b ON a.aID = b.bID
GROUP BY a.aID;
What is the definition of Eventual Consistency?
Eventual consistency is a property of distributed systems that ensures, given enough time and the absence of new updates, all replicas of a dataset will eventually converge to the same value. However, temporary inconsistencies may occur while updates propagate across nodes.
Why is Eventual Consistency important?
Scalability: It allows distributed databases to scale across multiple nodes while maintaining availability.
High Availability: Ensures the system remains operational even when some nodes experience network delays or failures.
Performance Optimisation: Unlike strong consistency, which requires synchronisation across all nodes, eventual consistency reduces latency and improves throughput.
Define what a conceptual model is
A model that showcases a database as a relationship based model, focusing on the relations.
- Entities become relations, often multiple entities merge into one.
- Relationships become FOREIGN KEYS
- Many to many relationships need to be resolved
What is a recursive relationship?
A relationship between a table and it’s elements, so table Person will have a 0.* relationship with personID
Give a one to one relationship example
A table of People and Department, where the Person is a manager and their ID, which is a PRIMARY KEY is a FOREIGN KEY in the Departments table.
Identify the PRIMARY KEY and any potential FOREIGN KEYS
Date | Time | Room_Booked | Person_Booking
23/06/2015 | 10:00 | CC03 | pjr
23/06/2015 | 12:00 | CC03 | ec34
27/07/2015 | 10:00 | CC01 | pjr
23/06/2015 | 10:00 | CC01 | pjr
If we look, the Date and Time and Room_booked fields are unique in combination, which could make up the PRIMARY KEY.
The FOREIGN KEYS probably come from the Room_booked and Person_booking.
PRIMARY KEY (Date, Time, Room_Booked)
FOREIGN KEY (Room_booked)
FOREIGN KEY (Person_booking)
What is the convention for writing relational database schemas?
We underline PRIMARY KEYS and FOREIGN KEYS, but we also might colour the FOREIGN KEYS to differentiate.
Name some characteristics of a relational data model
– each relation in a model has a distinct name (object identity)
– each attribute in a relation has a distinct name
– all values of an attribute are drawn from the same domain
– attribute values must be atomic
– ordering of attributes in a relation is of no significance
– ordering of elements in a relation is of no significance
– elements in a relation must be distinct; primary key (unique row
identifier) must not have a null value
* primary key may be composite
– non-key attributes may have null values
Mixing up PHP and HTML
hello ".$myvariable."" ?>“;
Some page with an header
Some content.
Thank you, '.$_POST['firstname'].'!
'"; echo $banana->get_name(); ?>