Databases Flashcards

1
Q

What is an entity in entity-relationship modelling?

A

An entity is a category of object, person, event or thing of interest about which data is to be recorded.
Examples: Employee, Film, Actor, Product, Recipe, Ingredient
Each entity in a database system has attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What entities might there be for a database used to store data about dentist appointments at a particular dentist surgery?

A

E.g. Dentist, Patient, Appointment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What attributes might the entity “Dentist” have in a database used to store data about dentist appointments at a particular dentist surgery?

A

E.g. DentistID, Title, FirstName, Surname, Qualification

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What attributes might the entity “Patient” have in a database used to store data about dentist appointments at a particular dentist surgery?

A

E.g. PatientID, Title, FirstName, Surname, EmailAddress, TelephoneNumber

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What attributes might the entity “Appointment” have in a database used to store data about dentist appointments at a particular dentist surgery?

A

E.g. StartTime, EndTime, PatientID, DentistID, Cost, Procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are entity descriptions normally written?

A

An entity description is typically written using the format
Entity1 (Attribute1, Attribute2)
E.g.
Dentist(Title, Firstname, Surname, Qualification)
Patient(Title, Firstname, Surname, Address, Telephone)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write an entity description for the entity Pet with the attributes Name, Species, Age, and Owner.

A

Pet(Name, Species, Age, Owner)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write an entity description for the entity Student with the attributes FirstName, Surname, Age, YearGroup, and EmailAddress.

A

Student(FirstName, Surname, Age, YearGroup, EmailAddress)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an entity identifier?

A

Each entity needs to have an entity identifier which uniquely identifies the entity.
In a relational database, the entity identifier is known as the primary key.
An ID number can be used as a primary key for Dentist and Patient:
Dentist(DentistID, Title, Firstname, Surname, Qualification)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the three types of relationships that can exist between entities?

A

One-to-one
One-to-many
Many-to-many

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give an example of a one-to-one relationship.

A

Husband and Wife (in the UK)
Country and Prime Minister

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Give an example of a one-to-many relationship.

A

Customer and order (a customer can have multiple orders)
Borrower and library book (a borrower can borrow multiple books)
School and pupil (a school can have multiple pupils)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give an example of a many-to-many relationship.

A

Student and course
Student and teacher
Stock item and supplier
Film and actor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an entity-relationship diagram?

A

A diagrammatic way of representing the relationships between the entities in a database.
To show the relationship between two entities, both the degree and the name of the relationship need to be specified.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do relational databases work?

A

In a relational database, a separate table is created for each entity identified in the system.
Where a relationship exists between two entities, an extra field called a foreign key links the tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s a foreign key?

A

A foreign key is an attribute that creates a joint between two tables. It is the attribute that is common to both tables.
The primary key in one table is the foreign key in the table to which it is linked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you link tables that have a many-to-many relationship with each other?

A

When there is a many-to-many relationship between two entities, tables cannot be directly linked.
E.g. the relationship between Student and Course. A student takes many courses, and the same course is taken by many students.
In this case, an extra table is needed to link the two entities.
For this example, this table could be called Enrolment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a composite primary key?

A

Sometimes 2 or even more attributes are needed to uniquely define a record.
For example, in a customer order consisting of many different order lines, each order line may be uniquely identified by the two attributes orderNumber and orderLine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How are primary keys shown in an entity-relationship diagram?

A

The name of the attribute which is the primary key is underlined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How are foreign keys shown in an entity relationship diagram?

A

The name of the attribute which is the foreign key is in italics.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is normalisation?

A

Normalisation is a process used to come up with the best possible design for a database.
Tables should be organised so that data is not duplicated in the same table or in different tables.
The structure should allow complex queries to be made.
There are 3 stages in normalisation, called First Normal Form (1NF), Second Normal Form (2NF), and Third Normal Form (3NF).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is First Normal Form (1NF)?

A

A table is in First Normal Form if it contains no repeating attributes or groups of attributes.
All attributes must be atomic – a single attribute cannot consist of 2 data items (e.g. addresses, each part must be on a separate line to be able to search by street name, postcode etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is Second Normal Form (2NF)?

A

A table is in Second Normal Form if it is in first normal form and contains no partial dependencies.
Partial dependencies can only occur if the primary key is a composite key.

24
Q

What is Third Normal Form (3NF)?

A

A table is in Third Normal Form if it is in Second Normal Form and contains no non-key dependencies. This can be defined by saying
“All attributes are dependent on the key, the whole key, and nothing but the key”

25
Q

What are the advantages of normalisation?

A

It is easier to maintain and change a normalised database.
There is no unnecessary duplication of data.
Data integrity is maintained – if a person changes address, for example, the update needs to be made only once to a single table.
Having smaller tables with fewer fields means faster searches and savings in storage.

26
Q

What is SQL?

A

SQL stans for Structured Query Language.
It is a declarative language used for querying and updating tables in a relational database.
It can also be used to create tables.
You can create SQL statements in a programming language such as Python to access and manipulate a database.

27
Q

How do you use a SELECT…FROM…WHERE statement in SQL?

A

The select statement is used to extract fields from one or more tables, using the following syntax:
SELECT – list of fields to be displayed
FROM – list the table or tables the data will come from
WHERE – list of search criteria
ORDER BY – list the fields that the data is to be sorted based on (ASC or DESC, default is ASC)

28
Q

How do you use a wildcards (*) and LIKE statements in SQL?

A

The wildcard () can be used to select all fields – SELECT *
LIKE can be used to search for a pattern:
E.g.
SELECT *
FROM tblProduct
WHERE subject LIKE “Comp

29
Q

What are some operators you can use in the WHERE clause when selecting records from a database with SQL?

A

BETWEEN – between an inclusive range
IN – specify multiple possible values for a column
For example:
SELECT *
FROM tblProduct
WHERE price BETWEEN 5.00 and 10.00
The following can also be used in a WHERE clause:
= > < >= <= AND OR NOT

30
Q

How are semicolons used in SQL?

A

Some database systems require a semicolon at the end of each SQL statement.
A semicolon is the standard way to separate each SQL statement.
DO NOT put a semicolon at the end of each line.

31
Q

How do you extract data from multiple tables in one SELECT statement?

A

Using SQL, you can combine data from two or more tables by specifying the links between the tables.
This is specified in the WHERE clause.
E.g.
SELECT *
FROM Pets, Owners
WHERE Pets.PetID == Owners.PetID

32
Q

What if you are selecting data from multiple tables, but an attribute name occurs in more than one of the tables?

A

When you are selecting attributes from linked tables, if the attribute name occurs in more than one table, you should specify the table name.
If the attribute only occurs in only one table, specifying the table name is optional.

33
Q

How is the JOIN keyword used in SQL?

A

Data from two linked tables can be extracted using the join keyword (an alternative to WHERE clause).

34
Q

What is Data Definition Language (DDL) in SQL?

A

DDL is used to create the database, design tables, set access privileges for users, etc.

35
Q

What is Data Manipulation Language (DML) in SQL?

A

DML can be used to retrieve (select), update, insert, and delete data in a database.

36
Q

How do you insert data into a table using SQL?

A

The INSERT INTO statement is used to insert a new record into a database.
E.g.
INSERT INTO Product(ProductID, Description, Price)
VALUES(“A345”, “Pink rabbit”, 7.50)

37
Q

What is the data type CHAR(n) in SQL?

A

Character string of fixed length n.
E.g. ProductCode CHAR(6)
would mean that ProductCodes have to be 6 characters long.

38
Q

What is the data type VARCHAR(n) in SQL?

A

Character string of variable length, the maximum being n.
E.g. Surname VARCHAR(25)
would mean that Surnames can’t be longer than 25 characters.

39
Q

What is the data type BOOLEAN in SQL?

A

BOOLEAN means that the data can either be TRUE or FALSE.
e.g. ReviewComplete BOOLEAN
would mean that ReviewComplete can either be TRUE or FALSE.

40
Q

What is the data type INTEGER or INT in SQL?

A

INTEGER or INT means that the data can be any whole number (positive, zero, or negative).
e.g. Quantity INTEGER
would mean that Quantity can be any whole number.

41
Q

What is the data type FLOAT in SQL?

A

FLOAT means that the data can be any number which can be represented with a floating decimal point.
e.g. Length FLOAT(10,2) means the value of float can have a maximum of 10 digits with a maximum of 2 after the decimal point.

42
Q

What is the data type DATE in SQL?

A

DATE stores Day, Month, and Year values.
e.g. HireDate DATE
can be used to store the Day, Month, and Year that an employee was hired.

43
Q

What is the data type TIME in SQL?

A

TIME stores Hour, Minute, and Second values.
e.g. RaceTime TIME
can be used to store how long it took each person to complete a race (in hours, minutes and seconds).

44
Q

What is the data type CURRENCY in SQL?

A

CURRENCY formats numbers in the currency used in your region.
e.g. EntryFee CURRENCY
can be used to store the entry fee in the local currency.

45
Q

How do you update data using SQL?

A

The UPDATE statement is used to update a record in a table.
E.g.
UPDATE Product
SET Description = “Blue Rabbit”, Price = 8.25
WHERE ProductID = “A345”

46
Q

How do you delete a record in SQL?

A

The DELETE statement is used to delete a record in a table.
E.g.
DELETE FROM Product
WHERE ProductID = “A345”

47
Q

How do you create a new table in SQL?

A

Using the CREATE TABLE statement.
E.g.
CREATE TABLE tblProduct
(
ProductID CHAR(4) NOT NULL PRIMARY KEY,
Description VARCHAR(20) NOT NULL,
Price CURRENCY,
)

48
Q

How do you alter a table structure in SQL?

A

The ALTER TABLE statement is used to add, delete, or modify columns (fields) in an existing table.

49
Q

How do you add a new column to a table structure using the ALTER TABLE statement?

A

Using the ADD statement.
E.g.
ALTER TABLE tblProduct
ADD QtyInStock INTEGER

50
Q

How do you delete a column from a table structure using the ALTER TABLE statement?

A

Using the DROP statement.
E.g.
ALTER TABLE tblProduct
DROP QtyInStock

51
Q

How do you change the data type of a column using the ALTER TABLE statement?

A

Using the MODIFY COLUMN statement.
E.g.
ALTER TABLE tblProduct
MODIFY COLUMN Description VARCHAR(30) NOT NULL

52
Q

How do you define linked tables in SQL?

A

Using the FOREIGN KEY and REFERENCES statements.
E.g.
CREATE TABLE ProductComponent
(ProductID CHAR(4) NOT NULL
CompID CHAR(6) NOT NULL
Quantity INTEGER,
FOREIGN KEY ProductID REFERENCES Product(ProductID)
FOREIGN KEY CompID REFERENCES Component(CompID)
PRIMARY KEY (ProductID, CompID) )

53
Q

What is a client-server database?

A

A client-server database provides simultaneous access to a database for several clients.
Database Management System (DBMS) software on the server processes requests for clients, which are individual workstations.
E.g. When you make an online transfer from your bank account to a company you have bought goods from, a client program in your computer forwards the request to a server program at the bank. When your account has been retrieved from the database, the amount is deducted and the new balance displayed to you by the client program in your computer.

54
Q

What are the advantages of a client-server system?

A

Consistency of the database is maintained because only one copy is held.
The resources of a powerful computer are made available to a large number of users.
Access rights and security are managed centrally.
Backup and recovery are managed centrally.

55
Q

What is record locking used for in a client-server system?

A

Record locking prevents simultaneous access to objects in a database in order to prevent updates being lost or inconsistencies in the data arising.
With record locking, a record is locked when a user retrieves it for editing or updating.
Anyone else attempting to retrieve it is denied access until the transaction is completed or cancelled.

56
Q

What is serialisation and timestamp ordering in a client-server system?

A

Serialisation is an alternative technique to ensure that transactions do not overlap in time. It can be implemented using timestamp ordering. Every object in the database has a read timestamp and a write timestamp. These are updated every time an object is read or written.
When the user tries to save an update, if the read timestamp is not the same as it was when they started the transaction, the DBMS knows that another user has accessed the same object. The transaction will be cancelled and a message will be sent to the user.

57
Q

What is commitment ordering in a client-server system?

A

This is another serialisation technique to ensure that no transactions are lost if two clients are simultaneously trying to update a record. Transactions are ordered in terms of their dependencies on one another as well as the time they were initiated. It can be used to prevent deadlock by blocking one request until another is completed.