Mastering Data Types and Constraints in SQL: A Comprehensive Guide

06 Jun 2023 Balmiki Mandal 0 SQL

Data Types and Constraints in SQL

SQL data types are used to specify the type of data that can be stored in a column in a table. There are many different data types available in SQL, each with its own specific purpose. Some of the most common data types include:

  • INTEGER: Stores whole numbers, such as 1, 10, and 100.
  • VARCHAR: Stores variable-length strings of text.
  • DATE: Stores dates and times.
  • DECIMAL: Stores numbers with decimal places.
  • BOOLEAN: Stores true or false values.

SQL constraints are used to enforce rules on the data in a table. Constraints can be used to ensure that the data is accurate, consistent, and complete.

Some of the most common constraints include:

  • PRIMARY KEY: A PRIMARY KEY uniquely identifies each row in a table.
  • FOREIGN KEY: A FOREIGN KEY creates a relationship between two tables.
  • UNIQUE: A UNIQUE constraint ensures that all values in a column are unique.
  • NOT NULL: A NOT NULL constraint ensures that a column cannot have a NULL value.

Adding Constraints

Constraints can be added to a table when it is created, or they can be added to an existing table using the ALTER TABLE statement. For example, the following statement creates a table called customers with a PRIMARY KEY constraint on the customer_id column:

SQL
CREATE TABLE customers (
  customer_id INT NOT NULL AUTO_INCREMENT,
  first_name VARCHAR(255) NOT NULL,
  last_name VARCHAR(255) NOT NULL,
  PRIMARY KEY (customer_id)
);

The AUTO_INCREMENT keyword tells the database to automatically generate a unique value for the customer_id column each time a new row is inserted into the table.

The following statement adds a FOREIGN KEY constraint to the customers table, which ensures that the customer_id column can only contain values that exist in the orders table:

SQL
ALTER TABLE customers
ADD CONSTRAINT fk_customers_orders FOREIGN KEY (customer_id) REFERENCES orders (order_id);

Exploring Data Types

The following table shows some examples of common SQL data types and their uses:

Data Type Usage
INTEGER Storing whole numbers, such as product IDs, customer IDs, and order numbers.
VARCHAR Storing variable-length strings of text, such as customer names, product descriptions, and addresses.
DATE Storing dates and times, such as order dates, product release dates, and customer birthdays.
DECIMAL Storing numbers with decimal places, such as product prices, shipping costs, and taxes.
BOOLEAN Storing true or false values, such as whether an order has been shipped or whether a customer has subscribed to a newsletter.
 

Modifying Existing Tables with ALTER TABLE

The ALTER TABLE statement can be used to modify existing tables in a variety of ways, including adding and removing constraints. For example, the following statement removes the FOREIGN KEY constraint from the customers table:

SQL
ALTER TABLE customers
DROP CONSTRAINT fk_customers_orders;

The ALTER TABLE statement can also be used to modify the data types of columns in an existing table. For example, the following statement changes the data type of the customer_name column from VARCHAR(255) to VARCHAR(500):

SQL
ALTER TABLE customers
ALTER COLUMN customer_name VARCHAR(500);

Conclusion

SQL data types and constraints are essential for ensuring the accuracy, consistency, and completeness of data in a relational database. By using data types and constraints effectively, you can build data models that are reliable and easy to maintain.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.