auto-increment

SQL Create Table that Auto-Increments one column based on another column

大城市里の小女人 提交于 2019-12-24 21:50:28
问题 I'm still a beginner with SQL so please bear with me. I need to create a table that stores information from a form. Each form has an ID associated with it that has two parts. The first part is MMYY and the second part is a 5 digit auto-incrementing integer. An example ID is 0714-00001. And every time the month changes, the 5 digit numbering should start over. So, I want the columns to look something like this: Order_No. | Order_ID ---------------------------- 0714 | 0001 0714 | 0002 0714 |

Make one ID with auto_increment depending on another ID - possible?

痴心易碎 提交于 2019-12-24 18:34:59
问题 I want to make a small ticket-system for a project server which has several projects. So far the TicketID will be counted globally, i.e. there is a project A and a ticket with TicketID 1 and another project B and the ticket for this project will get TicketID 2 - like this: (TicketID, ProjectID) (1, 1) (2, 1) (3, 1) (4, 2) (5, 2) (6, 3) But I think it would be better to count the TicketID depending on the ProjectID such as: (TicketID, ProjectID) (1, 1) (2, 1) (3, 1) (1, 2) (2, 2) (1, 3) Here

Primary Key Value Not Incrementing Correctly

家住魔仙堡 提交于 2019-12-24 17:10:23
问题 I have a django model that started to have its ids increment strangely. The postgres definition of the column (generated from Django Model): id | integer | not null default nextval('billing_invoice_id_seq'::regclass) tpg=> SELECT MAX(id) FROM billing_invoice; max ------- 16260 Then I created a new record through the django admin: tpg=> SELECT MAX(id) FROM billing_invoice; max ------- 17223 tpg=> SELECT nextval('billing_invoice_id_seq'); nextval --------- 17224 Then I created a new record

Get max id of all sequences in PostgreSQL

孤者浪人 提交于 2019-12-24 16:28:43
问题 We have a monitor on our databases to check for ids approaching max-int or max-bigint. We just moved from MySQL, and I'm struggling to get a similar check working on PostgreSQL. I'm hoping someone can help. Here's the query in MySQL SELECT table_name, auto_increment FROM information_schema.tables WHERE table_schema = DATABASE(); I'm trying to get the same results from PostgreSQL. We found a way to do this with a bunch of calls to the database, checking each table individually. I'd like to

mysql not unique auto increment, primary key two fields

假如想象 提交于 2019-12-24 05:43:34
问题 I want to create a table structure like this in MySQL: id | area | name ----+----------+------------ 1 | name-1 | test | | 1 | name-1 | value2 | | 2 | name-2 | test-value | | 3 | name-3 | test | | 3 | name-3 | test ie. the primary key will be: primary_key( id, area ) and the id will be auto_increment , but i only want the id to increment for every new unique area is this possible? 回答1: What you want is not possible. You want id and area to be the primary key but in your example they are not

Alter table or reset auto-increment using CDbMigration

非 Y 不嫁゛ 提交于 2019-12-24 05:21:50
问题 How can I alter table or reset the auto-increment of a field in Yii 1.x, using CDbMigration? I found alterColumn method, as good as createTable, dropTable, renameTable and truncateTable methods, but either I'm blind or there isn't anything for altering table or resetting the auto-increment of particular column or field. 回答1: You can use execute() as Yii defines it: Executes a SQL statement. This method executes the specified SQL statement using dbConnection. So, $this->execute("ALTER TABLE

Alter table or reset auto-increment using CDbMigration

柔情痞子 提交于 2019-12-24 05:21:27
问题 How can I alter table or reset the auto-increment of a field in Yii 1.x, using CDbMigration? I found alterColumn method, as good as createTable, dropTable, renameTable and truncateTable methods, but either I'm blind or there isn't anything for altering table or resetting the auto-increment of particular column or field. 回答1: You can use execute() as Yii defines it: Executes a SQL statement. This method executes the specified SQL statement using dbConnection. So, $this->execute("ALTER TABLE

Use current auto_increment value as basis for another field in the same INSERT query - is this possible in MySQL?

我只是一个虾纸丫 提交于 2019-12-24 03:53:19
问题 In my table (MySQL) I have these fields: id - primary, auto_increment hash - base 36 representation of id I'd like to populate both fields at once, using a stored procedure to compute the base 36 equivalent of the value id receives on INSERT. Something like this: INSERT into `urls` (`id`,`hash`) VALUES (NULL,base36(`id`)); Obviously this doesn't work, but I hope it illustrates what I'm trying to achieve. Is this possible, and if so, how? Note: I want to use only one query, so last_insert_id

PostgreSQL increase a table's sequence with one query

亡梦爱人 提交于 2019-12-24 03:24:06
问题 I want to unite the following two queries into one: SELECT pg_get_serial_sequence('purchase_orders', 'id'); SELECT setval('purchase_orders_id_seq', 30000); But if I place the upper SELECT into the setval's first parameter I get: SELECT setval(SELECT pg_get_serial_sequence('purchase_orders', 'id'), 30000); ERROR: syntax error at or near "SELECT" SQL state: 42601 Character: 15 How can I pass on the select's result ("purchase_orders_id_seq") for setval? EDIT: The reason for this is that; I want

Reset auto increment sequence pl-sql

▼魔方 西西 提交于 2019-12-24 02:21:42
问题 How can i reset auto increment primary key ? I have a doc_id_seq and a doc_pk_trg trigger like that: CREATE SEQUENCE doc_id_seq START WITH 1; CREATE OR REPLACE TRIGGER doc_pk_trg BEFORE INSERT ON TFIDF FOR EACH ROW BEGIN IF :NEW.doc_id IS NULL THEN SELECT doc_id_seq.NEXTVAL INTO :NEW.doc_id FROM DUAL; END IF; END; / I want to learn reset the sequence . How can I do this ? 回答1: You could use the ALTER SEQUENCE syntax. Tom Kyte explains how to do exactly this here: http://asktom.oracle.com/pls