Tag: Primary Key


  • A foreign key constraint enforces referential integrity by linking a column in one table to the primary key of another table. Explanation: Example SQL Foreign Key Constraint: sqlCopyEditCREATE TABLE Employees ( emp_id INT PRIMARY KEY, dept_id INT, FOREIGN KEY (dept_id) REFERENCES Departments(dept_id) ON DELETE SET NULL ); Here, if a department is deleted, dept_id in…

  • Data redundancy occurs when the same piece of data is stored multiple times in a database. Explanation:

  • Referential integrity ensures that relationships between tables remain consistent by enforcing foreign key constraints. Explanation: Example SQL Enforcing Referential Integrity: sqlCopyEditCREATE TABLE Orders ( order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES Customers(customer_id) ON DELETE CASCADE ); Here, if a customer is deleted, all their orders are automatically deleted (ON DELETE CASCADE).

  • A composite key is a primary key that consists of two or more attributes that uniquely identify a record. Explanation: Example SQL Composite Key: sqlCopyEditCREATE TABLE Enrollment ( student_id INT, course_id INT, enrollment_date DATE, PRIMARY KEY (student_id, course_id) ); Here, student_id and course_id together form a composite key.

  • A candidate key is an attribute (or set of attributes) that can uniquely identify each tuple in a table. Explanation: Example: For a Students table: student_id email name 101 alice@email.com Alice Here, student_id and email are candidate keys, as both uniquely identify a student.

  • A tuple is a single record (row) in a relational database table. Explanation: Example: For the Employees table: emp_id name dept_id 1 Alice 10 The row (1, Alice, 10) is a tuple.

  • A schema is the logical structure of a database, defining tables, columns, relationships, constraints, and indexes. Explanation: Example Schema Definition in SQL: sqlCopyEditCREATE SCHEMA School; CREATE TABLE School.Students ( student_id INT PRIMARY KEY, name VARCHAR(50), age INT ); Here, we created a schema named School and a table Students within it.

  • A subquery (also known as a nested query) is an SQL query that is embedded inside another SQL query. Explanation: Example: Find employees who earn more than the average salary in the company. sqlCopyEditSELECT name, salary FROM Employees WHERE salary > (SELECT AVG(salary) FROM Employees); Here, the subquery (SELECT AVG(salary) FROM Employees) calculates the average…

  • An OUTER JOIN retrieves all records from one or both tables, even if there are no matches, by filling missing values with NULLs. Explanation: There are three types of outer joins: Example: SQL Query for LEFT OUTER JOIN: sqlCopyEditSELECT Employees.name, Departments.department_name FROM Employees LEFT OUTER JOIN Departments ON Employees.dept_id = Departments.dept_id; Output: name department_name Alice…

  • An INNER JOIN in SQL returns only the rows where there is a match in both tables based on a specified condition. Explanation: Example: Consider two tables:Employees Table emp_id name dept_id 1 Alice 10 2 Bob 20 3 Charlie 30 Departments Table dept_id department_name 10 HR 20 IT SQL Query for INNER JOIN: sqlCopyEditSELECT Employees.name,…