How to insert a primary/foreign key row in PostgreSQL -
i'm populating database in postgresql newspaper online. doubt lies on how insert value table attribute both primary , foreign key.
in context, admin first person ever register account. idadmin = ida = 1:
create table autenticado ( ida serial not null , login varchar(45) not null, password varchar(45) not null, email varchar(40) not null, primary key (ida) ); create table admin ( idadmin int not null references autenticado (ida), primary key (idadmin) ); it logical insert values 'admin' tried below, although not possible considering 'idadmin' primary key (and foreign key).
insert autenticado values ('john','adadfsfsdfs', 'john@random.com') insert admin values (1) is there way register first user create account (ida = 1) admin (idadmin = ida = 1) ?
although not possible considering 'idadmin' primary key (and foreign key).
so what?
if fix first query list columns , use returning clause auto-generated value serial id, works:
insert autenticado(login,password,email) values ('john','adadfsfsdfs', 'john@random.com') returning ida; result:
ida ----- 1 (1 row)
second query:
insert admin values(1); select * admin; result:
idadmin --------- 1
Comments
Post a Comment