Constraints are rules that are applied to columns in a database table to enforce data integrity. They ensure that the data stored in the table is accurate, consistent, and reliable. Some common types of constraints include:
users
table, the username
column should not be null.products
table, the product_code
column should have unique values.orders
table, the order_id
column can be the primary key.Relationships in a database define how different tables are connected. There are three main types of relationships:
person
table and a passport
table where each person has exactly one passport.department
table and an employee
table where one department can have many employees.student
table and a course
table where a student can take multiple courses and a course can have multiple students.Here is an example of creating a table with constraints in SQL (using MySQL syntax):
-- Create a products table
CREATE TABLE products (
product_id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100) NOT NULL,
product_code VARCHAR(20) UNIQUE,
price DECIMAL(10, 2) NOT NULL
);
In this example, the product_id
column is the primary key with auto - increment functionality. The product_name
column cannot be null, and the product_code
column must have unique values.
Let’s create two tables with a one - to - many relationship and define a foreign key constraint:
-- Create a customers table
CREATE TABLE customers (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(100) NOT NULL
);
-- Create an orders table with a foreign key
CREATE TABLE orders (
order_id INT PRIMARY KEY AUTO_INCREMENT,
order_date DATE NOT NULL,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
In this example, the orders
table has a foreign key customer_id
that refers to the customer_id
(primary key) in the customers
table.
Constraints are essential for maintaining data integrity. For example, in a bank_accounts
table, the balance
column should have a CHECK constraint to ensure that the balance is not negative:
CREATE TABLE bank_accounts (
account_id INT PRIMARY KEY AUTO_INCREMENT,
account_holder VARCHAR(100) NOT NULL,
balance DECIMAL(10, 2) NOT NULL CHECK (balance >= 0)
);
When designing a database, it is important to model real - world relationships accurately. For a library management system, we can have a one - to - many relationship between a library
table and a book
table:
-- Create a library table
CREATE TABLE library (
library_id INT PRIMARY KEY AUTO_INCREMENT,
library_name VARCHAR(100) NOT NULL
);
-- Create a book table
CREATE TABLE books (
book_id INT PRIMARY KEY AUTO_INCREMENT,
book_title VARCHAR(200) NOT NULL,
library_id INT,
FOREIGN KEY (library_id) REFERENCES library(library_id)
);
fk_orders_customers
.Understanding SQL database design constraints and relationships is essential for creating a well - structured and efficient database. Constraints ensure data integrity, while relationships define how different tables are connected. By following the usage methods, common practices, and best practices outlined in this blog, you can design databases that are reliable, easy to maintain, and performant. Whether you are building a small application or a large - scale enterprise system, a solid understanding of these concepts will help you make better design decisions.