Integrity constraints in dbms|SKILLS ARENA

0

 Integrity constraints in dbms


Integrity constraints are rules that are used to ensure the accuracy and consistency of data in a database. They are used to specify the data that is allowed in a database and the relationships between different data elements.


Integrity constraints in dbms SKILLS ARENA


There are several types of integrity constraints that can be used in a database:


NOT NULL constraint: 

This constraint specifies that a column cannot contain a NULL value. For example, if we have a table with a column age that has a NOT NULL constraint, we cannot insert a NULL value into that column.


UNIQUE constraint: 

This constraint ensures that all the values in a column are unique. For example, if we have a table with a column email that has a UNIQUE constraint, we cannot insert two rows with the same email address.


PRIMARY KEY constraint: 

This constraint defines a column or set of columns as the primary key of a table. The primary key is used to uniquely identify each row in the table and it cannot contain NULL values.


FOREIGN KEY constraint: 

This constraint is used to enforce referential integrity in a database. It specifies that the values in a column (or set of columns) must match the values in the primary key of another table. For example, if we have a table called orders with a column customer_id that is a foreign key to the customers table, we cannot insert an order for a customer that does not exist in the customers table.


CHECK constraint: 

This constraint is used to specify a condition that must be met by the data in a column. For example, if we have a table with a column age that has a CHECK constraint specifying that the age must be greater than 18, we cannot insert a row with an age value of 17.


Here is an example of how you might use these constraints in a SQL table:



CREATE TABLE customers ( customer_id INT PRIMARY KEY, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL, age INT CHECK (age > 18) ); 
CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT FOREIGN KEY REFERENCES customers(customer_id), order_total DECIMAL NOT NULL );

In this example, we have created two tables: customers and orders. The customers table has a primary key column customer_id and three other columns with NOT NULL constraints. The email column also has a UNIQUE constraint and the age column has a CHECK constraint. The orders table has a primary key column order_id and a foreign key column customer_id that references the customer_id column in the customers table. The order_total column has a NOT NULL constraint.



______________________________________



Please share this post and blog link with your friends.For more programs use this blog.


If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com


Created by-- HARSH CHAUHAN


Tags

Post a Comment

0Comments
Post a Comment (0)