M
M
Max Trophy2016-07-10 17:53:10
Transact SQL
Max Trophy, 2016-07-10 17:53:10

How to ensure the integrity of a ms sql server database?

For example, let's take 2 simple tables with Products (Products) and Suppliers (Vendors).
bae1b417f28744648927a7a5c7b6818e.JPG
It is necessary that it is not allowed to delete the Supplier (Vendor) if there is an entry in Products with its id.
I know that this can be done with a diagram. (we put the primary key on the Supplier and the foreign key on the Product).
Based on the definition of diagrams:

Charts are database components that block the deletion of records from primary tables if there are records associated with them in secondary tables. Therefore, charts prevent data integrity violations. In SQL Server, charts are created using the Chart Wizard

I follow this step, but when deleting an entry from Products, no warning comes up and nothing at all, the entry is simply deleted. Maybe something else needs to be done? (I know about triggers, but it seems that the diagram should be suitable without triggers)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksey Ratnikov, 2016-07-10
@kapitoly

Use a foreign key , it is specifically designed for this:

ALTER TABLE Products ADD FOREIGN KEY(vendorId) REFERENCES Vendors(id);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question