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 multi-valued attributes in SQL?
Multivalued Attributes A multivalued attribute of an entity is an attribute that can have more than one value associated with the key of the entity. For example, a large company could have many divisions, some of them possibly in different cities.
There is generally no such thing as multi-valued attribute in relational databases. Possible solutions for your problem: Create a separate table for storing phone numbers which references your company table by primary key and contains undefinite number of rows per company.
A multivalued attribute is represented using a double oval with the name of the attribute inside the oval.
Relationship : Storing multiple values for one attribute of an entity is done through referenced key-foreign key relationships. For more info, check this. Set : A scalar datatype in mysql that can have zero or more values, each of which must be chosen from a list of permitted values specified when the table is created.
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.'
There is generally no such thing as multi-valued attribute in relational databases.
Possible solutions for your problem:
Create a separate table for storing phone numbers which references your company table by primary key and contains undefinite number of rows per company.
For example, if you have table company
with fields id, name, address, ...
then you can create a table companyphones
with fields companyid, phone
.
(NOT recommended in general, but if you only need to show a list of phones on website this might be an option) Storing telephones in a single field using varchar(...) or text and adding separators between numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With