Databases and DBMS

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

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?

  • 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?

  • 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?

<p>Relational databases (RDB). (D)</p> Signup and view all the answers

Which type of database is suitable for handling unstructured, messy, and unpredictable data?

<p>Non-relational databases (NoSQL). (A)</p> Signup and view all the answers

What is the full form of SQL?

<p>Structured Query Language. (D)</p> Signup and view all the answers

What is a record also known as?

<p>Rows or Tuples. (D)</p> Signup and view all the answers

What is SQLite?

<p>A small, simplified relational database. (B)</p> Signup and view all the answers

Which of the following is NOT a step in database design?

<p>Create a marketing plan for the database. (B)</p> Signup and view all the answers

What is the first stage in developing an effective database?

<p>Modeling the problem into an entity–relationship diagram (ERD). (A)</p> Signup and view all the answers

What is an ERD?

<p>A type of structural diagram that visualises entities and relationships. (B)</p> Signup and view all the answers

In ERDs, how are entities typically represented?

<p>Rectangles. (D)</p> Signup and view all the answers

In ERDs, how are relationships typically represented?

<p>Diamonds. (B)</p> Signup and view all the answers

What are attributes in the context of ERDs?

<p>Information about the entity. (B)</p> Signup and view all the answers

How are attributes drawn?

<p>Ovals. (B)</p> Signup and view all the answers

What is a primary key?

<p>Unique data identifiers. (C)</p> Signup and view all the answers

What is a surrogate key?

<p>A number used as a primary key. (B)</p> Signup and view all the answers

What happens when the primary key is violated?

<p>The system will generate an error. (C)</p> Signup and view all the answers

In the context of converting ERDs to tables, what do entities become?

<p>Tables. (D)</p> Signup and view all the answers

In the context of converting ERDs to tables, what do attributes become?

<p>Columns. (C)</p> Signup and view all the answers

What is a foreign key?

<p>An attribute in a table that links to another table. (B)</p> Signup and view all the answers

In which type of relationship do you need to create a junction table?

<p>Many-to-many. (B)</p> Signup and view all the answers

What is the other name of a junction table?

<p>Composite table. (B)</p> Signup and view all the answers

What is a composite primary key?

<p>A primary key with multiple columns. (C)</p> Signup and view all the answers

What programming language is used to create tables within our database?

<p>SQL. (C)</p> Signup and view all the answers

What does the CREATE TABLE syntax do?

<p>Creates a new table in a database. (C)</p> Signup and view all the answers

What command is used to delete a table?

<p><code>DROP TABLE</code> (B)</p> Signup and view all the answers

What does the INSERT INTO command do?

<p>Adds new data to a table. (C)</p> Signup and view all the answers

Which SQL command is used to retrieve data from a database?

<p>SELECT (C)</p> Signup and view all the answers

In a SELECT statement, what does * signify?

<p>Selects all columns from the table. (C)</p> Signup and view all the answers

What is the purpose of the WHERE keyword in SQL?

<p>To filter rows based on a condition. (C)</p> Signup and view all the answers

What is the purpose of the LIKE operator in SQL?

<p>To check if text matches a pattern. (B)</p> Signup and view all the answers

In SQL, what does the wildcard % match in a LIKE operator?

<p>Zero or more characters. (D)</p> Signup and view all the answers

What is the purpose of the ORDER BY keyword in SQL?

<p>To sort the results. (B)</p> Signup and view all the answers

In ORDER BY, what does the keyword DESC signify?

<p>Descending order. (C)</p> Signup and view all the answers

What does the AS keyword do in SQL?

<p>Renames a column. (C)</p> Signup and view all the answers

Which clauses are used to restrict the number of rows a select statement returns?

<p><code>LIMIT</code> and <code>OFFSET</code>. (A)</p> Signup and view all the answers

What is SQL aggregate function?

<p>Special functions that work over multiple rows and combine the rows. (D)</p> Signup and view all the answers

Which aggregate function returns the number of rows in a table?

<p><code>COUNT()</code> (C)</p> Signup and view all the answers

What is the primary function of a Database Management System (DBMS)?

<p>To create and manage databases. (A)</p> Signup and view all the answers

In database terminology, what does ERD stand for?

<p>Entity-Relationship Diagram (D)</p> Signup and view all the answers

Which database type organizes data into tables with rows and columns?

<p>Relational database (A)</p> Signup and view all the answers

What is the purpose of a primary key in a database table?

<p>To uniquely identify each record in the table. (C)</p> Signup and view all the answers

Which SQL command is used to add new data to a database table?

<p>INSERT INTO (C)</p> Signup and view all the answers

Which SQL JOIN type returns all rows from both tables, joining records where available?

<p>FULL JOIN (D)</p> Signup and view all the answers

Which SQL JOIN type returns only the rows that have matching values in both tables?

<p>INNER JOIN (A)</p> Signup and view all the answers

Which SQL JOIN type returns all rows from the left table and matching rows from the right table?

<p>LEFT JOIN (B)</p> Signup and view all the answers

Write an SQL query to retrieve distinct values from the "department" column of the "employees" table.

<p>SELECT DISTINCT department FROM employees; (B)</p> Signup and view all the answers

Write an SQL query to retrieve the names of employees whose salary is greater than 50000.

<p>SELECT name FROM employees WHERE salary &gt; 50000; (B)</p> Signup and view all the answers

Write an SQL query to retrieve the number of employees in each department.

<p>SELECT department, COUNT(*) AS employees_count FROM employees GROUP BY department; (C)</p> Signup and view all the answers

Write an SQL query to retrieve the names of employees whose names start with 'A'.

<p>SELECT name FROM employees WHERE name LIKE 'A%'; (A)</p> Signup and view all the answers

Write an SQL query to retrieve the names of employees who belong to the 'Sales' department and have a salary greater than 60000.

<p>SELECT name FROM employees WHERE department = 'Sales' AND salary &gt; 60000; (A)</p> Signup and view all the answers

Flashcards

What is a database?

A collection of data organized for access, retrieval, modification, and use.

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)?

Data is stored in interrelated tables

What is an Object-Oriented Database (OODB)?

Allows the definition of objects, which can then be referenced or called later as a unit without going into its complexities.

Signup and view all the flashcards

What are Non-Relational Databases (NoSQL)?

Can handle unstructured, messy, and unpredictable data, perfect for big data applications.

Signup and view all the flashcards

How do Relational Databases work?

Organises data into tables with rows (records) and columns (fields), enabling efficient querying and manipulation using SQL.

Signup and view all the flashcards

What is data redundancy?

Wastes space and can lead to update anomalies when data is repeated across a single table.

Signup and view all the flashcards

What are the four basic steps for database design?

  1. ERD Modeling 2. ERD conversion to Tables 3. SQL table creation and data import 4. Information extraction via SQL queries
Signup and view all the flashcards

What is an Entity-Relationship Diagram (ERD)?

A diagram that uses symbols and connectors to visualize entities within a system and their relationships.

Signup and view all the flashcards

What are Entities in ERD?

Business objects such as people, tangible items, or intangible concepts within a system.

Signup and view all the flashcards

How are Entities drawn in an ERD?

Drawn as rectangles, represent major entities.

Signup and view all the flashcards

How are Relations drawn in an ERD?

Drawn as diamonds, represent the relationships between entities.

Signup and view all the flashcards

What are Attributes in ERDs?

Information about an entity, drawn as ovals and connected to the entity.

Signup and view all the flashcards

What are Primary Keys?

Unique data identifiers, enforced by the database, which refuse records violating it. Represented as a data type with an underline

Signup and view all the flashcards

What is a database-generated sequence number?

Sequence numbers generated by the database.

Signup and view all the flashcards

What are Universally Unique Identifiers (UUIDs)?

Very large numbers, generated randomly, to the point where the likelihood of duplication is assumed to be zero.

Signup and view all the flashcards

What is a Relationship Attribute?

A quantity attribute added to a relationship to specify that a customer can buy multiples of the same item.

Signup and view all the flashcards

How are table definitions written?

Represented as TABLENAME (attribute1, attribute2, attribute...), with the primary key underlined.

Signup and view all the flashcards

Do you use singular or plural for table names?

Use the singular name, not the plural, to denote a table name.

Signup and view all the flashcards

Foreign Key

An attribute in a relational database table that links to another table, representing a relationship based on shared data.

Signup and view all the flashcards

How are foreign keys used in one-to-many relations?

The M entity gets a new attribute called a foreign key. This new attribute will contain the other entity’s primary key.

Signup and view all the flashcards

What happens to many-to-many relations?

A new table (called a junction table). This table will contain the primary keys of both tables.

Signup and view all the flashcards

What is a composite primary key?

A primary key with multiple columns.

Signup and view all the flashcards

List 3 scenarios where 1-to-1 relationships can be used

Table sharing confidential data, adding attributes to a table

Signup and view all the flashcards

What is Structured Query Language (SQL)?

A popular programming language for creating tables within a database.

Signup and view all the flashcards

What does the CREATE TABLE syntax allow someone to define?

Column names, data types, constraints, and indexes

Signup and view all the flashcards

What does CREATE TABLE do?

This statement creates a new table in a database and allows you to define the table’s structure, including the column names, data types, constraints and indexes.

Signup and view all the flashcards

What is important when creating foreign keys?

Ensure that this data type matches between tables.

Signup and view all the flashcards

What is the command for deleting a table?

DROP TABLE MyCoolTable;

Signup and view all the flashcards

What is the syntax for the INSERT INTO command?

INSERT INTO MyTable (MyFirstCol, MySecondCol) VALUES (value1, value2);

Signup and view all the flashcards

What is the SQL command for extracting data?

SELECT * FROM TableName;

Signup and view all the flashcards

What does SELECT * FROM TableName do?

Selects all data from a table.

Signup and view all the flashcards

How do you select certain columns of the table?

SELECT FirstName, Address FROM Customer;

Signup and view all the flashcards

What does the WHERE keyword do?

Filters out rows based on a condition.

Signup and view all the flashcards

What does the LIKE Operator do?

Checks if text matches a specific pattern using wildcards % (zero or more characters) and _ (exactly one character).

Signup and view all the flashcards

What does the IN operator do?

Checks if a value is in a list of possible values.

Signup and view all the flashcards

What does the ORDER BY keyword do?

Displays results in a particular order, ascending (ASC) or descending (DESC).

Signup and view all the flashcards

What does the AS (alias) keyword do?

Allows renaming a column.

Signup and view all the flashcards

What does LIMIT clause do?

Restricts the number of rows a SELECT statement returns.

Signup and view all the flashcards

What does OFFSET clause to?

Skips rows before returning data.

Signup and view all the flashcards

What are Aggregate Functions?

Functions that work over multiple rows and combine them, reducing a table to a single row.

Signup and view all the flashcards

List some Aggregate Functions

COUNT(), SUM(), AVG(), MIN(), MAX()

Signup and view all the flashcards

Why use the GROUP BY keyword?

Used to determine the average price per brand.

Signup and view all the flashcards

What is the HAVING clause?

Is similar to WHERE, except that it occurs AFTER the aggregates have finished.

Signup and view all the flashcards

What is a VIEW?

A saved query that works like a regular table, but cannot be edited.

Signup and view all the flashcards

What is a JOIN in SQL?

Connects tables

Signup and view all the flashcards

List 3 JOIN types.

INNER, LEFT, RIGHT

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.

Quiz Team

More Like This

Database Management Systems (DBMS)
10 questions
Database Management Systems (DBMS)
20 questions
Introduction to Database Management Systems (DBMS)
39 questions
Use Quizgecko on...
Browser
Browser