0

Let's say we have a table called Salesman with the folling properties

Create Table SalesMan(
  EmplyeeId int Primary Key,
  Name varchar(20) Not Null,
  LastName Varchar(20) Not Null,
  Email Varchar(40) Unique);

and a simple table definition for "Order"

Create Table Orders(
  Number Varchar(20),
  Quantity Int,
  DealerCode Varchar(20),
  ModelName Varchar(20),
  ModelVersion Varchar(20));

At this point I forgot to add a One to Many relation between Order and Salesman so that a SalesMan can have many orders and Orders can have only one Salesman

Alter Table Orders Add sm_fk int;
Alter Table Orders Add Constraint Sales_fk Foreign Key (sm_fk) References SalesMan(EmplyeeId)

Now my question is: could I have simplified the query by omitting the second alter table query and compress it to

Alter Table Orders add sm_fk int References SalesMan(EmplyeeId)

Would this be the same as the second Alter Table query?

Thank you for your time!

2
  • 4
    Yes, they're the same. You can specify a foreign key constraint during column creation, or afterwards in an alter command. Only difference is that you've explicitly specified the name "Sales_fk" in the two-queries version.
    – Brad Koch
    Jan 22, 2020 at 13:31
  • 2
    ALTER TABLE allows to specify more than one action, so both queries may be combined into one - in such case both constraint symbol and index name may be specified.
    – Akina
    Jan 22, 2020 at 13:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.