문제

I have a table "User" (idUser, name, departmentId) and another table "Department" (idDepartment, name).

I would like to insert random data into table User, but it would be necessary to consider only the departments previously inserted.

I think it would be something like:

INSERT INTO User (idUser, name, departmentId)
VALUES (seq_user.nextVal, 'random name', FK_RANDOM_DEPARTMENT);

How can I generate this 'random name' and only use departments that I have already inserted?

Thanks in advance!

도움이 되었습니까?

해결책

INSERT INTO User 
(idUser, name, departmentId)
VALUES 
(seq_user.nextVal,
 DBMS_RANDOM.STRING('L', 20), /*20 is a number of characters in a string*/
 (SELECT * FROM (SELECT idDepartment FROM Department ORDER BY DBMS_RANDOM.VALUE) WHERE ROWNUM = 1)
);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top