im trying to create 2 column and set auto_increment i already create 1 column and set auto_increment and 2nd column showing error to set auto_increment
how can i create 2 column and set auto_increment
please help me to fix this issue
thanks
SQL query:
ALTER TABLE `admission` CHANGE `rollno` `rollno` INT( 8 ) NOT NULL AUTO_INCREMENT
MySQL said:
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
Just like the error says you can only have one auto_increment column. It seems to me that since the values of the two columns would most likely always be identical you can just use the one column. If that's not the case then it's up to you to either iterate that column manually , or you can create an INSERT trigger on the table that sets the new column either to the other auto_increment's value or to whatever value it's supposed to be (max + 1)?
Related
Code
DB
This is the error I get when I try to add data in the db
The problem is in your table produs2: the id_produs field doesn't have the AUTO_INCREMENT attribute. Then, when you try to insert with id=null, no automatic numeric id is autogenerated.
To fix this, run this query in PHPMyAdmin:
ALTER TABLE `produs2` ADD `id_produs` INT NOT NULL AUTO_INCREMENT PRIMARY KEY
Make id_produs auto_increment and in line 76 in inserare.php neither include id_produs nor "null" in the insert statement because it is trying to insert null value to a primary key field.
I am getting the error
Error Code: 1062. Duplicate entry '0' for key 1
when inserting a new value to a table where the primary key is set to auto_increment. This column is set to int(11) data type and when you select the max value of the column it is 16000. (Note: I can manually add a new row in with a random high number fine, the issue is purely with auto_increment).
If I do this:
SHOW TABLE STATUS FROM database LIKE 'tablename'
The value of auto_increment is set to NULL.
Strangely enough I tried to replicate the table, with the plan to just copy all the existing data over and when I recreated the same table with a new name and there is no data in the table the auto_increment next value is also set to null.
This particular table has a lot of columns so I wonder if maybe I have hit on another limit of some sort?
I resolved the issue. For some reason the column was no longer set to auto-increment. It had worked fine for the previous 13,000 records so I am not sure why the flag had suddenly been removed.
When I copy all tables from one database and paste them to other database, how to set all auto_increments from these tables to start value? Is it possible to make a php script for such thing?
Try this one:
ALTER TABLE test AUTO_INCREMENT = 'startvalue'
Condition:
if table not empty start value must greater then MAX(AUTO_INCREMENT)
If AUTO_INCREMENT column is primary key and if you re-sort the AUTO_INCREMENT column data, it may be problem in relationship with other tables. Instead of re-sorting your AUTO_INCREMENT data, you can import as follows:
You can disable the AUTO_INCREMENT on the new table as follows:
ALTER TABLE [your_table] CHANGE auto_increment_column_name auto_increment_column_name INT(11) NOT NULL
After inserting, you can enable AUTO_INCREMENT and set it again as follows:
ALTER TABLE [your_table] AUTO_INCREMENT = [Maximum value]
Alternatively, you have to update all dependent tables foreign key values using a migration script.
I have a database and i am putting data inside. I have one node called key , which is the primary key and other nodes. Now when i put data on my table , i put data in all the nodes except this key node. How do i make it automatically to increase from 0 when i have a new entry? If i run a script to put something in the database , i can see that the nodes have correctly all the data and the key takes the value 0. When i run my script again i get the error :
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY''
From what i understand , because i dont pass anything to this node , the database "thinks" i am passing again a 0 argument so i have the error. How can i fix it to auto increment every time i have a new entry?
You need to set the field as autoincrement. You would need to run an ALTER TABLE statement like this:
ALTER TABLE table_name
MODIFY `key` MEDIUMINT NOT NULL AUTO_INCREMENT
id MEDIUMINT NOT NULL AUTO_INCREMENT
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
Start it at 1, not 0.
So the first record is id number 1.
What database are you using?
in postgres you register a number sequence.
In my sql you just use AUTO_INCREMENT when specifying the column attributes
With MySQL, you would declare the table with the AUTO_INCREMENT keyword when defining your table to achieve this behavior
Example:
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
When you INSERT data, you do not specify a value for the primary key. MySQL will automatically use the next available integer value for the key.
UPDATE
You can change this directly within PHP My Admin: go to the table in question and then
Operations->Table Options->Auto-Increment
I asked this question a little earlier today but am not sure as to how clear I was.
I have a MySQL column filled with ordered numbers 1-56. These numbers were generated by my PHP script, not by auto_increment.
What I'd like to do is make this column auto_incrementing after the PHP script sets the proper numbers. The PHP script works hand in hand with a jQuery interface that allows me to reorder a list of items using jQuery's UI plugin.
Once I decide what order I'd like the entries in, I'd like for the column to be set to auto increment, such that if i were to insert a new entry, it would recognize the highest number already existing in the column and set its own id number to be one higher than what's already existing.
Does anyone have any suggestions on how to approach this scenario?
I'd suggest creating the table with your auto_increment already in place. You can specify a value for the auto_inc column, and mysql will use it, and still the next insert to specify a NULL or 0 value for the auto_inc column will magically get $highest + 1 assigned to it.
example:
mysql> create table foobar (i int auto_increment primary key);
mysql> insert into foobar values (10),(25);
mysql> insert into foobar values (null);
mysql> select * from foobar;
# returns 10,25,26
You can switch it to MySQL's auto_increment implementation, but it'll take 3 queries to do it:
a) ALTER TABLE to add the auto_increment to the field in question
b) SELECT MAX(id) + 1 to find out what you need to set the ID to
c) ALTER TABLE table AUTO_INCREMENT =result from (b)
MySQL considers altering the AUTO_INCREMENT value a table-level action, so you can't do it in (a), and it doesn't allow you to do MAX(id) in (c), so 3 queries.
You can change that with a query, issued through php, using the mysql console interface or (easiest) using phpmyadmin.
ALTER TABLE table_name CHANGE old_column_name new_column_name column_definition;
ALTER TABLE table_name AUTO_INCREMENT = highest_current_index + 1
column_definiton:
old_column_definition AUTO_INCREMENT
More info:
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
http://dev.mysql.com/doc/refman/5.1/en/create-table.html
EDIT
Always use mysql_insert_id or the appropiate function of your abstraction layer to get the last created id, as LAST_INSERT_ID may lead to wrong results.
No, stop it. This isn't the point of auto_increment. If you aren't going to make them ordered by the id then don't make them auto_increment, just add a column onto the end of the table for ordering and enjoy the added flexibility it gives you. It seems like you're trying to pack two different sets of information into one column and it's really only going to bite you in the ass despite all the well-meaning people in this thread telling you how to go about shooting yourself in the foot.
In MySQL you can set a custom value for an auto_increment field. MySQL will then use the highest auto_increment column value for new rows, essentially MAX(id)+1. This means you can effectively reserve a range of IDs for custom use. For instance:
CREATE TABLE mytable (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
col1 VARCHAR(256)
);
ALTER TABLE mytable AUTO_INCREMENT = 5001;
In this schema all ids < 5001 are reserved for use by your system. So, your PHP script can auto-generate values:
for ($i=1; $i<=56; $i++)
mysql_query("INSERT INTO mytable SET id = $i, col1= 'whatevers'");
New entries will use the non-reserved range by not specifying id or setting it to null:
INSERT INTO mytable SET id = NULL, col1 = 'whatevers2';
-- The id of the new row will be 5001
Reserving a range like this is key - in case you need more than 56 special/system rows in the future.
ALTER TABLE <table name> <column name> NOT NULL AUTO_INCREMENT
More info:
AUTO_INCREMENT Handling in InnoDB
Server SQL Modes