Creation of views and primary foreign keys in SQL Server

Source: Internet
Author: User

I. BACKGROUND

Originally learned the relevant content of the database, just form, never practiced, slowly drifting just, now in the engine room, a lot of knowledge needs to use the view, only to begin to slowly practice.

View: What I understand is a table that extracts some of the information we need from a table or a few tables, creating a temporary table.

It has two advantages: 1. Simple, the data that it presents to us is the data we want, no superfluous.

2. Security, providing users with the data within their purview

Why would I use a view? As a simple example, we may use the contents of two tables when querying user information. Then I need to extract the data I need from two tables to form a virtual table, and when I use it, I can see the data I want to see directly from this virtual table. This virtual table is a view.

Second, actual combat

But this time to do some small problems, if in a table each data record only one, and the other table in the corresponding data corresponding to more than one, at this time if the direct view, the results will be a lot of repetition.

How to solve it?

Of course, the use of primary and foreign keys to constrain. This will automatically summarize the corresponding data records in the view. Records that exist in the foreign key table are automatically matched to the primary key table

third, the creation of foreign keysRight-click the database diagram:

On the table that you added, right-click the relationship:


Click the table and column specifications to set the column names of the primary key table and foreign key table

There are several specifications in the insert and update specification that are identified in the table above, which must be set up. At first I chose not to do anything, but when I delete the primary key table records, will be error, because the foreign key table depends on the record disappears, then there is no foreign key value, so the foreign key table records must be deleted, so when it comes to delete operations, we should choose "Cascade", That is, the data in the Foreign key table is updated as the primary key table is updated.

The settings are as follows: Select the column name you want to set


So the primary key foreign key relationship of the two tables is basically built.

Iv. creation of viewsit should be easier to create a view, as long as you select New in the view and choose the column name you want. when using views in the room, we should be aware of the points that we see as a table, so when we write the code, we create a new entity class to hold the column data we need. When querying a table on the D-level, we can directly query the name of the view we created, usually starting with v_.
These are some of the things that are related to the use of views and primary foreign keys, and the refactoring reinforces what you've learned before, and a little more about the knowledge in SQL Server.
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.