In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
How to create constraints in SQL Server2008, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
1. Primary Key constraint
There is often a combination of one or more columns in a table whose values uniquely identify each row in the table.
Such a column or columns become the primary key (PrimaryKey) of the table. A table can have only one primary key, and the columns in the primary key constraint cannot be null. Only primary key columns can be created as foreign keys of other tables.
To create a primary key constraint, you can right-click the table and select Design.
Select the column for which you want to create the primary key, and then click the small key above.
You can also right-click the column where you want to create the primary key, and then click the small key.
2. Foreign Key constraint
Foreign key constraints are used to strengthen the join between one or more columns of data in two tables (master and slave tables). The order in which foreign key constraints are created is to define the primary key of the primary table and then the foreign key of the slave table. In other words, only the primary key of the master table can be used as a foreign key in the slave table, the columns in the constrained slave table may not be the primary key, and the master table restricts the operation of updating and inserting from the slave table.
Right-click the column where you want to set the foreign key (at this time the table appears in the foreign key as a slave table) and select the relationship.
Next, click add-> Table and column specifications.
Select the primary table and the primary key column of the primary table in the primary key table.
Save it after setting it up.
3. Unique constraint
Unique constraints ensure that a column of data in a table does not have the same value. Like primary key constraints, unique constraints also enforce uniqueness, but unique constraints are used for one or more columns that are not primary keys, and a table can define multiple unique constraints.
Right-click the column you want to set and select the index / key.
Then click the add button.
Select the column you want to set, which can be a combination of one column or multiple columns.
Close and save the settings.
4. Default constraint
If the default value constraint is defined in the table, the system assigns the default value to the column if the user inserts a new data row and the row does not specify data. If we do not set the default value, the system defaults to NULL.
Take the student information table as an example, in the table designer, fill in the default male value for the gender sex column.
5. Check constraint
Check constraints determine the validity of data through logical expressions and are used to limit the range of values entered in one or more columns. When updating data in a column, what you want to enter must meet the conditions of the Check constraint, otherwise it will not be entered correctly.
Taking the sex in the student information table as an example, we want to limit the value of the sex column to male or female.
Close and save the design.
As a result, the five constraints in the database are reviewed again. Constraints ensure the integrity of the data in the database, but constraints alone are far from enough.
This is the answer to the question about how to create constraints in SQL Server2008. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.