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)
);
Insert random data into a table that has foreign key
Domanda
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!
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow