Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: multi valued attributes

Tags:

sql

database

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?

like image 811
user1150176 Avatar asked Jan 15 '12 08:01

user1150176


People also ask

What are multivalued 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.

How do I write a multivalued attribute query in SQL?

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.

How do you represent a multi-valued attribute?

A multivalued attribute is represented using a double oval with the name of the attribute inside the oval.

How do you store multiple value attributes in a database?

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.


2 Answers

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.'
like image 68
Oleg Dok Avatar answered Sep 28 '22 07:09

Oleg Dok


There is generally no such thing as multi-valued attribute in relational databases.

Possible solutions for your problem:

  1. 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.

  2. (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.

like image 44
Sergey Kudriavtsev Avatar answered Sep 28 '22 08:09

Sergey Kudriavtsev