Database SQL Flashcards
What is a database?
~a structured collection of records or data which are logically related
What is a Database Management System?
- is computer software designed for the purpose of managing databases
- typical examples of DBMSs include Oracle, Ms SQL Server and MySQL
What is a Relational Database Management System (RDBMS)?
~is a database management system (DBMS) that is based on the relational model
What is the standard language for interacting with an RDBMS?
Structured Query Language (SQL)
Give some background on SQL Server. (4)
- owned by Microsoft Corp
- first version released in 1988
- Microsoft’s philosophy of combining a high performance database with an easy-to-use interface proved to be very successful
- one of the most popular high-end relational database softwares
List the components of a relational database? (6)
Table
Row
Column
Field
Primary Key
Foreign Key
What is a table with regards to a relational database? (3)
- a basic storage structure of an RDBMS and consists of columns and rows
- represents an entity
- e.g. the employee table stores information about the employees of an organization
What is a row with regards to a relational database? (3)
- a combination of column values in a table
- identified by a primary key -also known as records
What is a column with regards to a relational database?
- a collection of one type of data in a table
- represent the attributes of an object
What is a field with regards to a relational database? (3)
- an intersection of a row and a column
- contains one data value
- there is no data in the field, the field is said to contain a NULL value
What is a primary key? (3)
- a column or a combination of columns that is used to uniquely identify each row (occurrence) in a table
- must contain a value
- cannot contain a NULL value
What is a foreign key?
- a column or set of columns that refers to a primary key in the same table or another table
- must either match a primary key or else be NULL
Give the properties of a relational database. (5)
- column values are of the same kind
- each row is unique
- the sequence of columns is insignificant
- the sequence of rows is insignificant
- each column must have a unique name
What is an ERD?
- Entity Relationship Diagram
- a design tool that graphically represents the logical relationships of entities (or objects) in order to create a database
What are the properties of an ERD?
- DBMS independent
- has many variants
- is composed of entities, attributes, and relationships
What is an entity of an ERD? (4)
- any object in the system that we want to model and store information about
- individual objects are called entities
- will become a Table in the database
- represented by rectangles (either with round or square corners)
What are ERD attributes? (3)
- property of an entity
- all data relating to an entity is held in its attributes
- appear inside ovals and are attached to their entity
How are primary keys identified in an ERD?
They are underlined.
What is a composite key?
~a key that consists of two or more attributes
What is a foreign key in an ERD and how is it represented?
- is an attribute (or group of attributes) that is the primary key to another relation
- usually shown in italics or with a wiggly underline
What is a relationship in an ERD and how is it shown?
- an association of entities
- represented on the ER diagram by a series of lines
What is cardinality?
Indicates the maximum number of relationships between the entities, therefore “many” can be explained as “one or many” or sometimes “zero, one, or many”.
What types of relationships exists in an ERD?
1 : 1 one to one relationship
1 : M one to many relationship
M : M many to many relationship
What are the steps to creating an ERD? (10)
- Identify Entities
- Find Relationships
- Draw Rough ERD
- Fill in Cardinality
- Define Primary Keys
- Draw Key-Based ERD
- Identify Attributes
- Map Attributes
- Draw fully attributed ERD
- Check Results
When do you split a relationship in an ERD? (3)
- where the m:n relationship hides an entity
- a many to many relationship in an ER model can be replaced using an intermediate entity
- the resulting ER diagram is easier to understand
What is ordinality / optionality in an ERD? (3)
- indicate whether the relationship is mandatory or optional
- can be different at each end of the relationship
- to show optionality, put a circle or
0' at the
optional end’ of the relationship
When is a relationship considered mandatory?
If the relationship is one or more.
When is a relationship optional?
If the relationship is zero or more.
List the 4 main function of Data Manipulation Language and explain what they are there for?
SELECT – to retrieve records from a table
INSERT – to add a record to a table
UPDATE – to modify the data held in a table record
DELETE – to remove a record from a table
What is the basic syntax of the SELECT function?
SELECT *
FROM tablename;
What is the basic syntax of the INSERT function?
INSERT INTO tablename
(field1, field2, … fieldn)
VALUES (value1, value2, …valuen);
Note: Text values go into single quotes; numbers have no quotes!
Give the basic syntax of the UPDATE function,
UPDATE tablename
SET field1 = value1,
field2 = value2, …
fieldn = valuen
WHERE criteria;
Note: WHERE is optional, but if omitted all data are replaced with given values!
Give the basic syntax of the DELETE function.
DELETE
FROM tablename
WHERE criteria
Note: WHERE is optional, but if not given, all records are deleted.
What operators are allowed in SQL?
– Comparison Operators
– Logical Operators
– Arithmetic Operators
– Operators used to negate conditions
Which comparison operators can be used in SQL?
= equals to
<> not equals to
> greater than
< less than
>= greater than or equal to
<= smaller than or equal to
Code: Select all products with a price greater than $100.
SELECT *
FROM Products
WHERE price > 100;
Which logical operators exist in SQL? (6)
- AND, OR, NOT
- LIKE
- BETWEEN
- IN
- IS NULL
- ALL and ANY
Code: Select all products where the price is greater than $250 and the stock quantity is greater than 5.
SELECT *
FROM Products
WHERE Price > 250 AND StockQty > 5;
How is the LIKE operator used? (3)
~used to search a character pattern without knowing the exact character value
- uses wildcard operators to compare a value to similar values
- wild card operators can be used in combinations
What wildcard operators exist and what are they used for?
% percent -> can replace zero, one or multiple characters _ underscore -> represents single numbers or characters
Code: Return all records where the surname has a “ean” in it.
SELECT *
FROM Clients
WHERE Surname LIKE ‘%ath%’
Code: Retrieve all records that have 6 digits starting with a 1 and ending with an 8.
SELECT *
FROM Clients
WHERE ClientID LIKE ‘1____8’
Code: Retrieve all records that have a 7 in 2nd position and end with a 0.
SELECT *
FROM Clients
WHERE ClientID LIKE ‘_7%0’
Why and how is the BETWEEN operator used? (5)
- used to search for values whose minimum and maximum values are given
- max and min values are included as a part of the search
- can be used with both character and numeric data types -cannot mix the data types though
- ideal for date range comparisons
Code: Retrieve all sales records with invoice dates between 01/07/2010 and 31/12/2010.
SELECT *
FROM Sales
WHERE InvoiceDate BETWEEN ‘01 Jul 2010’ AND ‘31 Dec 2010’
Note: Always use the following date format, dd mmm yyyy!