How the sql database creates relationships

How to connect table to table relationship in SQL database (how to create association between sql tables)

Tables are linked by primary and foreign keys

Can be linked by ‘Database Relationship Diagram’

Select the tables you want to connect and then use the mouse to Drag

Columns

For example

Define the relationship diagram between the tables Student, Course and SC.

Requirements: First, define the primary keys SNO, CNO, (SNO, CNO) for the three tables

(1) Expand the “Student Management” node of the database, right-click on the “Database Relationship Diagram”, and choose “New Database Relationship Diagram” command to bring up the New Database Relationship Diagram Wizard, select the tables Student, Course and SC to be added to the relationship diagram, and the three tables will appear in the New Relationship Diagram window. Each table displays the contained attributes and defined primary keys, and dragging the title bar changes their position in the window.

(2) Student table attribute SNO dragged to the SC table SNO, release the mouse to pop up the “Create Relationship” window, set up and click “OK” button. In the Student table and SC table will automatically appear between a line, that the creation of the relationship is successful. In the same way, you can create a relationship between the Course table and the SC table.

(3) Click the Close button to save.

(4) Try modifying or deleting the data in the Student, Course, and SC tables to see what the defined relationship does.

Sqlserver how to create primary and foreign key relationships

Sqlserver how to create primary and foreign key relationships.

Following reference:

1. To help you understand with an example. It basically means constraining a field in table 1 by referencing a field in table 2.

2. There are two tables, the first one creates a foreign key table because of the first foreign key, the primary key can be referenced. First create the database and then create a new query.

3. Create a new foreign key table [teacher], then enter the command:Createtableteacher. add the required field for teacher_xingming and set it to [primary key]. nchar(10) primary key,

4. Select the command line you need to execute the command line, click [execute] and at the bottom there will be a success or failure message. Now, after performing a refresh operation on the table, you will find that a new table has been successfully created [teacher].

5. Next, create the table student(student_xingmingnchar(10) primary key is not null).

6. Continue to write, type:student_banjinchar(10)notnull, — a new [student_banji field, type nchar(10), not allowed to be null].

7, create a foreign key, enter the command student_jiaoshinchar(10) foreign key reference teacher (teacher_xingming) looks very long, the actual meaning is very simple: in the table to create a new (student_jiaoshi fields), it is a foreign key, the nchar(10) type, the content of teacher_xingming.

8. Select, click [Execute], there is no error, that is success. Refresh the table and see if any additional table [student]? All constraints have been created.

9. Let’s see if we can actually do what we said. Open the Teacher form and fill in “Mr. Liu”. Open the [student table], fill in any front, and finally fill in “Mr. Liu”, no error is possible.

10. How to test whether the constraint is really effective, fill in the other content to see if it is successful? As long as it is not in the teacher field), it reports an error.