Database Schema vs Instance, Data Independence, Keys, Integrity Constraints and Relational Algebra Explained
1. Schema vs Instance
- Schema is the logical structure of a database.
- It defines the design of the database.
- It specifies tables and their attributes.
- It defines relationships between tables.
- It also defines database constraints.
- Schema is created during database design.
- Instance is the actual data stored in the database at a particular time.
- It represents the current state of the database.
- It changes when data is inserted.
- It changes when data is updated or deleted.
- An instance can be different at different times.
- Example: Student(Rollno, Name, Marks) is a schema, while (101, Amit, 80) is an instance.
Remember: Schema = Structure | Instance = Actual Data
2. Physical vs Logical Data Independence
- Data independence means changing one database level without affecting another level.
- Physical data independence deals with the physical storage level.
- It allows changes in the internal storage structure.
- It does not affect the logical database structure.
- Examples include changing file organization.
- Changing indexes or storage devices is also an example.
- Logical data independence deals with the conceptual/logical level.
- It allows changes in the logical structure of the database.
- Such changes should not affect external views.
- It should not require changes in application programs.
- Example: Adding a new attribute to a table.
- Logical data independence is generally more difficult to achieve than physical data independence.
Remember: Physical = Storage | Logical = Structure
3. Degree vs Cardinality
- Degree is the number of attributes in a relation.
- Attributes are represented as columns.
- Degree tells us how many columns a table contains.
- It is also called the arity of a relation.
- Example: STUDENT(RollNo, Name, Branch, Marks).
- The degree of this relation is 4.
- Cardinality is the number of tuples in a relation.
- Tuples are represented as rows.
- Cardinality tells us how many records a table contains.
- It can change when records are inserted or deleted.
- If a table has 50 records, its cardinality is 50.
- Therefore, degree represents columns and cardinality represents rows.
Remember: Degree = Columns | Cardinality = Rows
4. Super Key vs Candidate Key vs Primary Key vs Foreign Key
- A super key is a set of one or more attributes that uniquely identifies a record.
- A super key may contain extra or unnecessary attributes.
- A candidate key is a minimal super key.
- A candidate key uniquely identifies a record without unnecessary attributes.
- A table can have more than one candidate key.
- A primary key is one candidate key selected to identify records.
- A primary key must contain unique values.
- A primary key cannot contain NULL values.
- A foreign key is an attribute that refers to a primary key in another table.
- A foreign key establishes a relationship between two tables.
- Foreign key values can generally repeat.
- Example: In STUDENT(RollNo, Name, DeptID), RollNo can be the primary key and DeptID can be a foreign key.
Remember: Super Key → Candidate Key → Primary Key | Foreign Key → Connects Table
5. Entity Integrity vs Referential Integrity
- Entity integrity is a rule applied to the primary key.
- It ensures that every record can be uniquely identified.
- A primary key must contain unique values.
- A primary key cannot contain NULL values.
- It prevents duplicate identification of records.
- Example: Student Roll Number cannot be NULL.
- Referential integrity maintains consistency between related tables.
- It is mainly applied to foreign keys.
- A foreign key should refer to a valid primary key value.
- It prevents invalid references between tables.
- Example: A student’s DeptID should exist in the Department table.
- Thus, entity integrity ensures valid records, while referential integrity ensures valid relationships.
Remember: Entity → Primary Key | Referential → Foreign Key
6. σ vs π: Selection vs Projection
- Selection is a relational algebra operation.
- Its symbol is σ (Sigma).
- It is used to select specific rows from a relation.
- It selects rows based on a given condition.
- It does not normally remove columns.
- Example: σ Marks > 70 (STUDENT) selects students having marks greater than 70.
- Projection is also a relational algebra operation.
- Its symbol is π (Pi).
- It is used to select specific columns from a relation.
- It removes unwanted attributes from the result.
- Duplicate rows in the projected result are eliminated in relational algebra.
- Example: π Name, Marks (STUDENT) selects only Name and Marks.
Remember: σ = Rows | π = Columns
7. JOIN vs DIVISION
- JOIN is a relational algebra operation.
- It combines data from two or more relations.
- It is used when relations have related attributes.
- JOIN normally uses a matching condition.
- It produces a relation containing combined information.
- Example: STUDENT and DEPARTMENT can be joined using DeptID.
- Division is another relational algebra operation.
- It is used to find records related to all records of another relation.
- It is commonly used for queries containing the word “all”.
- It requires two relations for the operation.
- Example: Finding students who have completed all required courses.
- Therefore, JOIN combines related tables, while DIVISION finds records satisfying an “all” condition.
Remember: JOIN = Combine | DIVISION = ALL
