单行的多个值 [英] Multiple Values For Single Row

查看:64
本文介绍了单行的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单行多个值



嗨我对数据库完全陌生,作为一个项目我必须设计一个用户数据库......他们必须先注册像任何网站一样..所以我使用存储过程并使用insert命令对数据库进行输入...它现在正在工作..



现在每个用户都会搜索并添加数据库中的其他用户..所以每个用户都会有一个联系人列表...我不知道如何实现这个...



到目前为止我创建了一个table''UserAccount''列名为

UserName as varchar(50)

密码为varchar(50)

EmailID as varchar( 100)

DateOfJoining as datetime

UserID as int --->这对于user..i启用自动增量是唯一的..这是主键..



所以现在每个用户都必须有一个其他用户ID的列表..作为联系人列表..



任何帮助任何想法将是伟大的,因为我不知道如何为每一行放置多个值..我甚至不知道如何搜索这个问题的解决方案......如果这张贴在其他地方,对不起..

谢谢!

Multiple Values For Single Row

hi iam totally new to databases , as a project i have to design a database of users...they have to register first like any site..so i used stored procs and made entries to database using insert command...its working for now..

now every user will search and add other users in the database..so every user will have a contact list...i have no idea how to implement this...

so far i created a table ''UserAccount'' with column names as
UserName as varchar(50)
Password as varchar(50)
EmailID as varchar(100)
DateOfJoining as datetime
UserID as int ---> this is unique for user..i enabled automatic increment..and this is primary key..

so now every user must have a list of other userid''s.. as contact list..

Any help any ideas will be great since i have no clue how to put multiple values for each row..i didnt even know how to search for this problems solution..iam sorry if this posted somewhere else..
THANK YOU !

推荐答案

有两种方法可以这样做 - 你的联系人列表是一个字符串,其中的联系人用特殊字符分隔,或者你有一个单独的表格。

问题是SQL没有数组的概念 - 所以你不能有一个包含可变数量字段的行。

虽然您可以用字符串设置它:

There are two ways to do it - either your contact list is a string with contacts separated by a special character, or you have a separate table for this.
The problem is that SQL does not have a concept of arrays - so you can''t have a row with a variable number of fields.
While you could set it up with a string:
UserID|UserID|UserID

很快就会变成PITA来操纵或搜索,或者更糟糕的是 - 完全从系统中删除用户。



最好的方法是创建一个单独的表Contacts:

THis very quickly becomes a PITA to manipulate or search, or worse - remove a user from your system completely.

The best approach is to create a separate table, "Contacts":

ID          int - This can be an identity field.
UserID      int - ID of your user
ContactID   int - ID of one of his contacts



然后,您可以通过简单的方式获取联系人列表SQL语句:


You can then fetch the list of contacts with a simple SQL statement:

SELECT ContactID FROM Contacts WHERE UserID=100

更改100以匹配您的用户ID。

Changing the 100 to match the ID of your user.


这篇关于单行的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆