简体   繁体   English

SQL从多个表插入行

[英]SQL inserting rows from multiple tables

I have got an assignment. 我有一个任务。 We have been given a table, MAIN_TABLE, which has a column patient_id as foreign key. 我们得到了一个表MAIN_TABLE,该表具有一列Patient_id作为外键。

I need to make a separate table named patient which has patient_id as a primary key along with some other attributes such as name and address. 我需要创建一个名为Patient的单独表,该表具有Patient_id作为主键以及一些其他属性,例如名称和地址。

I did successfully create schema of this table. 我确实成功创建了该表的架构。 Now there is a serious problem I am facing. 现在,我面临着一个严重的问题。 After creating this table I used insert statement to insert values for name and address from a dummy table. 创建此表后,我使用insert语句从虚拟表中插入名称和地址的值。

Till this point everything works fine. 至此,一切正常。 However, the column patient_id is still empty rather I have set it to 0 by default. 但是,patient_id列仍然为空,而我默认将其设置为0。

Now the problem is that I need to get values into this column, patient_id, from the patient_id column of MAIN TABLE. 现在的问题是,我需要从MAIN TABLE的Patient_id列中获取值到此列Patient_id中。

I can't figure out how do I do this? 我不知道该怎么做? I did try to use: 我确实尝试使用:

UPDATE patient
SET patient_id=(select id from MAIN_TABLE) 

BUT this gives me error that multiple rows returned which does make sense but what condition do I put in where clause then? 但是这给了我一个错误,那就是多行返回确实有意义,但是那我在where子句中放置了什么条件呢?

That sounds strange. 听起来很奇怪。 How can there be a table MAIN_TABLE with a foreign key patient_id but the master table patient does not exist. 哪有一个表MAIN_TABLE与外键patient_id但主表patient不存在。 Where do that patient_id s in MAIN_TABLE come from? patient_id中的MAIN_TABLE来自何处?

I suggest not to insert your data from a dummy table alone and then try to update it. 我建议不要单独从虚拟表中插入数据,然后尝试对其进行更新。 But insert it with both - the MAIN_TABLE and the dummy table joined. 但同时插入两者MAIN_TABLE和虚拟表已连接。 If you can not join them. 如果您不能加入他们。 You would also not be able during the update. 您也将无法在更新过程中。

So since i think they have no connected primary/foreign keys the only way to join them is using a good business key. 因此,由于我认为它们没有连接的主/外键,因此加入它们的唯一方法是使用良好的业务密钥。 Do you have a good business key? 你有一个好的商务钥匙吗? You are talking about persons. 您在谈论人。 So First Name, Last Name, Birth Day, Address often is good enough. 因此,名字,姓氏,生日,住址通常足够好。 But you have to think about it. 但是你必须考虑一下。

With your given data I can only give you some kind of meta insert statement. 对于您给定的数据,我只能给您某种元插入语句。 But you will get the point. 但是你会明白的。

Example: 例:

insert into patient (col1, col2, col3)
select 
  a.colA, 
  a.colF,
  b.colX
from 
  dummy_table a
    inner join MAIN_TABLE b on a.colN=b.colA and a.colM=b.colB

And : If patient_id is your primary key in patient you should ensure that it is even not possible to have duplicate values or null in this column. 并且 :如果patient_idpatient的主键,则应确保在此列中甚至不可能有重复的值或null And you should use constraints to ensure your data integrity. 并且您应该使用约束来确保数据完整性。

http://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm http://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM