filmov
tv
Day-11) Constrains in sql database table #sqlserver #database #databasetutorial #coding #training

Показать описание
| SQL training| SQL Tutorial for beginners| Intellipaat
Constraints in SQL Server :
What is Data Integrity in SQL Server?
Data integrity means the data contained in the database is accurate, consistent, and reliable. To provide data integrity, RDBMS provides us with a set of integrity constraints that ensures that the data entered into the database is going to be accurate, consistent, and reliable. This is the reason why the end-user can trust the data.
What is a Constraint in SQL Server?
We can define the SQL Server Constraint as a property that can be assigned to a column or columns of a table. The SQL Server Constraints are mainly used to maintain data integrity.
Why do we need SQL Server Constraints?
The SQL Server Constraints are used to restrict the insertion of unwanted data in any columns i.e. they are mainly used to maintain data integrity. We can create the constraint on single or multiple columns of a table in SQL Server.
What are the different types of SQL Server Constraints available?
SQL Server supports six types of constraints for maintaining data integrity. They are as follows
1. Default Constraint
2. UNIQUE KEY constraint
3. NOT NULL constraint
4. CHECK KEY constraint
5. PRIMARY KEY constraint
6. FOREIGN KEY constraint.
Note: Constraints are imposed on columns of a table.
Default Constraint in SQL Server
The Default constraint in SQL Server is used to fill the column with a default value that is defined during the creation of a table if the user does not supply any value while inserting the data. In simple words, we can say that Default constraints enable the SQL Server to insert a default value to a column when the user doesn’t specify a value.
CREATE TABLE Employee (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Mumbai’,
DateOfBirth date DEFAULT GETDATE(),
Salary DECIMAL (18, 2) DEFAULT 5000.00
)
Let’s insert a record into the Employee table by executing the following Insert statement.
Insert into Employee (ID, LastName, Age) values(1, ‘Rout’, 30)
NOT NULL Constraint in SQL Server:
When you want a column not to accept NULL then you need to apply the NOT NULL constraint to that column. That means this constraint is used to avoid NULL values but it accepted duplicate values into a column.
CREATE TABLE customer
(
id INT NOT NULL,
name VARCHAR(30) NOT NULL,
mobileNo CHAR(10) NOT NULL
)
Insert Values:
INSERT customer VALUES (101, 'AA', '1111111111')--ALLOWED
INSERT customer VALUES (101, 'AA', '2222222222')--ALLOWED
INSERT customer VALUES (NULL, NULL, NULL)--NOTALLOWED
Constraints in SQL Server :
What is Data Integrity in SQL Server?
Data integrity means the data contained in the database is accurate, consistent, and reliable. To provide data integrity, RDBMS provides us with a set of integrity constraints that ensures that the data entered into the database is going to be accurate, consistent, and reliable. This is the reason why the end-user can trust the data.
What is a Constraint in SQL Server?
We can define the SQL Server Constraint as a property that can be assigned to a column or columns of a table. The SQL Server Constraints are mainly used to maintain data integrity.
Why do we need SQL Server Constraints?
The SQL Server Constraints are used to restrict the insertion of unwanted data in any columns i.e. they are mainly used to maintain data integrity. We can create the constraint on single or multiple columns of a table in SQL Server.
What are the different types of SQL Server Constraints available?
SQL Server supports six types of constraints for maintaining data integrity. They are as follows
1. Default Constraint
2. UNIQUE KEY constraint
3. NOT NULL constraint
4. CHECK KEY constraint
5. PRIMARY KEY constraint
6. FOREIGN KEY constraint.
Note: Constraints are imposed on columns of a table.
Default Constraint in SQL Server
The Default constraint in SQL Server is used to fill the column with a default value that is defined during the creation of a table if the user does not supply any value while inserting the data. In simple words, we can say that Default constraints enable the SQL Server to insert a default value to a column when the user doesn’t specify a value.
CREATE TABLE Employee (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Mumbai’,
DateOfBirth date DEFAULT GETDATE(),
Salary DECIMAL (18, 2) DEFAULT 5000.00
)
Let’s insert a record into the Employee table by executing the following Insert statement.
Insert into Employee (ID, LastName, Age) values(1, ‘Rout’, 30)
NOT NULL Constraint in SQL Server:
When you want a column not to accept NULL then you need to apply the NOT NULL constraint to that column. That means this constraint is used to avoid NULL values but it accepted duplicate values into a column.
CREATE TABLE customer
(
id INT NOT NULL,
name VARCHAR(30) NOT NULL,
mobileNo CHAR(10) NOT NULL
)
Insert Values:
INSERT customer VALUES (101, 'AA', '1111111111')--ALLOWED
INSERT customer VALUES (101, 'AA', '2222222222')--ALLOWED
INSERT customer VALUES (NULL, NULL, NULL)--NOTALLOWED