简体   繁体   中英

Restricting distinct keyword on single column in Sql plus

INSERT INTO DEPT(Dept_ID, Dept_Name)
(SELECT DISTINCT department_id, last_name
FROM my_employee);

department_id in my_employee table may contain Duplicate key but Dept_ID is PRIMARY KEY.

error displayed (ORA-00001: unique constraint (ROHITRAJ.SYS_C0013013) violated) .

Above code has been changed as shown below, but it still doesn't work:

INSERT INTO DEPT(Dept_ID, Dept_Name)
(SELECT (DISTINCT department_id), last_name
FROM my_employee);

Is it possible to insert data into a column that contains unique key from a column containing duplicates?

Is it possible to insert data into a column that contains unique key from a column containing duplicates?

Your Primary Key field must contain unique values only. It will throw an exception if you are trying to insert duplicate data on it.

If you want create another column that will handle the department_id and just store an autonumber on the Dept_ID .

Try this:

INSERT INTO DEPT(Dept_ID, Department_id, Dept_Name)
(SELECT rownum, department_id, last_name
FROM my_employee);

Note: (rownum only works for Oracle it means row number.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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