SQL: multi valued attributes

后端 未结 4 1165
庸人自扰
庸人自扰 2020-12-31 04:21

I created a table that contains information about a company. One attribute is their telephone number. A company can have many telephone numbers.

How do I create mul

4条回答
  •  一整个雨季
    2020-12-31 05:13

    In a separate table like:

    CREATE TABLE Company
    (
      Id int identity primary key, 
      Name nvarchar(100) not null UNIQUE --UNIQUE is optional
    )
    GO
    CREATE TABLE CompanyPhones
    (
      Id int identity primary key, 
      Phone nvarchar(100) not null, 
      CompanyId int NOT NULL REFERENCES Company(Id) ON DELETE CASCADE
    )
    

    How to use these structures:

    SELECT CompanyPhones.Phone
    FROM Company
    JOIN CompanyPhones
      ON Company.Id = CompanyPhones.CompanyId
    WHERE Company.Name=N'Horns and Hoogs Ltd.'
    

提交回复
热议问题