I have an existing product table which is already populated with 120 records and I have tried below SQL query:
ALTER TABLE product ADD product_id
INT PRIMARY KEY AUTO_INCREMENT
But it's given me the error:
Error: #1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
Here is work around
create a simple column with autoincrement
browse the table and again make this newly added column as primary key
You are done !
:) Thanks to Guy who is not in Stackoverflow
Friend
If you try to do this in MSSQL then your script will be this.
suppose your table is below
create table product
(
name varchar(20),
product_type varchar(20)
)
and suppose you entered some record in this table after that you want to add a column that name product_id with auto increment and primary key. Then you use this script that is below
alter table product add product_ID int primary key identity(1,1)
suppose this will be help full for you.
There can only be one auto increment field per table. But, you could have a calculated field based on the auto increment field. Or, you could have an int field where you manage the sequence by front end code or by a trigger. And also you could use a sequence in SQL Server.
CREATE SEQUENCE MySequence START WITH 100;
CREATE TABLE MyTable
(
RealIdentity INT IDENTITY(1,1),
RandomCol NVARCHAR(100),
FakeIdentity INT DEFAULT NEXT VALUE FOR MySequence
);
UPDATE - MySQL Way
I just noticed that your question is taged for MySQL. Above answer is for MS SQL. Here's how you'd do the same in MySQL.
How do I create a sequence in MySQL?
MySQL equivalent of Oracle's SEQUENCE.NEXTVAL
Related
I need that the values for a ColumnID, which is a Primary Key, to start at 100 and increment by 5. This condition is asked to be included as a constraint before populating the tables. I already created the tables, I just need to add that constraint. I know I can't use AUTO_INCREMENT because the increase is only by 1. Is there a way to do it in MySQL?
MySQL does not provide any built-in function to create a sequence for a table's rows or columns. But we can generate it via SQL query.
Example:
Let us understand it with the help of the following example. First, we need to create a new table and make sure that there is one column with the AUTO_INCREMENT attribute and that too, as PRIMARY KEY.
Execute the below query to create a table:
CREATE TABLE Insects (
Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Type VARCHAR(30) NOT NULL,
Origin VARCHAR(30) NOT NULL
);
Then you can alter your column to start from another value:
ALTER TABLE Insects AUTO_INCREMENT=100;
You can check it here.
I want to create a column that will auto increment when a new row is inserted.
The table has already data and this data does not need to receive this index, or it can be NULL. I just want to start to increment since now.
It looks simple, but I run in Workbench this:
ALTER TABLE `serra`.`acionamento`
ADD COLUMN `indice` INT NULL AUTO_INCREMENT AFTER `date_insercao`
... and it says
Incorrect table definition; there can be only one auto column and it must be defined as a key
This column really needs to be a primary key?
I found the solution I was looking for...
I was missing an UNIQUE configuration...
ALTER TABLE `serra`.`acionamento`
ADD COLUMN `indice` INT NOT NULL AUTO_INCREMENT AFTER `column`,
ADD UNIQUE INDEX `indice_UNIQUE` (`indice` ASC);
Thanks for the comments
I have a database table with several columns where some of them used to form the primary key together. Now the model changed and a single primary key column, called 'id', gets introduced.
When I alter the table and add the column, the column gets populated in every row with an default value, 0 or NULL, so that it can not be used as a candidate for a new primary key. To achieve that, I have to have a different (numerical) value for 'id' in every row. How can I achieve that, preferably as generic as possible (MySQL, Oracle, SQL Server).
Here is a sample of how to add an identity column that populates as a primary key.
create table #t (vals varchar(100))
insert into #t
values('a'),('b'),('c')
alter table #t
add ID int identity primary key
select * from #t
Results:
vals ID
a 1
b 2
c 3
This is a SQL Server answer. I don't know how the other databases work.
CREATE TABLE Persons (
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
Might be this will be helpful.
The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.
In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.
Tip: To specify that the "ID" column should start at value 10 and increment by 5, change it to IDENTITY(10,5).
To insert a new record into the "Persons" table, we will NOT have to specify a value for the "ID" column (a unique value will be added automatically):
I am entering records in the MySQL DB. Now I want to have a "Serial_Number" field that increements automatically whenever a record is entered into the DB.
I don't want this "Serial_Number" field to be the primary key of the DB.
How can I create this field (with the attributes needed to be set).
I am using "SQL YOG" to access MySQL. If you are aware of the SQL YOG then tell me how to do that through SQL YOG.
The AUTO_INCREMENT column has to have a UNIQUE KEY constraint associated to it.
For instance, this will work just fine:
CREATE TABLE AutoNotId
(
Id INTEGER PRIMARY KEY,
Auto INTEGER AUTO_INCREMENT,
UNIQUE (Auto)
);
Edit:
The ALTER statement would look somewhat like this:
ALTER TABLE AutoNotId
MODIFY COLUMN Auto INTEGER AUTO_INCREMENT,
ADD UNIQUE (Auto);
I recommended, however the use of the long-hand syntax to specify the name of the UNIQUE constraint; But you can always refer to MySQL's Reference Manual for the exact specifications.
In MySQL tables can only have one auto increment field and they must be indexed.
There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value.
Is there a reason you don't want it to be the primary key?
If you want an incrementing value, you could fudge it by running updates after each insert:
SELECT MAX(serial) + 1 FROM myTable;
UPDATE myTable SET serial = <that number> WHERE id = ...
I don't think you can have an auto increment field:
CREATE TABLE `t` (`dd` int(11) NOT NULL)
ALTER TABLE `t` CHANGE `dd` `dd` INT( 11 ) NOT NULL AUTO_INCREMENT
MySQL said: Documentation
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
You cannot do this in MySQL. From the doc:
There can be only one AUTO_INCREMENT
column per table, it must be indexed,
and it cannot have a DEFAULT value. An
AUTO_INCREMENT column works properly
only if it contains only positive
values. Inserting a negative number is
regarded as inserting a very large
positive number. This is done to avoid
precision problems when numbers “wrap”
over from positive to negative and
also to ensure that you do not
accidentally get an AUTO_INCREMENT
column that contains 0.
For MyISAM and BDB tables, you can
specify an AUTO_INCREMENT secondary
column in a multiple-column key. See
Section 3.6.9, “Using AUTO_INCREMENT”.
create table mytable (
ID INT IDENTITY(1,1) PRIMARY KEY,
SN INT IDENTITY(1,1)
)
I'm trying the following:
CREATE TABLE Table1
(
RecordNo autonumber, --error here!
PersonId varchar(50),
...
)
But, there is an error.
How can I build the correct query in Access?
According to SQL Auto Increment a Field:
CREATE TABLE Persons
(
P_Id PRIMARY KEY AUTOINCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
The MS Access uses the AUTOINCREMENT
keyword to perform an auto-increment
feature.
By default, the starting value for
AUTOINCREMENT is 1, and it will
increment by 1 for each new record.
To specify that the "P_Id" column
should start at value 10 and increment
by 5, change the autoincrement to
AUTOINCREMENT(10,5).
Synonyms for AUTOINCREMENT include COUNTER and IDENTITY. Using IDENTITY the makes a lot of sense because it matched the #IDENTITY variable which returns the last used autonumber value.
The order might be important
CREATE TABLE Persons
( pkObject AUTOINCREMENT PRIMARY KEY)
If I try PRIMARY KEY AUTOINCREMENT as suggested, it gets upset (MSAccess 2010).
Method 1:
Open table in design view
Make a field named "ID" or whatever the field will be that will have the Auto Increment
Put "AutoNumber" under DataType
Method 2:
Make a new table
Close the table and save it
When it asks if you want a primary key click ok
Open the table in Design View
Edit the new field to whatever name you like
When using ancient DAO 3.60 and Jet 4.0 with Access 2003 files, Eugene Yokota'a syntax did not work. I found that COUNTER keyword will do the trick:
CREATE TABLE tablename(id COUNTER, Name Text (30))
Thanks to this post:
http://www.vbforums.com/showthread.php?234335