Câu hỏi phỏng vấn MySQL
Câu hỏi

What are key constraints. What different types of constraints are there in MySQL?

Câu trả lời

In MySQL, constraints are rules applied to table columns to ensure the validity and integrity of the data within the database. They are used to enforce certain types of data manipulation rules that the data must comply with, thus ensuring the accuracy and reliability of the data in the database. MySQL supports several types of constraints, each serving a specific purpose in data integrity enforcement.

Key Constraints

Key constraints are a subset of MySQL constraints that specifically deal with the relationships between tables and the uniqueness of data. There are two main types of key constraints in MySQL:

  1. PRIMARY KEY: A PRIMARY KEY constraint uniquely identifies each record in a table. This constraint does not allow NULL values and ensures that each value in the primary key column is unique. In essence, it combines a NOT NULL constraint with a UNIQUE constraint. A table can have only one primary key, which can consist of single or multiple columns (fields)[1][2][3].

  2. FOREIGN KEY: A FOREIGN KEY constraint is used to link two tables together. It is a field (or collection of fields) in one table that uniquely identifies a row of another table. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table. FOREIGN KEY constraints ensure referential integrity by allowing only values that appear in the corresponding primary key of the referenced table, thus preventing orphan records and ensuring the consistency of data across tables[1][2][8].

Other Types of Constraints in MySQL

In addition to key constraints, MySQL supports several other types of constraints:

  1. NOT NULL: This constraint ensures that a column cannot hold a NULL value. It enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a ...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What happens if a parent row which is referenced by child row is being deleted in case of foreign key constraint?

junior

How are VARCHAR and CHAR different. Talk about cases where you will use one over other.

middle

What is Stored Function in MySQL. How are they different from Stored Procedure?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào