{"id":2201,"date":"2012-02-16T18:16:38","date_gmt":"2012-02-16T18:16:38","guid":{"rendered":"https:\/\/www.acugis.com\/blogs\/?p=2201"},"modified":"2015-05-11T18:19:39","modified_gmt":"2015-05-11T18:19:39","slug":"auto-increment-in-phppgadmin","status":"publish","type":"post","link":"https:\/\/acugis.com\/blogs\/auto-increment-in-phppgadmin\/","title":{"rendered":"Auto Increment in PhpPgAdmin"},"content":{"rendered":"<p>Unlike PhpMySQL, PhpPgAdmin does not feature a nice drop-down to auto increment on a column.<\/p>\n<p>This post will demonstrate how to auto increment on a column in PostgreSQL using either the PhpPgAdmin SQL Editor as well as how to make use of PhpPgAdmin GUI to do some of the work for us.<\/p>\n<p>The steps for using the PhpPgAdmin SQL Editor can be applied to using the command line (psql) as well.<\/p>\n<p>In our example we will create a table, Managers.<\/p>\n<p>Our table will have three columns: mgr_id, mgr_name, and mgr_email.<\/p>\n<p>For each insertion of a new Manager entry, we want to auto increment our primary key, mgr_id, by 1.<\/p>\n<p>Let&#8217;s start by using the command line. You can use below for the PhpPgAdmin SQL Editor as well.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"color: #990000; text-align: center;\"><span style=\"font-size: small;\"><strong>Step 1: Create a Sequence<\/strong><\/span><\/div>\n<pre class=\"js\">testdb=&gt; CREATE SEQUENCE mgr_id_seq;\r\nCREATE SEQUENCE\r\n<\/pre>\n<div style=\"color: #990000; text-align: center;\"><span style=\"font-size: small;\"><strong>Step 2. Create Table and Set the Column Default<\/strong><\/span><\/div>\n<p>We now create the Manager table.<\/p>\n<p>For mgr_id, set as PRIMARY KEY and set the DEFAULT to NEXTVAL(&#8216;mgr_id_seq&#8217;) as shown below.<\/p>\n<p>This will increment our sequence by 1 each time a new Manager entry is inserted.<\/p>\n<pre class=\"js\">testdb=&gt; CREATE TABLE managers(\r\ntestdb(&gt; mgr_id INTEGER PRIMARY KEY DEFAULT NEXTVAL('mgr_id_seq'),\r\ntestdb(&gt; mgr_name VARCHAR(50),\r\ntestdb(&gt; mgr_email VARCHAR(50)\r\ntestdb(&gt; );\r\n\r\nNOTICE:  CREATE TABLE \/ PRIMARY KEY will create implicit index \"managers_pkey\" for table \"managers\"\r\nCREATE TABLE\r\ntestdb=&gt;\r\n<\/pre>\n<div style=\"color: #990000; text-align: center;\"><span style=\"font-size: small;\"><strong>Step 3. Insert Data.<\/strong><\/span><\/div>\n<pre class=\"js\">testdb=&gt; INSERT INTO managers (mgr_name, mgr_email) VALUES('bob smith', 'bob@smith.com');\r\nINSERT 0 1\r\ntestdb=&gt; INSERT INTO managers (mgr_name, mgr_email) VALUES('tom jones', 'tom@jones.com');\r\nINSERT 0 1\r\n<\/pre>\n<div style=\"color: #990000; text-align: center;\"><span style=\"font-size: small;\"><strong>Step 4. Select on Table to View Sequence.<\/strong><\/span><\/div>\n<pre class=\"js\">testdb=&gt; select * from managers;\r\n mgr_id | mgr_name  |   mgr_email\r\n--------+-----------+---------------\r\n      1 | bob smith | bob@smith.com\r\n      2 | tom jones | tom@jones.com\r\n(2 rows)\r\n\r\ntestdb=&gt;\r\n\r\n<\/pre>\n<p>As we an see from above, the increment begins at 1 and increments by 1 by default.<\/p>\n<p>Now, let&#8217;s take a look at how we can do this using some of the PhpPgAdmin GUI tools.<\/p>\n<div style=\"text-align: center;\">\n<p><strong style=\"color: #990000; font-size: small;\">Using the PhpPgAdmin GUI Tools<\/strong><\/p>\n<\/div>\n<div style=\"text-align: center;\">\n<p>&nbsp;<\/p>\n<p><strong style=\"font-size: small; color: #990000;\">Step 1: Create Sequence in PhpPgAdmin<\/strong><\/p>\n<\/div>\n<p>Create your sequence using by selecting the Sequence node in PhpPgAdmin and then click &#8220;Create Sequence&#8221;<\/p>\n<p>Enter the sequence name as shown below.<\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"http:\/\/www.davidghedini.com\/images\/PgPhpAdminPost\/CreatePgSeq.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.davidghedini.com\/images\/PgPhpAdminPost\/CreatePgSeq.jpg\" alt=\"\" width=\"549\" height=\"275\" border=\"0\" \/><\/a><\/div>\n<p>With your sequence created, you can now set the DEFAULT value for the mgr_id column to nextval(&#8216;mgr_id_seq&#8217;) in the table creation GUI as shown below:<\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"http:\/\/www.davidghedini.com\/images\/PgPhpAdminPost\/PhpPgAdminAddSeq.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.davidghedini.com\/images\/PgPhpAdminPost\/PhpPgAdminAddSeq.jpg\" alt=\"\" width=\"598\" height=\"214\" border=\"0\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p>More on Sequences in PostgreSQL:<\/p>\n<p><a href=\"http:\/\/www.postgresql.org\/docs\/9.0\/static\/sql-createsequence.html\" target=\"_&quot;blank&quot;\">http:\/\/www.postgresql.org\/docs\/9.0\/static\/sql-createsequence.html<\/a><\/p>\n<p><a href=\"http:\/\/www.postgresql.org\/docs\/9.1\/static\/functions-sequence.html\" target=\"_&quot;blank&quot;\">http:\/\/www.postgresql.org\/docs\/9.1\/static\/functions-sequence.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unlike PhpMySQL, PhpPgAdmin does not feature a nice drop-down to auto increment on a column. This post will demonstrate how to auto increment on a column in PostgreSQL using either the PhpPgAdmin SQL Editor as well as how to make use of PhpPgAdmin GUI to do some of the work for us. The steps for &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/acugis.com\/blogs\/auto-increment-in-phppgadmin\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Auto Increment in PhpPgAdmin&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[31,36,35],"tags":[],"class_list":["post-2201","post","type-post","status-publish","format-standard","hentry","category-postgis","category-postgresql","category-tutorials"],"_links":{"self":[{"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/posts\/2201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/comments?post=2201"}],"version-history":[{"count":1,"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/posts\/2201\/revisions"}],"predecessor-version":[{"id":2202,"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/posts\/2201\/revisions\/2202"}],"wp:attachment":[{"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/media?parent=2201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/categories?post=2201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/acugis.com\/blogs\/wp-json\/wp\/v2\/tags?post=2201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}