Here, we call the SETVAL function which is used under the hood in the ALTER SEQUENCE command above, and set the value to the max ID in the project table, plus one. SELECT SETVAL('project_id_seq', (SELECT MAX(id) + 1 FROM project)) However, the following demonstrates a more dynamic approach, where we can set the new value to be the highest ID in the table, plus one: So now our sequence will restart with a value of 3000, and increment from there. Change the starting value of the sequenceĪLTER SEQUENCE project_id_seq RESTART 3000 Luckily, modifying the sequence to start at a value other than 1 is very straightforward: Our new project_id_seq sequence is going to start at 1, which means there's a good chance the IDs are going to overlap at some point, and your unique constraint on the primary key will fail. ![]() If your project table already has rows in it, each row will already have an ID. Great, right? Well there's one more problem. Now, each time a row is added to the project table, a new ID will be generated. Use it to provide a new value for each project ID The following demonstrates how to create a sequence and use it to provide a new default value for project.id each time a new one is created: The project table has a primary-key called id that you want to 'auto increment' each time a new project is added to the database. Let's assume you just migrated your portfolio website from a MySQL database, and you have a table called project. Or maybe you simply need to modify the next value your sequence provides. Maybe you migrated your data from another engine, such as MySQL, and lost your primary-key sequence (Auto Increment in MySQL). Sometimes you need to add or modify a sequence in PostgreSQL. html Extensions Distributed Locks using Golang and Redis Deploying Go with Docker and Alpine Linux Related Posts Transfer Learning and Retraining Inception/MobileNet with TensorFlow and Docker Docker Stats (Memory, CPU, etc.) in JSON Format Deploying a Jekyll Blog to Amazon S3 Without.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |