Introduction to DBMS Constraints
Constraints are very important parts of the dbms, which allows us to impose rules of the data type of data. The main purpose of using constraints is to maintain and provide the integrality while deleting, inserting or updating from the database tables. We have many types of constraints available in dbms, which ensures that we don’t have to handle manually in the code; the dbms constraints will automatically handle it. A few of the types of constraints in dbms are integrality constraints, referential constraints, domain constraints, entity constraints etc.
Relational Model Concepts
The relational model is used to represent a table which consists of rows and columns. Each row in the tables is known as the tuple, and each column of the table is associated with a name which is called as the attribute of the table. These column names are used to identify the type of value stored inside it. Relational model concepts consist of few terminologies.
- Domain: It is used to represent the type of value that attribute will hold for a column. That means if the attribute is ‘name’, then it will contain the character, not numeric values inside it. So this decides the domain for the specific attribute.
- Relational instance: This can be represented by the set of tuples or rows from the table. All these tuples or rows should be unique and should not contain duplicates.
- Attribute: This concept of the relational model is used to represent the name of the column in a table. An attribute must define the type of an attribute as well. So it is used to represent the column of the table uniquely.
- Relational key: Relational key can be used to identified the record of the table uniquely. In relational key, every row in that table can have one or more attributes.
- Relational schema: This concept is used to define our relation model’s name, attribute or column, etc.
Operations in Relational Model
These operations are required to perform anything on the relational model; we have four basic operations that can be used to get the record from the table. By using this, we can easily modify the structure of the model in dbms.
This is used to get any record from the table or model. We can either select all the records from the table or apply a filter based on the condition we want.
SELECT * FROM table_name WHERE your_condition if any;
This is the basic syntax for using the SELECT statement in a relational model. Let’s see the basic select query to fetch data.
SELECT * FROM Customers where city = ‘London’;
This statement can be used to delete the record from the table; also, we can specify the condition with it using the ‘where’ clause.
DELETE FROM table_name WHERE your_condition;
This is the basic syntax for using the DELETE statement in a relational model; let’s see at the basic select query to fetch data.
DELETE FROM Customers where city = 'Berlin';
This is used to insert new records into the table. It will always create a new row inside the table.
INSERT INTO table_name (column1, column2, so on..) VALUES (value1, value2, so on..);
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('New Cus', 'Hello', 'some house', 'Mumbai', '400078', 'India');
This statement is used to update the records inside the table, which is already existing.
SET ContactName = 'amit verma', City= 'Mumbai'
WHERE CustomerID = 2;
Examples of DBMS Constraints
Different examples are mentioned below:
Example #1 – Key constraints
This is used to identify the record in the table uniquely, so that means we cannot duplicate the column with key constraints applied.
|Id (Key Constraints)||First_Name||Last_Name|
Not allowed because Id already present at the table.
Example #2 – Domain constraints
This is used to define the value that an attribute can take. Here age can only be numeric; we cannot insert a string to it.
|Id (Key constraints)||Name||Age (Numeric)|
Not allowed because Age is a numeric attribute.
Example #3 – Entity integrity constraints
This is used to indicate the rule on the primary key filed, which cannot contain the null key because we used this to identified the record.
|Id (Primary Key)||Name||Age (Numeric)|
Example #4 – Referential constraints
Related with the foreign key, every foreign key value must be present in the primary key table.
|Id (Primary Key)||Name||Age (Numeric)|
Advantages and Disadvantages of using DBMS Constraints
Given below are the advantages and disadvantages mentioned:
- It helps us to achieve the data integrity.
- Need not to handle separately in code by applying checks.
- Throw exception if anything invalid we try to add.
- Increase complexity
- Database failure
- Huge size, cost
By using it, we can take care of our data in the table, all the points seen in the article, which helps us understand when to use this. Easy to use, readable, and maintainable by the developers.
This is a guide to DBMS Constraints. Here we discuss the introduction, relational model concepts, operations, examples, advantages & disadvantages. You may also have a look at the following articles to learn more –