English 中文(简体)
文 件:
原标题:SQL: multi valued attributes
  • 时间:2012-01-15 08:18:24
  •  标签:
  • sql
  • database

我创建了一个表格,其中载有关于公司的信息。 其中一个特征是他们的电话号码。 公司可以拥有许多电话号码。

我如何在Kall创造多价值特性?

问题回答

单独表格如下:

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
)

如何利用这些结构:

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

总的来说,在关系数据库中不存在多价值属性。

3. 可能的解决办法:

  1. 建立一个单独的电话平台,以显示你的公司桌位按主要钥匙分列的数字,每家公司的浏览量不明确。

    例如,如果您在<条码>与领域<条码>、名称、地址、......上有表<条码>,则你可建立一个表格<条码>。

  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.

除了Oleg和Serges的答复外,第三个选择可能是在公司桌上建立多个电话领域,例如,作为主开关板和传真线路的<条码>。

这种解决办法通常被视为一种非正常状态,通常只有在少数多种选择的情况下才合适,每个选择都有明确的作用。

因此,例如,这是代表陆地和移动电话号码参加联络名单表的一种常见办法,但完全不适合公司内所有电话延期清单。

房舍管理事务的不同实施有一些可能性。

例如,见。 PostgreSQL 您可使用array hstore。 或甚至JSON(9.3版本):

create table Company1 (name text, phones text[]);

insert into Company1
select  Financial Company , array[ 111-222-3333 ,  555-444-7777 ] union all
select  School , array[ 444-999-2222 ,  555-222-1111 ];

select name, unnest(phones) from Company1;

create table Company2 (name text, phones hstore);

insert into Company2
select  Financial Company ,  mobile=>555-444-7777, fax=>111-222-3333 ::hstore union all
select  School ,  mobile=>444-999-2222, fax=>555-222-1111 ::hstore;

select name, skeys(phones), svals(phones) from Company2    

<http://sqlfiddle.com/#!1/599e5/3” rel=“nofollow noreferer”>sql fiddle demo

您也可在以下领域建立指数:https://dba.stack Exchange.com/questions/45820/how-to-properly-index-hstore-tags-column-to-faster-search-for-keys,

在<>SQL服务器上,你可以使用xml数据类型储存多价值:

create table Company (name nvarchar(128), phones xml);

insert into Company
select  Financial Company ,  <phone type="mobile">555-444-7777</phone><phone>111-222-3333</phone>  union all
select  School ,  <phone>444-999-2222</phone><phone type="fax">555-222-1111</phone> 

select
    c.name,
    p.p.value( @type ,  nvarchar(max) ) as type,
    p.p.value( . ,  nvarchar(max) ) as phone
from Company as c
    outer apply c.phones.nodes( phone ) as p(p)

<http://sqlfiddle.com/#!3/ba415/2”rel=“nofollow noreferer”>sql fiddle demo

也可create xmlindexes on xml打字栏。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签