Re: How do I enabled Windows 10 to be able to run PSQL etc
Steve Midgley <[email protected]> Thu, 12 Sep 2019 13:07:39 +0100
| Newsgroups | gmane.comp.db.postgresql.sql |
|---|---|
| Message-ID | <CAJexoSLb0s26Nb+EjQH8fB8OUwF49ACTbmJQJ2pMZZ4WiO_dGA@mail.gmail.com> |
--000000000000ba9ffd059259fc56 Content-Type: text/plain; charset="UTF-8" On Thu, Sep 12, 2019 at 11:50 AM Karen Goh <[email protected]> wrote: > Anyway, now my problem is that i get a > > org.postgresql.util.PSQLException: ERROR: null value in column "parent_id" > violates not-null constraint > > I'd like to know since I have already made parent_id a primary key, do I > need to put a idenity as ALWAYS and increment by 1 ? > > cos another table I had used pgAdmin4 to define it as identity, ALWAYS, > increment 1 and it works well.... > > so what is the difference between identity and PRIMARY KEY ? > > and if I follow the other table definition for id and put in the ALWAYS, > it will give me another problem...that the generated_id doesn't get > inserted even though it should not > Hi Karen, Please bottom post on this forum - it's uncommon these days, but the custom on this email list to post replies at the bottom of the email. Regarding your question about nulls and primary keys -- yes you want to define, where it makes sense, to use autoincrementing primary keys, if you want the system to manage your keys. Note that autoincrementing IDs, if exposed to the public via the web or similar, could create some security issues. But in many cases it makes a lot of sense to use them. To create a primary key that autoincrements, use the "serial" or "bigserial" datatype. Combined with a primary key (that enforces "not null" among other things), your create table would look like this: CREATE TABLE sample ( id SERIAL PRIMARY KEY,); I have not tested it, but I'd guess you should be able to alter a table that has an integer primary key and convert it to a serial. I hope that's helpful! Steve --000000000000ba9ffd059259fc56 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail= _attr">On Thu, Sep 12, 2019 at 11:50 AM Karen Goh <<a href=3D"mailto:kar= [email protected]">[email protected]</a>> wrote:<br></div><blockquote= class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px so= lid rgb(204,204,204);padding-left:1ex"><div> Anyway, now my = problem is that i get a <br><br>org.postgresql.util.PSQLException: ERROR: n= ull value in column "parent_id" violates not-null constraint<br><= br>I'd like to know since I have already made parent_id a primary key, = do I need to put a idenity as ALWAYS and increment by 1 ?<br><br>cos anothe= r table I had used pgAdmin4 to define it as identity, ALWAYS, increment 1 a= nd it works well....<br><br>so what is the difference between identity and = PRIMARY KEY ?<br><br>and if I follow the other table definition for id and = put in the ALWAYS, it will give me another problem...that the generated_id = doesn't get inserted even though it should not </div></blockquote><div>= <br></div><div>Hi Karen,</div><div><br></div><div>Please bottom post on thi= s forum - it's uncommon these days, but the custom on this email list t= o post replies at the bottom of the email.</div><div><br></div><div>Regardi= ng your question about nulls and primary keys -- yes you want to define, wh= ere it makes sense, to use autoincrementing primary keys, if you want the s= ystem to manage your keys. Note that autoincrementing IDs, if exposed to th= e public via the web or similar, could create some security issues.</div><d= iv><br></div><div>But in many cases it makes a lot of sense to use them. To= create a primary key that autoincrements, use the "serial" or &q= uot;bigserial" datatype. Combined with a primary key (that enforces &q= uot;not null" among other things), your create table would look like t= his:</div><div> <div class=3D"gmail-language-sql gmail-highlighter-rouge"><div class=3D"gma= il-highlight"><pre class=3D"gmail-highlight"><code><span class=3D"gmail-k">= CREATE</span> <span class=3D"gmail-k">TABLE</span> sample <span class=3D"gm= ail-p">(</span> <span class=3D"gmail-n">id</span> <span class=3D"gmail-n">SE= RIAL</span> <span class=3D"gmail-k">PRIMARY</span> <span class=3D"gmail-k">= KEY</span><span class=3D"gmail-p">,</span> <span class=3D"gmail-p">);</span><br><br></code></pre><pre class=3D"gmail-h= ighlight"><code><span style=3D"font-family:arial,sans-serif">I have not tes= ted it, but I'd guess you should be able to alter a table that has an i= nteger primary key and convert it to a serial. I hope that's helpful! S= teve</span><br></code></pre></div></div> </div></div></div> --000000000000ba9ffd059259fc56--