{"id":131,"date":"2023-07-22T12:18:58","date_gmt":"2023-07-22T12:18:58","guid":{"rendered":"http:\/\/local.tutorials\/?post_type=topic&p=131"},"modified":"2023-07-22T12:23:59","modified_gmt":"2023-07-22T12:23:59","slug":"mysql-constraints","status":"publish","type":"topic","link":"http:\/\/local.tutorials\/topic\/mysql-constraints\/","title":{"rendered":"MySQL\u00a0– Constraints"},"content":{"rendered":"\n
In MySQL, constraints are used to define rules that enforce data integrity within a table. They help ensure that the data stored in the table meets specific criteria and prevents the insertion of invalid or inconsistent data. Here are some examples of commonly used constraints in MySQL:<\/p>\n\n\n\n
The PRIMARY KEY constraint is used to define a column or a combination of columns that uniquely identify each row in the table. It enforces the uniqueness of the values and automatically creates an index for faster data retrieval. Example:<\/p>\n\n\n\n
CREATE TABLE students (\n student_id INT PRIMARY KEY,\n first_name VARCHAR(50),\n last_name VARCHAR(50),\n age INT\n);<\/code><\/pre>\n\n\n\nUNIQUE Constraint:<\/strong><\/h2>\n\n\n\nThe UNIQUE constraint ensures that the values in the specified column or combination of columns are unique and do not have any duplicates. Example:<\/p>\n\n\n\n
CREATE TABLE employees (\n employee_id INT PRIMARY KEY,\n email VARCHAR(100) UNIQUE,\n department VARCHAR(50)\n);<\/code><\/pre>\n\n\n\nNOT NULL Constraint:<\/strong><\/h2>\n\n\n\nThe NOT NULL constraint ensures that a column does not accept NULL values, meaning it must have a value during data insertion. Example:<\/p>\n\n\n\n
CREATE TABLE orders (\n order_id INT PRIMARY KEY,\n order_date DATE NOT NULL,\n customer_id INT,\n total_amount DECIMAL(10, 2)\n);<\/code><\/pre>\n\n\n\nDEFAULT Constraint:<\/strong><\/h2>\n\n\n\nThe DEFAULT constraint sets a default value for a column when no value is explicitly provided during data insertion. Example:<\/p>\n\n\n\n
CREATE TABLE products (\n product_id INT PRIMARY KEY,\n product_name VARCHAR(100) NOT NULL,\n quantity INT DEFAULT 0\n);<\/code><\/pre>\n\n\n\nFOREIGN KEY Constraint:<\/strong><\/h2>\n\n\n\nThe FOREIGN KEY constraint establishes a relationship between two tables based on a column. It ensures that the values in the specified column exist as primary key values in another table. Example:<\/p>\n\n\n\n
CREATE TABLE orders (\n order_id INT PRIMARY KEY,\n order_date DATE,\n customer_id INT,\n total_amount DECIMAL(10, 2),\n FOREIGN KEY (customer_id) REFERENCES customers(customer_id)\n);<\/code><\/pre>\n\n\n\nMySQL CHECK Constraint<\/strong><\/h2>\n\n\n\nMySQL supports the CHECK<\/code> constraint starting from version 8.0.23. The CHECK<\/code> constraint is used to enforce a condition on a column or a set of columns, ensuring that only valid data is inserted into the table.<\/p>\n\n\n\nCREATE TABLE products (\n product_id INT PRIMARY KEY,\n product_name VARCHAR(100) NOT NULL,\n price DECIMAL(10, 2) NOT NULL,\n quantity INT NOT NULL,\n CHECK (price >= 0 AND quantity >= 0)\n);<\/code><\/pre>\n\n\n\nIn this example, the customer_id<\/code> column in the “orders” table references the customer_id<\/code> column in the “customers” table, creating a relationship between the two.<\/p>\n\n\n\nThese are some of the commonly used constraints in MySQL. By using constraints effectively, you can maintain data integrity and improve the overall quality of your database.<\/p>\n","protected":false},"excerpt":{"rendered":"
In MySQL, constraints are used to define rules that enforce data integrity within a table. They help ensure that the data stored in the table meets specific criteria and prevents the insertion of invalid or inconsistent data. Here are some examples of commonly used constraints in MySQL: PRIMARY KEY Constraint: The PRIMARY KEY constraint is […]<\/p>\n","protected":false},"featured_media":45,"comment_status":"open","ping_status":"closed","template":"","meta":[],"categories":[3],"tags":[],"subject":[16,17],"yoast_head":"\n
MySQL Constraints Guide<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\t\n