Podcast
Questions and Answers
What is a database?
What is a database?
- A collection of random files.
- A single document containing all data.
- A collection of organised data. (correct)
- A collection of disorganised data.
What is a DBMS?
What is a DBMS?
- A digital marketing system.
- A data mining statistics system.
- A data backup management system.
- A database management system. (correct)
Which of the following is NOT a functionality of a DBMS?
Which of the following is NOT a functionality of a DBMS?
- Enforcing constraints to ensure data integrity.
- Facilities for recovering the database should it become damaged.
- Unlimited and unrestricted data access for all users. (correct)
- Data storage, retrieval, and update.
In which database type is data stored in interrelated tables?
In which database type is data stored in interrelated tables?
Which type of database is suitable for handling unstructured, messy, and unpredictable data?
Which type of database is suitable for handling unstructured, messy, and unpredictable data?
What is the full form of SQL?
What is the full form of SQL?
What is a record also known as?
What is a record also known as?
What is SQLite?
What is SQLite?
Which of the following is NOT a step in database design?
Which of the following is NOT a step in database design?
What is the first stage in developing an effective database?
What is the first stage in developing an effective database?
What is an ERD?
What is an ERD?
In ERDs, how are entities typically represented?
In ERDs, how are entities typically represented?
In ERDs, how are relationships typically represented?
In ERDs, how are relationships typically represented?
What are attributes in the context of ERDs?
What are attributes in the context of ERDs?
How are attributes drawn?
How are attributes drawn?
What is a primary key?
What is a primary key?
What is a surrogate key?
What is a surrogate key?
What happens when the primary key is violated?
What happens when the primary key is violated?
In the context of converting ERDs to tables, what do entities become?
In the context of converting ERDs to tables, what do entities become?
In the context of converting ERDs to tables, what do attributes become?
In the context of converting ERDs to tables, what do attributes become?
What is a foreign key?
What is a foreign key?
In which type of relationship do you need to create a junction table?
In which type of relationship do you need to create a junction table?
What is the other name of a junction table?
What is the other name of a junction table?
What is a composite primary key?
What is a composite primary key?
What programming language is used to create tables within our database?
What programming language is used to create tables within our database?
What does the CREATE TABLE
syntax do?
What does the CREATE TABLE
syntax do?
What command is used to delete a table?
What command is used to delete a table?
What does the INSERT INTO
command do?
What does the INSERT INTO
command do?
Which SQL command is used to retrieve data from a database?
Which SQL command is used to retrieve data from a database?
In a SELECT
statement, what does *
signify?
In a SELECT
statement, what does *
signify?
What is the purpose of the WHERE
keyword in SQL?
What is the purpose of the WHERE
keyword in SQL?
What is the purpose of the LIKE
operator in SQL?
What is the purpose of the LIKE
operator in SQL?
In SQL, what does the wildcard %
match in a LIKE
operator?
In SQL, what does the wildcard %
match in a LIKE
operator?
What is the purpose of the ORDER BY
keyword in SQL?
What is the purpose of the ORDER BY
keyword in SQL?
In ORDER BY
, what does the keyword DESC
signify?
In ORDER BY
, what does the keyword DESC
signify?
What does the AS
keyword do in SQL?
What does the AS
keyword do in SQL?
Which clauses are used to restrict the number of rows a select statement returns?
Which clauses are used to restrict the number of rows a select statement returns?
What is SQL aggregate function?
What is SQL aggregate function?
Which aggregate function returns the number of rows in a table?
Which aggregate function returns the number of rows in a table?
What is the primary function of a Database Management System (DBMS)?
What is the primary function of a Database Management System (DBMS)?
In database terminology, what does ERD stand for?
In database terminology, what does ERD stand for?
Which database type organizes data into tables with rows and columns?
Which database type organizes data into tables with rows and columns?
What is the purpose of a primary key in a database table?
What is the purpose of a primary key in a database table?
Which SQL command is used to add new data to a database table?
Which SQL command is used to add new data to a database table?
Which SQL JOIN type returns all rows from both tables, joining records where available?
Which SQL JOIN type returns all rows from both tables, joining records where available?
Which SQL JOIN type returns only the rows that have matching values in both tables?
Which SQL JOIN type returns only the rows that have matching values in both tables?
Which SQL JOIN type returns all rows from the left table and matching rows from the right table?
Which SQL JOIN type returns all rows from the left table and matching rows from the right table?
Write an SQL query to retrieve distinct values from the "department" column of the "employees" table.
Write an SQL query to retrieve distinct values from the "department" column of the "employees" table.
Write an SQL query to retrieve the names of employees whose salary is greater than 50000.
Write an SQL query to retrieve the names of employees whose salary is greater than 50000.
Write an SQL query to retrieve the number of employees in each department.
Write an SQL query to retrieve the number of employees in each department.
Write an SQL query to retrieve the names of employees whose names start with 'A'.
Write an SQL query to retrieve the names of employees whose names start with 'A'.
Write an SQL query to retrieve the names of employees who belong to the 'Sales' department and have a salary greater than 60000.
Write an SQL query to retrieve the names of employees who belong to the 'Sales' department and have a salary greater than 60000.
Flashcards
What is a database?
What is a database?
A collection of data organized for access, retrieval, modification, and use.
What is a Database Management System (DBMS)?
What is a Database Management System (DBMS)?
Software for creating and managing databases, serving as an interface between users and the database.
What is a Relational Database (RDB)?
What is a Relational Database (RDB)?
Data is stored in interrelated tables
What is an Object-Oriented Database (OODB)?
What is an Object-Oriented Database (OODB)?
Signup and view all the flashcards
What are Non-Relational Databases (NoSQL)?
What are Non-Relational Databases (NoSQL)?
Signup and view all the flashcards
How do Relational Databases work?
How do Relational Databases work?
Signup and view all the flashcards
What is data redundancy?
What is data redundancy?
Signup and view all the flashcards
What are the four basic steps for database design?
What are the four basic steps for database design?
Signup and view all the flashcards
What is an Entity-Relationship Diagram (ERD)?
What is an Entity-Relationship Diagram (ERD)?
Signup and view all the flashcards
What are Entities in ERD?
What are Entities in ERD?
Signup and view all the flashcards
How are Entities drawn in an ERD?
How are Entities drawn in an ERD?
Signup and view all the flashcards
How are Relations drawn in an ERD?
How are Relations drawn in an ERD?
Signup and view all the flashcards
What are Attributes in ERDs?
What are Attributes in ERDs?
Signup and view all the flashcards
What are Primary Keys?
What are Primary Keys?
Signup and view all the flashcards
What is a database-generated sequence number?
What is a database-generated sequence number?
Signup and view all the flashcards
What are Universally Unique Identifiers (UUIDs)?
What are Universally Unique Identifiers (UUIDs)?
Signup and view all the flashcards
What is a Relationship Attribute?
What is a Relationship Attribute?
Signup and view all the flashcards
How are table definitions written?
How are table definitions written?
Signup and view all the flashcards
Do you use singular or plural for table names?
Do you use singular or plural for table names?
Signup and view all the flashcards
Foreign Key
Foreign Key
Signup and view all the flashcards
How are foreign keys used in one-to-many relations?
How are foreign keys used in one-to-many relations?
Signup and view all the flashcards
What happens to many-to-many relations?
What happens to many-to-many relations?
Signup and view all the flashcards
What is a composite primary key?
What is a composite primary key?
Signup and view all the flashcards
List 3 scenarios where 1-to-1 relationships can be used
List 3 scenarios where 1-to-1 relationships can be used
Signup and view all the flashcards
What is Structured Query Language (SQL)?
What is Structured Query Language (SQL)?
Signup and view all the flashcards
What does the CREATE TABLE syntax allow someone to define?
What does the CREATE TABLE syntax allow someone to define?
Signup and view all the flashcards
What does CREATE TABLE do?
What does CREATE TABLE do?
Signup and view all the flashcards
What is important when creating foreign keys?
What is important when creating foreign keys?
Signup and view all the flashcards
What is the command for deleting a table?
What is the command for deleting a table?
Signup and view all the flashcards
What is the syntax for the INSERT INTO command?
What is the syntax for the INSERT INTO command?
Signup and view all the flashcards
What is the SQL command for extracting data?
What is the SQL command for extracting data?
Signup and view all the flashcards
What does SELECT * FROM TableName do?
What does SELECT * FROM TableName do?
Signup and view all the flashcards
How do you select certain columns of the table?
How do you select certain columns of the table?
Signup and view all the flashcards
What does the WHERE keyword do?
What does the WHERE keyword do?
Signup and view all the flashcards
What does the LIKE Operator do?
What does the LIKE Operator do?
Signup and view all the flashcards
What does the IN operator do?
What does the IN operator do?
Signup and view all the flashcards
What does the ORDER BY keyword do?
What does the ORDER BY keyword do?
Signup and view all the flashcards
What does the AS (alias) keyword do?
What does the AS (alias) keyword do?
Signup and view all the flashcards
What does LIMIT clause do?
What does LIMIT clause do?
Signup and view all the flashcards
What does OFFSET clause to?
What does OFFSET clause to?
Signup and view all the flashcards
What are Aggregate Functions?
What are Aggregate Functions?
Signup and view all the flashcards
List some Aggregate Functions
List some Aggregate Functions
Signup and view all the flashcards
Why use the GROUP BY keyword?
Why use the GROUP BY keyword?
Signup and view all the flashcards
What is the HAVING clause?
What is the HAVING clause?
Signup and view all the flashcards
What is a VIEW?
What is a VIEW?
Signup and view all the flashcards
What is a JOIN in SQL?
What is a JOIN in SQL?
Signup and view all the flashcards
List 3 JOIN types.
List 3 JOIN types.
Signup and view all the flashcards
Study Notes
- Databases are collections of data organized for access, retrieval, modification, and usage.
- An online store database exemplifies this, storing customer details, products, and prices as XML or JSON documents.
- Database design and management are crucial for IT and cybersecurity, as well-structured databases ensure efficient data storage, retrieval, and security.
- Understanding ERDs, SQL and data relationships is essential for building scalable systems, preventing data breaches and supporting informed decision making in industry.
- Databases and database management systems (DBMS) are closely related, and the term 'database' often refers to both.
Database Management Systems (DBMS)
- A DBMS is software that manages databases, serving as an interface between users and the database.
- It allows users to create, read, update, and delete data.
- Core functionalities include data storage, retrieval, and updates, along with transaction support, concurrency, and recovery mechanisms.
- DBMS also handle access authorization, remote support, and constraint enforcement for data integrity.
Main Database Types
- Relational Databases (RDB): Data stored in interrelated tables (e.g., Microsoft SQL Server, Oracle, MySQL).
- Object-Oriented Databases (OODB): Allows definition of objects that can be referenced later as a unit; used in applications requiring high performance and faster results (e.g., ObjectBox).
- Network Database: First-generation database model linking multiple member records to multiple owner files (e.g., IMAGE).
- Non-Relational Databases (NoSQL): Handles unstructured data, ideal for big data applications (e.g., Apache Cassandra).
Relational Databases
- Relational databases organize data into tables with rows (records/tuples) and columns (fields/attributes).
- Data is structured and related, enabling efficient querying and manipulation using SQL.
- SQLite is used in the topic, it is a small, simplified relational database that doesn't require a dedicated server and is popular in mobile apps.
Database Design Steps
- Four steps for effective database design:
- Model the problem into an entity–relationship diagram (ERD).
- Convert the ERD into a set of tables.
- Create the tables in the database using SQL and import any data.
- Create queries to extract information.
Entity-Relationship Diagrams (ERDs)
- ERDs model problems visually using symbols and connectors.
- They define major entities and their interrelationships within the system.
- Entities include business objects like people (e.g., student), tangible items (e.g., product), and intangible items (e.g., log).
- Entities are represented as rectangles, and relationships as diamonds with connecting lines, indicating one-to-many (1:M) relationships.
ERD Entities
- Entities relate to each other.
- Relation is expressed as a verb (action word).
- For an online shop:
- Customer: the person who is buying the products.
- Product: the thing to buy.
- Order: a collection of things the customer is buying.
- Brand: the company that made the product.
Adding Attributes to Entities
- Attributes provide information about each entity and are drawn as ovals, with names that should not contain spaces or symbols.
- Unique attributes serve as primary keys, identified with an underline.
- Primary keys are enforced by the database to prevent duplicate records.
Primary Keys
- Unique data identifiers are presented as a data type with an underline.
- The database enforces the primary key.
- It will refuse to add a record if it violates the primary key.
- It is often best to make up a number to use as a primary key, known as a surrogate key,
- When designing a database, primary keys should not have duplicates or undergo changes.
- Types:
- Database-generated sequence number: The database creates IDs for you. As a result, the database can perform performance enhancements.
- Large random numbers: Also known as universally unique identifiers (UUIDs).
Adding Attributes to Relationships
- Relationships can also have attributes, such as including a quantity attribute in the relationship between order and product for an online shop.
- Relationships can also have attributes, including a quantity attribute in the relationship between order and product, to reduce redundancies and add extra efficiencies.
Converting ERDs to Tables
- Involves turning entities into tables and their attributes into columns.
- Table definitions are written as TABLENAME (attribute1, attribute2, attribute...), with the primary key underlined.
- Singular names are conventionally used for table names (e.g., 'customer' instead of 'customers').
Foreign Keys
- Foreign keys link tables by representing relationships, such as one-to-many, many-to-many, and one-to-one.
- In one-to-many relations, the M entity receives a foreign key, which is an attribute containing the primary key of the other entity.
- Foreign keys do not appear on ERDs.
Many-to-Many Relations
- Require the creation of a junction table containing the primary keys of both tables involved.
- A primary key with multiple columns is known as a composite primary key, which must be unique for each record.
One-to-One Relations
- Implemented for access control with confidential attributes or table inheritance for adding more attributes to a table.
- Examples:
- Confidential data placed in separate table
- PERSON (PersonID, FirstName, LastName, DOB, Address)
- STUDENT (PersonID, StudentNo, Degree, ATAR)
- STAFF (PersonID, StaffNo, Office, Salary).
Creating Tables Using SQL
- Tables are created using Structured Query Language (SQL).
- Most relational databases support basic SQL operations, though some may have extra features.
- SQL is a popular language in database design as it is standard.
- However, some database software may have extra features that others do not. And there may be slight differences in the commands available.
CREATE TABLE Syntax
- Defines the structure of a new table, including column names, data types, constraints, and indexes.
- Example:
CREATE TABLE Customer (
CustomerID INTEGER ,
FirstName TEXT ,
LastName TEXT ,
Address TEXT ,
Phone TEXT , PRIMARY KEY (CustomerID)
);
- Ensures that the data type for foreign keys matches the corresponding primary key's data type.
DROP TABLE
- The command for deleting a table is:
DROP TABLE MyCoolTable;
INSERT INTO Syntax
- Used to insert data into a table.
- Example:
INSERT INTO Customer VALUES ( 1 , 'Fred' , 'Banks' , '3134 Hudson Lane' , '405195595' ); INSERT INTO Customer VALUES ( 2 , 'Nicholas' , 'Ramos' , '9 Washington Center' , '946151556' ); INSERT INTO Customer VALUES ( 3 , 'Anne' , 'Hansen' , '8604 Burrows Pass' , '129895116' ); INSERT INTO Customer VALUES ( 4 , 'Douglas' , 'Mendoza' , '9 Monument Avenue' , '898719584' ); INSERT INTO Customer VALUES ( 5 , 'Bonnie' , 'Lawson' , '9 Oneill Avenue' , '887514739' ); INSERT INTO Customer VALUES ( 6 , 'Elizabeth' , 'Ryan' , '1289 Del Mar Street' , '887514733' );
Extracting Data with SQL
- SQL commands: Allows you to set up and fill your database.
- Critical for interacting with data stored in our database.
SELECT Command
- Is the primary command in SQL that allows you to retrieve data from the database.
- Retrieves data from the database.
SELECT * FROM TableName;
is used to select all data from a table.- Specific columns can be selected by listing their names after the SELECT command.
WHERE Keyword
- Filters rows based on specified expressions.
SELECT * FROM Customer WHERE LastName = 'Wood';
only shows the row if it is true.- WHERE always comes after the FROM keyword
LIKE Operator
- Matches text based on patterns, using wildcards like % (zero or more characters) and _ (exactly one character).
- The LIKE operator is used to check if text matches a particular pattern.
- In SQLite, the LIKE operator is not case-sensitive, but some databases are.
SELECT * FROM Customer WHERE LastName LIKE 'W%';
IN Operator
- Checks if a value is in a list of possible values.
- Example:
SELECT * FROM Customer
WHERE CustomerID IN (2,3,4);
ORDER BY
- Sorts results in a specific order, either ascending (ASC) or descending (DESC).
- For this, the ORDER BY keyword can be used.
- ORDER BY has two extra keywords:
- ASC — ascending order (smallest to largest; this is a default one)
- DESC — descending order (largest to smallest).
SELECT * FROM Customer
ORDER BY LastName ASC;
AS (Alias)
- Renames a column in the result set.
- Example:
SELECT FirstName AS FN, LastName AS LN
FROM Customer;
LIMIT and OFFSET
- Restrict the number of rows returned.
- LIMIT determines the number of rows the query returns in this syntax.
- OFFSET clause skips the offset rows before returning data from the rows.
- These two keywords appear at the end of the SQL statement and are often used with ORDER BY.
Aggregate Data
- Functions that operate on multiple rows to combine them into a single row.
- COUNT() — count all the values
- SUM() — add all the values together and return the sum
- AVG() — compute the average
- MIN() — get the lowest value
- MAX() — get the highest value.
COUNT()
- Returns the number of rows in the table whether using COUNT(1) or COUNT(*), but COUNT(ProductID) will only count rows where ProductID is not null.
- Always returns a single row (unless GROUP BY is used).
SELECT COUNT (ProductID) FROM Product;
AVG(), MIN(), MAX()
- Compute average, minimum, and maximum values respectively which is shown in the example below:
SELECT AVG(Price), MIN(Price), Max(Price) FROM Product;
GROUP BY Keyword
- Used to determine average price per brand.
SELECT AVG(Price), BrandID
FROM Product
GROUP BY BrandID;
WHERE Clause
- WHERE clause is processed first
- In this query, rows with a BrandID that is < 3 are removed first, and then the AVG() is applied to the remaining rows.
SELECT ProductID,AVG(Price)
FROM Product
WHERE BrandID < 3
GROUP BY ProductID;
- Aggregate not useable:
- WHERE AVG(Price) > 14 is invalid because the AVG occurs after the where clause has been processed.
HAVING Clause
- Similar to WHERE, but occurs after aggregates are finished.
- This example only shows brands that have an average price of over $20:
SELECT ProductID,AVG(Price)
FROM Product
WHERE BrandID < 3
GROUP BY ProductID;
VIEW
- A saved query that works like a regular table, except the rows cannot be edited.
- Useful for large and complex queries.
- Command to make a view
CREATE VIEW MyCoolView AS
SELECT*FROM Mycool table;
Combining Tables
- Uses JOINs to connect and retrieve information from multiple tables.
- JOINs occur after the FROM section and usually have an ON clause to specify connection conditions.
JOIN Types
- INNER JOIN: Most common, returns rows with matching values in both tables.
- LEFT JOIN: Returns all rows from the left table and matching rows from the right table.
- RIGHT JOIN: Returns all rows from the right table and matching rows from the left table.
- FULL OUTER JOIN: Returns all rows when there is a match in either the left or right table
ON Clause
- Connects primary and foreign keys during a JOIN operation.
- Note that if you forget to provide the ON clause, the database will return all possible combinations of the rows from each table.
SELECT Product.ProductID, Description, Price, OrderID
FROM Product
LEFT JOIN OrderProduct
ON OrderProduct.ProductID = Product.ProductID;
USING Clause
- Replaces the ON clause and makes the query easier to read, only when the column (filed) in the brackets appears in both tables (Product and OrderProduct).
- Example query:
SELECT Product.ProductID, Description, Price, OrderID
FROM Product
LEFT JOIN OrderProduct
USING (ProductID);
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.