且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用注册表创建关系其他表?

更新时间:2023-09-28 22:44:46

我很抱歉这样说,但创建外键关系是非常基本的知识......错误信息你得到的也很简单。

你的 ID 是一个 INT 而你的用户名 VARCHAR(20)。创建外键约束时,您要确保一个表中的值与另一个表中的值相对应。如果其中一个表的值甚至无法在另一个表中创建,怎么能保证呢?



除此之外你的问题毫无意义。您想在同一个表中将外键放在 ID 用户名上吗?

以下是关于外键约束的一些内容: http://technet.microsoft。 com / en-us / library / ms175464(v = sql.105).aspx [ ^ ]。

我建议您阅读,或者这里没有人可以为您做任何事情。 />
祝你好运,听起来像个好项目!
I'm sorry to put it like this, but creating foreign key relations is pretty basic knowledge... The error message you're getting is also pretty straightforward.
Your ID is an INT while your Username is a VARCHAR(20). When creating a foreign key constraint you're making sure that a value in one table corresponds with a value in another table. How can this be guaranteed if one of the tables can have values that cannot even be created in the other table?

Other than that your question makes no sense. You want to put a foreign key on ID and Username from the same table?
Here's some reading up on Foreign Key Constraints: http://technet.microsoft.com/en-us/library/ms175464(v=sql.105).aspx[^].
I suggest you read it, or no one here can do anything for you.
Good luck on your assignment though, sounds like a good project!


Sander Rossel提供的笔记非常有用。可能会很快描述:从基础开始!



哇,11桌!

另一个注意事项是:我建议减少表格数量至少为3 :)



Notes provided by Sander Rossel are very useful. It could be shortly described: start with basics!

WOW, 11 tables!
Another note is: I'd suggest to decrease the number of tables at least to 3 :)



  1. 注册
  2. ItemType
  3. ItemsCollection











  1. 注册



  1. Registration
Create Table Registration
(
RegID int IDENTITY(1,1) primary key,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,  
Username varchar(50), 
Gender varchar(10),
Home_Address varchar(100),
Office_Address varchar(100), 
City varchar(25), 
State varchar(25), 
Zip varchar(25), 
Contact_No int , 
Email varchar(25)
); 

  • ItemType - 存储
    ItemType = {'Laptop', 'Shoes', 'Book', 'Post', 'Uniform', 'Image', ...}






    etc.

    CREATE TABLE ItemType
    (
    IttID INT IDENTITY(1,1) PRIMARY KEY,
    ItemType VARCHAR(50) 
    );

  • ItemCollection - 存储有关所有项目的信息

  • ItemCollection - to store information about all items
    CREATE TABLE ItemsCollection
    (
    ItcID INT IDENTITY(1,1) PRIMARY KEY,
    IttID INT FOREIGN KEY REFERENCES ItemType(IttID)
    --other fields 
    );







  • 我不知道你想如何在 ItemsCollection 注册表。并且......创造它有共鸣吗?除非您想存储添加数据的信息。在这种情况下,您需要在每个表中添加 RegID



    最终结论:重新思考数据库设计。







    所以...将 RegID 添加到 ItemsCollection 表作为外键,那么你将能够定义哪个项目来自或属于哪个用户等。

    [/ EDIT1]



    [ EDIT2]



    这里你有完整的脚本。区别在于:我在临时表上工作(在变量上 - 类型:表)



    I have no idea how you want to create relationship between ItemsCollection and Registration tables. And... Is there a reson to create it? Unless you want to store information who add data. In this case, you need to add RegID into each table.

    Final conclusion: Re-think the databse design.



    So... Add RegID into ItemsCollection table as a foreign key, then you'll be able to define which Item comes from or belongs to which user, etc.
    [/EDIT1]



    Here you've got complete script. The difference is: i worked on temporary tables (on variables - type: table)

    --Registration  
    DECLARE @Registration TABLE (RegID int IDENTITY(1,1) primary key,
    							FirstName varchar(50) NOT NULL, LastName varchar(50) NOT NULL,  
    							Username varchar(50), Gender varchar(10),
    							Home_Address varchar(100), Office_Address varchar(100), 
    							City varchar(25), [State] varchar(25), Zip varchar(25), 
    							Contact_No int , Email varchar(25))
    --insert data
    INSERT INTO @Registration (FirstName, LastName )
    VALUES('Maciej','Los'), ('Member','10651775')
    
    --ItemType
    DECLARE @ItemType TABLE (IttID INT IDENTITY(1,1) PRIMARY KEY, ItemType VARCHAR(50));
     --insert data
     INSERT INTO @ItemType (ItemType)
     VALUES ('Laptop'), ('Shoes'), ('Book'), ('Post'), ('Uniform'), ('Image')
    
    --ItemsCollection
    DECLARE @ItemsCollection TABLE (ItcID INT IDENTITY(1,1) PRIMARY KEY, IttID INT, RegID INT);
    --insert data
    INSERT INTO @ItemsCollection (IttID ,RegID )
    VALUES(1,1),(2,1),(4,1),(6,1),(1,2),(3,2),(5,2)
    
    
    --display related data
    SELECT IC.ItcID, IC.RegID, RE.FirstName, RE.LastName, IC.IttID, IT.ItemType 
    FROM @ItemsCollection AS IC LEFT JOIN @Registration AS RE ON IC.RegID = RE.RegID 
    	LEFT JOIN @ItemType AS IT ON IC.IttID = IT.IttID 







    结果:




    Result:

    1	1	Maciej	Los		1	Laptop
    2	1	Maciej	Los		2	Shoes
    3	1	Maciej	Los		4	Post
    4	1	Maciej	Los		6	Image
    5	2	Member	10651775	1	Laptop
    6	2	Member	10651775	3	Book
    7	2	Member	10651775	5	Uniform





    如您所见,共有7项:4属于我,3属于你:)

    有关系数据库的力量!

    [/ EDIT2]



    As you can see there are 7 items: 4 belongs to me and 3 belongs to you :)
    There is a power of relational databases!
    [/EDIT2]