开发者

How to add column in table 1 to be type of table 2?

开发者 https://www.devze.com 2023-01-28 20:26 出处:网络
I define table 1. Now i want to add new table - and one of the column of the second table need开发者_如何学运维 to be the first table -

I define table 1. Now i want to add new table - and one of the column of the second table need开发者_如何学运维 to be the first table -

How can i do it ?


If you want one column of a table to reference another table, then your best bet is probably to go read up on the concept of keys, primary keys, and foreign keys in database design.

For example, in a database of companies and employees, you might have 2 tables like this:

Company (c_id, name, city)
Employee (e_id, c_id, name)

In the Company table, c_id would be a primary key. In the Employee table, c_id would be a foreign key referencing Company. This would allow you to do queries like

SELECT E.name
FROM Employee as E, Company as C
WHERE E.c_id = C.c_id AND C.name = "IBM"

which would return the names of employees who work at IBM.

Links:

http://en.wikipedia.org/wiki/Primary_key

http://en.wikipedia.org/wiki/Foreign_key


Why cant you go for a foreign relationship.

for eg : Table1 (ID,ForeignKeyId, other columns)
         Table2 (ID,other columns)

ForigenKeyId will be the primary key of Table2


If you really need table as a column, you should read http://msdn.microsoft.com/en-us/library/ms175010.aspx for a solution. However, this is highly unlikely that you really need table column datatype, as it is primarily used for temporary storage.

If you don't know primary-foreign keys relationships stuff, you should take some time learning relational databases or have someone design a database schema for you based on business entities and your application needs. Otherwise you will end up with a design which is completely unmaintainable and mid term it will backfire on you.

If you need a quick reading on PK/FK topic, please read http://www.functionx.com/sqlserver2005/Lesson13.htm. It should give you a knowledge required to tackle with this particular issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消