How to add an auto-incrementing primary key to an existing table, in PostgreSQL?

(Updated – Thanks to the people who commented) Modern Versions of PostgreSQL Suppose you have a table named test1, to which you want to add an auto-incrementing, primary-key id (surrogate) column. The following command should be sufficient in recent versions of PostgreSQL: ALTER TABLE test1 ADD COLUMN id SERIAL PRIMARY KEY; Older Versions of PostgreSQL … Read more

ERROR: permission denied for sequence cities_id_seq using Postgres

Since PostgreSQL 8.2 you have to use: GRANT USAGE, SELECT ON SEQUENCE cities_id_seq TO www; GRANT USAGE – For sequences, this privilege allows the use of the currval and nextval functions. Also as pointed out by @epic_fil in the comments you can grant permissions to all the sequences in the schema with: GRANT USAGE, SELECT … Read more

Get current AUTO_INCREMENT value for any table

You can get all of the table data by using this query: SHOW TABLE STATUS FROM `DatabaseName` WHERE `name` LIKE ‘TableName’ ; You can get exactly this information by using this query: SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘DatabaseName’ AND TABLE_NAME = ‘TableName’;

Get the new record primary key ID from MySQL insert query?

You need to use the LAST_INSERT_ID() function: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id Eg: INSERT INTO table_name (col1, col2,…) VALUES (‘val1’, ‘val2’…); SELECT LAST_INSERT_ID(); This will get you back the PRIMARY KEY value of the last row that you inserted: The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned … Read more

How to create id with AUTO_INCREMENT on Oracle?

There is no such thing as “auto_increment” or “identity” columns in Oracle as of Oracle 11g. However, you can model it easily with a sequence and a trigger: Table definition: CREATE TABLE departments ( ID NUMBER(10) NOT NULL, DESCRIPTION VARCHAR2(50) NOT NULL); ALTER TABLE departments ADD ( CONSTRAINT dept_pk PRIMARY KEY (ID)); CREATE SEQUENCE dept_seq … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)