Link to home
Start Free TrialLog in
Avatar of JianJunShen
JianJunShen

asked on

Oracle SQL: Insert select

Can someone understand following schema: Why two first two will fail? In the will succeed case, why one could select from one column zip_code?

===================================================
CREATE TABLE zip_new (
zip_code     VARCHAR2(5) NOT NULL,
state_abbrev VARCHAR2(2) NOT NULL,
city_name    VARCHAR2(30));

-- will fail
INSERT INTO zip_new
SELECT *
FROM zip_code;

-- will fail for a different reason
INSERT INTO zip_new
SELECT zip_code, state_abbrev, city_name
FROM zip_code;

-- will succeed
INSERT INTO zip_new
SELECT zip_code, state_abbrev, city_name
FROM zip_code
WHERE zip_code IS NOT NULL
AND state_abbrev IS NOT NULL;

SELECT * FROM zip_new;
ASKER CERTIFIED SOLUTION
Avatar of GGuzdziol
GGuzdziol
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JianJunShen
JianJunShen

ASKER

FROM zip_code

What this mean in successful case? Can we omit?

INSERT INTO zip_new
SELECT zip_code, state_abbrev, city_name
WHERE zip_code IS NOT NULL
AND state_abbrev IS NOT NULL;
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial