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)
)
Related
I am creating a relation between tables which is necessary to auto increment two columns id_quotation and seq_quotation. The column id_quotation, I am referencing into another table (tb_core_process_id_quotation) where I already increment it.
This table below (tb_core_process_customer_data) will be used by other tables to catch commons and main customers data. To make it is necessary this three validation keys: cpf_cnpj, id_quotation and seq_quotation that are common to entire tables in this database.
tb_core_process_customer_data query:
CREATE TABLE tb_core_process_customer_data(
cpf_cnpj VARCHAR(255) NOT NULL,
id_quotation INT NOT NULL,
seq_quotation INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
dt_birth DATE,
cd_insurance_type INT,
PRIMARY KEY (cpf_cnpj, seq_quotation, id_quotation),
FOREIGN KEY (cd_insurance_type) REFERENCES tb_nm_insurance_type(cd_insurance_type),
FOREIGN KEY (id_quotation) REFERENCES tb_core_process_id_quotation(id_quotation)
);
tb_core_process_id_quotation query:
CREATE TABLE tb_core_process_id_quotation(
id_quotation INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id_quotation)
);
So, I am having difficult to relation this three keys and to make this validation. When I try to create tb_core_process_customer_data the follow message shows me off:
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
MySQL uses the key on the auto_increment field to make look ups faster. You placed your auto_increment field as the 2nd field in your pk, therefore MySQL cannot use the pk on its own to check the value of the auto increment, hence the error message.
There are a number of ways to remediate the issue.
Add a separate key (does not have to be unique or pk) on the seq_quotation field.
The auto increment value will be unique anyway, therefore you can make it alone as pk and add another index on the other 2 fields. Packaging other fields with an auto_increment in a unique index or pk is a bit pointless - unless you use MySQL. (see notes below)
I'm not sure if it works, but you may try to move seq_quotation to the 1st field in the current PK.
Note
If your intention was to use the auto increment to have an incrementing number per group, then
make sure you either use myisam or bdb table type,
or in case of innodb do not use auto_increment, use a trigger instead (see this question on SO for details)
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
Is it possible to create auto-increment based on a specific field? For example i have UserId and Status fields, so for each row with same UserId i need to auto-increment its Status, not global.
There is three thing that come to mind when I read your question. One was an auto incrementing field which acts as your ID number. Updating a table with data that has no unique ID number. Searching for fields with the same Userid to Status
Mt First example is of a creating a table and your AUTO_INCREMENTing number ID:
CREATE TABLE tableNameHere
(
UniqueID int NOT NULL AUTO_INCREMENT,
FirstName varchar(255) NOT NULL,
StatusOrYourColumn int(100) NOT NULL,
PRIMARY KEY (UniqueUD)
)
More on auto incrementation.
You may have already built your table and now want to 'add' additional and or modify your fields using ALTER:
ALTER TABLE tableNameHere StatusOrYourColumn INT AUTO_INCREMENT PRIMARY KEY
But be careful, you don't want to overwrite your settings that you have already set.
Another Thing that came to my mind when reading was where you said Status and Userid where the same. You can find these using the WHERE clause like so:
SELECT * FROM tableName WHERE tableName.Userid = anotherTableOrTableName.Status
Using these queries you can update, remake, alter and query your database table.
how to ensure mysql column has 4 character ?
Due to using the MATCH function with minimum 4 char, i want my primary key which is a INT to be at least 4 char.
How can i do it ?
eg. 1 will be 0001 in sql table
Use ZEROFILL attribute of column. Alter your Primary key column to have zerofill enabled.
Try this:
ALTER TABLE `tablename` CHANGE
COLUMN `id` `id` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT ;
You can use MySQL's LPAD function:
SELECT LPAD('1',4,'0');
-> '0001'
Docs here.
Assuming your primary key is an auto-incremented id.
Temporarily take the auto-increment off.
Add 999 to every id. (Of course if other tables use this id as a foreign key, you are going to have to adjust them, too).
Re-add the auto-increment, but make sure that it starts at at least 1000. See: How to set initial value and auto increment in MySQL?
In php myadmin it says it is no use in defining auto increment column as primary key for table. you can remove primary key constrain. (since both do the same job like).
Is this true. should I remove primary key constrain?
won't it good to have a primary key column for where clause rather than auto increment column
Keep The PK constraint. It will save you from some trouble:
if you intend to use some frameworks which check for this constraint to determine which columns are used as PK. They will be lost or require additional configuration if the constraint is not present.
When you'll use DB design software it will work better if your DB is properly designed.
When you'll have to change your DB software (upgrade or change brands) You will be happy to have all the constraints properly defined in your SQL statements.
You can create a column that is auto_incrementing without it being the primary key:
This statement is legal:
mysql> create table silly_but_possible (
id int auto_increment not null,
xx varchar(9),
key(id),
primary key (xx));
Query OK, 0 rows affected (0.15 sec)
So if you want your auto_incrementing column to be the PK, you'd better define it as such.
Note that a table (at present) can only have one auto_incrementing column.
What happens if you don't define a PK?
If you don't MySQL will define a hidden auto_incrementing PK for you.
However if you already have a auto_increment column, that column will always have an index, and because there can only be one auto_incrementing column, MySQL (at present!) has no other choice than to promote that row to the primary key.
when u define a column as a auto_increment it must be key(primary , unique ,index).
take an example
mysql> create table test
-> (id int(5) auto_increment,
-> name char(10)
-> );
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
so when we define a column as auto_increment it should be key.So it is better to define the column as PK.
The primary index is used by the database manager for efficient access to table rows, and allows the database manager to enforce the uniqueness of the primary key.