Create views and primary foreign keys in SQLserver, and create views in sqlserver

Source: Internet
Author: User

Create views and primary foreign keys in SQLserver, and create views in sqlserver
I. background

I have learned the relevant content of the database, but I have never practiced it in the form of a database. Now, when I am working on a data center, I need a lot of knowledge to use the view, and then start to practice it slowly.

View: I understand a table. It extracts some information from a table or several tables we need to form a temporary table.

It has two main advantages: 1. Simplicity. The data presented to us is the data we want, and there is no excess.

2. Security: provides users with data within their Permissions

Why do I use a view? For example, when querying user information, we may use the content in two tables. At this time, I need to extract the data I need from two tables to form a virtual table. When I use it, I can view the data that I want to view directly from this virtual table. This virtual table is a view.

Ii. Practice

However, this operation encountered some minor problems. If there is only one data record in one table, but there are multiple data records in the other table, if you directly create a view, the results are repeated.

How can this problem be solved?

The primary key and foreign key are used as constraints. In this way, the corresponding data records are automatically summarized in the view. Records in the foreign key table are automatically matched with those in the primary key table.

3. Create a foreign key and right-click the database relationship diagram:

Right-click the link on the added table:

 
Click table and column specification to set the column names of the primary key and foreign key tables.

You must set the Insert and Update specifications described in the preceding table. At the beginning, I chose not to perform any operations, but when I delete the records in the primary key table, an error will be reported because the records on which the foreign key table depends have disappeared, this foreign key value does not exist, so the records in the foreign key table must also be deleted. Therefore, when the delete operation is involved, we should select "cascade ", that is, the data in the foreign key table is updated with the primary key table.

The specific settings are as follows: select the column name you want to set.


In this way, the foreign key relationship of the primary keys of the two tables is basically created.

4. Creating a view should be easier. You only need to create a new view and select the desired column name. When using a view in the data center, we should pay attention to the following points: we will regard the view as a table, so a new entity class will be created during code writing, to store the column data we need. When querying a table at Layer D, you can directly query the name of the created view, which generally starts with V.
The above are some operations related to views and the use of the primary and Foreign keys. through reconstruction, we have consolidated the previous knowledge and gained a little more understanding of the knowledge in SQL server.
How does SQL server create a primary foreign key statement?

Constraint Primary Key name Primary Key (), create a Primary Key
Constraint Foreign Key name Foreign Key (column name)
Which table (column name) Does References use to create a foreign key?

How does SQL server create a primary-foreign key relationship?

In the foreign key table for which you want to set the link, right-click the link → add → select the associated primary table in the table and column specifications, and then select the fields associated with the foreign key table.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.