将oracle表数据从两个表复制到另一个表

时间:2021-07-17 16:22:23

标签: sql oracle oracle11g oracle-xe

这是我的表格:

<块引用>

创建表国家( country_id number(5) 主键, 国家 varchar2(36) );

    create table city(
    city_id number(5) primary key,
    country_id number(5) constraint city_country_fk references country(country_id) NOT NULL,
    city varchar2(36)
    );

SQL> desc airportdemodata;
 Name                    Null?    Type
 ----------------------------------------- -------- ----------------------------
 AIRPORT_ID                  NOT NULL NUMBER(5)
 IATA_CODE                        VARCHAR2(3)
 CITY                         VARCHAR2(36)
 COUNTRY                      VARCHAR2(36)
 AIRPORT                      VARCHAR2(58)

我将机场演示数据中的国家/地区插入到国家/地区表中,如下所示:

INSERT INTO country (country)
SELECT unique(country) from airportdemodata;

效果很好。
现在我试图通过将 airportdemodata.country 与 country.country 匹配来将 airportdemodata.city 复制到 city.city(country_id,city)。我试过这样:

SQL> SELECT a.unique(city), b.country_id FROM airportdemodata a, country b where a.country=b.country;
SELECT a.unique(city), b.country_id FROM airportdemodata a, country b where a.country=b.country
         *
ERROR at line 1:
ORA-01747: invalid user.table.column, table.column, or column specification

1 个答案:

答案 0 :(得分:0)

对;发明你自己的语法通常会导致你所经历的结果。您是要使用 distinct 吗?

SELECT DISTINCT a.city,
                b.country_id
FROM airportdemodata a
JOIN country b ON a.country = b.country;