Home / Notes / Class 12 / Computer Science / Databases and SQL
Database and DBMS concepts, the relational model, keys, normalisation, entity relationship ideas and the main SQL commands for defining, changing and querying data.
A database is an organised collection of related data stored so that it can be retrieved and managed easily. A database management system (DBMS), such as MySQL, Oracle or Microsoft Access, is the software that creates, stores, protects and gives access to the data. It reduces redundancy, keeps data consistent, shares data between users and enforces security.
In the relational model data is stored in tables, called relations. A row is a record, or tuple, and a column is a field, or attribute. Each field has a data type, such as integer, text or date. Each table describes one kind of entity, such as students or courses.
Keys identify and link records:
The relationships between tables can be one to one, one to many or many to many. A many to many relationship, such as students and courses, is handled by a third table, called a junction or linking table, that holds the keys of both.
Redundancy is the unnecessary repeating of data. It wastes space and causes anomalies: an insertion anomaly (data cannot be added without other data), an update anomaly (the same fact must be changed in many places) and a deletion anomaly (deleting a record loses other facts).
Normalisation removes redundancy by splitting tables:
SQL (Structured Query Language) is the standard language of relational databases. Its statements fall into groups: DDL (data definition language: CREATE, ALTER, DROP), DML (data manipulation language: INSERT, UPDATE, DELETE), DQL (query: SELECT) and DCL (control: GRANT, REVOKE).
The most important statements:
In a SELECT statement, WHERE filters rows, ORDER BY sorts them (ASC is the default, DESC is the reverse), and GROUP BY groups rows so that aggregate functions can be used on each group. The aggregate functions are COUNT, SUM, AVG, MIN and MAX. HAVING filters groups after GROUP BY, while WHERE filters rows before grouping.
The operators used in WHERE include =, <>, >, <, >=, <=, AND, OR, NOT, BETWEEN, IN and LIKE. In a LIKE pattern, % stands for any number of characters and _ stands for exactly one character. A JOIN combines rows from two tables using a condition on the matching keys, for example SELECT s.name, c.title FROM student s JOIN course c ON s.course_id = c.id.
Study tip: To write an SQL query, say it in English first: which columns, from which table, which rows, in what order. Then write SELECT, FROM, WHERE and ORDER BY in that order.
Say the meaning first, then open the card to check.
An organised collection of related data.
Software that creates and manages databases and gives access to the data.
A field that uniquely identifies each record in a table.
A field that holds the primary key of another table and links the two.
Splitting tables to remove redundancy and avoid anomalies.
A fault such as an insertion, update or deletion problem that results from redundancy.
An SQL operation that combines rows from two tables on matching key values.
A function such as COUNT or SUM that returns one value for a group of rows.
These are the kind of questions asked for 2, 3 and 5 marks. Try each one before opening the answer.
A primary key uniquely identifies each record in its own table and cannot be null or repeated. A foreign key is a field in another table that refers to the primary key and is used to link the two tables.
It reduces redundancy and keeps data consistent, and it allows many users to share the same data with security controls.
SELECT name FROM student WHERE marks > 60 ORDER BY marks DESC;
WHERE filters individual rows before grouping, and HAVING filters groups after GROUP BY, usually using an aggregate function.
To remove redundancy and avoid insertion, update and deletion anomalies, by dividing a large table into smaller related tables.
Answer: 41666.67 and 50000
Answer: SELECT city, COUNT(*) FROM student GROUP BY city;
Answer: SELECT name FROM student WHERE name LIKE 'A%';