Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1MySQL Constraints
Introduction
Constraints in MySQL are used to enforce rules and restrictions on the data stored in tables. They help maintain data integrity and ensure that the data conforms to specific criteria. In this article, we will explore the commonly used constraints in MySQL with examples to illustrate their functionality.
NOT NULL Constraint
The NOT NULL constraint ensures that a column does not contain any NULL (empty) values. Here's an example of creating a table with a NOT NULL constraint:
CREATE TABLE employees (
id INT,
name VARCHAR(50) NOT NULL,
age INT
);
UNIQUE Constraint
The UNIQUE constraint ensures that the values in a column or a group of columns are unique. Here's an example of creating a table with a UNIQUE constraint:
CREATE TABLE customers (
id INT,
email VARCHAR(50) UNIQUE,
phone VARCHAR(20)
);
PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a table. It is a combination of the NOT NULL and UNIQUE constraints. Here's an example of creating a table with a primary key:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
FOREIGN KEY Constraint
The FOREIGN KEY constraint establishes a relationship between two tables based on a column in each table. It ensures that values in the referencing column exist in the referenced column of another table. Here's an example of creating a table with a foreign key:
CREATE TABLE orders (
id INT,
customer_id INT,
amount DECIMAL,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
CHECK Constraint
The CHECK constraint is used to enforce a condition on a column's values. It allows you to define a custom condition that must be satisfied for each row. Here's an example of creating a table with a CHECK constraint:
CREATE TABLE employees (
id INT,
age INT,
salary DECIMAL CHECK (salary > 0)
);
Conclusion
Constraints in MySQL play a vital role in maintaining data integrity and enforcing rules on the data stored in tables. In this article, we explored the NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK constraints in MySQL. By utilizing these constraints, you can ensure that the data in your MySQL databases meets specific criteria and remains consistent, helping to prevent data corruption and inconsistencies.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners