【问题标题】:SQL Database Design with more than one data in same column同一列中具有多个数据的 SQL 数据库设计
【发布时间】:2020-03-12 14:11:33
【问题描述】:

我想创建一个包含多个信息的数据库表。这些信息之一是电话。我如何创建一个数据库表,它在电话列中将包含比方说子列,因为用户可能拥有多个电话并存储在同一行的同一 ID 中。

下面是我的意思以及我希望我的桌子如何的图片...

所有答案都很有帮助,不幸的是我只能选择一个,但所有答案都对我有帮助

【问题讨论】:

  • 使用的是哪个 DBMS?
  • 当然,规范化你的数据模型(如所有答案所示)是推荐的方式,但一些 DBMS 支持 arrays,可用于存储此类信息.

标签: sql database database-design foreign-keys create-table


【解决方案1】:

关系数据库中没有子列的概念。

如果您想为每个用户存储可变数量的电话,一种解决方案是创建另一个表以将它们存储为phones 表应该有一个列来存储与每个电话相关的用户的 id,并带有一个外键约束,以确保该列中包含的值确实存在于 users 表中。

所以:

users(id, name, address)
phones(id, user_id, phone_no)

Typical DDL (MySQL syntax)

create table users (
    id int primary key auto_increment,
    name varchar(50) not null,
    address varchar(50)
);

create table phones(
     id int primary key auto_increment,
     user_id int not null,
     phone_no varchar(20) not null,
     foreign key (user_id) references users(id)
);

【讨论】:

    【解决方案2】:

    您通常不会。您创建另一个表。假设你有:

    create table users (
        userId serial primary key,
        name varchar(255),
        . . .
    );
    

    然后你创建一个“联结”或“关联”表:

    create table userPhones (
         userPhoneId serial primary key,
         userId int references users(userId),
         phone varchar(32)
    );
    

    您没有指定数据库。我使用serial 作为主键,因为它被 Postgres 使用并且易于键入。

    【讨论】:

      【解决方案3】:

      SQL Fiddle

      MS SQL Server 2017 架构设置

      create table my_user (
          ID  int primary key,
          FirstName varchar(255),
          LastName varchar(255),
          address varchar(max)
      )
      create table user_phones (
           ID int primary key,
           userId int references my_user(ID),
           countrycode int,
           phone varchar(max),
           type varchar(255)
      )
      INSERT INTO my_user(ID,FirstName,LastName,address) VALUES(1,'test','test','test');
      INSERT INTO user_phones(ID,userId,countrycode,phone,type)VALUES(1,1,99,'000099900','mobile');
      INSERT INTO user_phones(ID,userId,countrycode,phone,type)VALUES(2,1,99,'99900000','home');
      INSERT INTO user_phones(ID,userId,countrycode,phone,type)VALUES(3,1,99,'000009999','fax');
      

      查询 1

      select * from my_user u
      left join user_phones p on u.ID=p.userId
      

      Results

      | ID | FirstName | LastName | address | ID | userId | countrycode |     phone |   type |
      |----|-----------|----------|---------|----|--------|-------------|-----------|--------|
      |  1 |      test |     test |    test |  1 |      1 |          99 | 000099900 | mobile |
      |  1 |      test |     test |    test |  2 |      1 |          99 |  99900000 |   home |
      |  1 |      test |     test |    test |  3 |      1 |          99 | 000009999 |    fax |
      

      【讨论】:

        【解决方案4】:

        这是一个 ER 流程,在您的情况下通过主键外键关系有一个电话号码

               users(id (Pk), name, address, phoneid(fk))
        
               phones(userid(fk), phoneid (pk), phonenumber) 
               here userid and phoneid references users.phoneid and users.id
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-08
          • 2018-02-17
          • 1970-01-01
          • 2020-10-16
          • 1970-01-01
          • 2011-05-03
          • 2021-07-15
          • 1970-01-01
          相关资源
          最近更新 更多