{"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

PRIMARY KEY Constraint:<\/strong><\/h2>\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\n

UNIQUE Constraint:<\/strong><\/h2>\n\n\n\n

The 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\n

NOT NULL Constraint:<\/strong><\/h2>\n\n\n\n

The 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\n

DEFAULT Constraint:<\/strong><\/h2>\n\n\n\n

The 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\n

FOREIGN KEY Constraint:<\/strong><\/h2>\n\n\n\n

The 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\n

MySQL CHECK Constraint<\/strong><\/h2>\n\n\n\n

MySQL 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\n

CREATE 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\n

In 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\n

These 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":"\nMySQL Constraints Guide<\/title>\n<meta name=\"description\" content=\"In MySQL, constraints are used to define rules that enforce data integrity within a table preventing the insertion of inconsistent data\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.tutorials\/topic\/mysql-constraints\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Constraints Guide\" \/>\n<meta property=\"og:description\" content=\"In MySQL, constraints are used to define rules that enforce data integrity within a table preventing the insertion of inconsistent data\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.tutorials\/topic\/mysql-constraints\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com Tutorials and Courses\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-22T12:23:59+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.tutorials\/wp-content\/uploads\/2022\/03\/tutorial-mysql.png\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"360\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.tutorials\/topic\/mysql-constraints\/\",\"url\":\"http:\/\/local.tutorials\/topic\/mysql-constraints\/\",\"name\":\"MySQL Constraints Guide\",\"isPartOf\":{\"@id\":\"http:\/\/local.tutorials\/#website\"},\"datePublished\":\"2023-07-22T12:18:58+00:00\",\"dateModified\":\"2023-07-22T12:23:59+00:00\",\"description\":\"In MySQL, constraints are used to define rules that enforce data integrity within a table preventing the insertion of inconsistent data\",\"breadcrumb\":{\"@id\":\"http:\/\/local.tutorials\/topic\/mysql-constraints\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.tutorials\/topic\/mysql-constraints\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.tutorials\/topic\/mysql-constraints\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Topics\",\"item\":\"http:\/\/local.tutorials\/topic\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL\u00a0– Constraints\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.tutorials\/#website\",\"url\":\"http:\/\/local.tutorials\/\",\"name\":\"Brightwhiz.com Tutorials and Courses\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/local.tutorials\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.tutorials\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.tutorials\/#organization\",\"name\":\"Brightwhiz.com\",\"url\":\"http:\/\/local.tutorials\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.tutorials\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/staging.brightwhiz.com\/wp-content\/uploads\/2022\/03\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"https:\/\/staging.brightwhiz.com\/wp-content\/uploads\/2022\/03\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz.com\"},\"image\":{\"@id\":\"http:\/\/local.tutorials\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\",\"https:\/\/twitter.com\/brightwhizmag\",\"https:\/\/www.instagram.com\/glamorglaze\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/c\/Sobbayi\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL Constraints Guide","description":"In MySQL, constraints are used to define rules that enforce data integrity within a table preventing the insertion of inconsistent data","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.tutorials\/topic\/mysql-constraints\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Constraints Guide","og_description":"In MySQL, constraints are used to define rules that enforce data integrity within a table preventing the insertion of inconsistent data","og_url":"http:\/\/local.tutorials\/topic\/mysql-constraints\/","og_site_name":"Brightwhiz.com Tutorials and Courses","article_publisher":"https:\/\/www.facebook.com\/brightwhiz","article_modified_time":"2023-07-22T12:23:59+00:00","og_image":[{"width":640,"height":360,"url":"http:\/\/local.tutorials\/wp-content\/uploads\/2022\/03\/tutorial-mysql.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@brightwhizmag","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/local.tutorials\/topic\/mysql-constraints\/","url":"http:\/\/local.tutorials\/topic\/mysql-constraints\/","name":"MySQL Constraints Guide","isPartOf":{"@id":"http:\/\/local.tutorials\/#website"},"datePublished":"2023-07-22T12:18:58+00:00","dateModified":"2023-07-22T12:23:59+00:00","description":"In MySQL, constraints are used to define rules that enforce data integrity within a table preventing the insertion of inconsistent data","breadcrumb":{"@id":"http:\/\/local.tutorials\/topic\/mysql-constraints\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.tutorials\/topic\/mysql-constraints\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/local.tutorials\/topic\/mysql-constraints\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.tutorials\/"},{"@type":"ListItem","position":2,"name":"Topics","item":"http:\/\/local.tutorials\/topic\/"},{"@type":"ListItem","position":3,"name":"MySQL\u00a0– Constraints"}]},{"@type":"WebSite","@id":"http:\/\/local.tutorials\/#website","url":"http:\/\/local.tutorials\/","name":"Brightwhiz.com Tutorials and Courses","description":"","publisher":{"@id":"http:\/\/local.tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.tutorials\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.tutorials\/#organization","name":"Brightwhiz.com","url":"http:\/\/local.tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/staging.brightwhiz.com\/wp-content\/uploads\/2022\/03\/brightwhiz-com-logo-orange.png","contentUrl":"https:\/\/staging.brightwhiz.com\/wp-content\/uploads\/2022\/03\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz.com"},"image":{"@id":"http:\/\/local.tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz","https:\/\/twitter.com\/brightwhizmag","https:\/\/www.instagram.com\/glamorglaze\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/c\/Sobbayi"]}]}},"_links":{"self":[{"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/topic\/131"}],"collection":[{"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/types\/topic"}],"replies":[{"embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/comments?post=131"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/media\/45"}],"wp:attachment":[{"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/tags?post=131"},{"taxonomy":"subject","embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/subject?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}