Is there no use in defining auto increment column as primary key - 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.

Related

ERROR 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

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)

(MySQL) Why ID column should be set to Primary Key (being unique)?

I've heard Primary Key means to be unique. Correct me please if I'm wrong.
Assume we have a table of users. It has 3 columns of id, username and password. We usually set the id to be AUTO_INCREMENT. So it would technically make a new unique id each time we add a row to the table. Then, why we also set the id column to be Primary Key or Unique?
Having a column as a key offers other aspects. First, if it is primary or unique, this would enforce that no query could enter a duplicate value for that key. Also keys can allow you do things like
INSERT ... ON DUPLICATE KEY UPDATE...
Of course you also want an index on the column for quick lookups.
AUTO_INCREMENT behavior only manifests when the column is not specified during an insert. Consider:
CREATE TABLE ai (
ai int unsigned not null auto_increment,
oi int unsigned,
key (ai),
primary key (oi)
);
INSERT INTO ai VALUES (1,2);
INSERT INTO ai VALUES (1,3);
INSERT INTO ai VALUES (null,5);
This will yield (1,2), (1,3), (2,5). Note how the AUTO_INCREMENT column has a duplicate.
A primary key does two things:
enforce database integrity (uniqueness and not-null of the column)
create an index to implement that, which also makes for fast look-up by the primary key column as a "side-effect".
You may not strictly need (1) if you can ensure that in your application code (for example by only using the auto-increment value), but it does not hurt.
You almost certainly want (2), though.
So it would technically make a new unique id each time we add a row to the table
Well, that is up to you. The unique id only gets inserted if you don't specify an explicit value. And technically, it is not guaranteed to be unique, it is just an auto-increment that does not take into consideration any existing values in the table (that may have somehow ended up in there).

How to make ID "Primary Key" Field set number based on Rows rather than AUTO_INCREMENT

Right now I have my ID field as the primary key in MySQL and have AUTO_INCREMENT on. What I want to know is how to make the ID represent the number of that row in the table rather than giving it a number when it's inserted, then sticking with that number? Because when I delete something, then that number isn't used. I want them all to be unique based on row count.
Always have a primary key. Either a basic auto increment int or a composite key of multiple fields. It helps your DB do it's job and comes in handy when you want to have relationships. Add a field called RowIndex and renumber it when you delete anything.
When you create a table don't add the AUTO_INCREMENT key word
For existing table, use
ALTER TABLE <Table_Name> MODIFY COLUMN <Column_Name> INTEGER;
to remove the AUTO_INCREMENT and the Primary key will be kept.

Add another primary key to table in mysql

I want to add another primary key to a table in mysql.
I use phpmyadmin to communicate with mysql server.
When I click the primary icon for the desired field it gives me this error:
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
Edited:
here's the query:
ALTER TABLE `files` DROP PRIMARY KEY ,ADD PRIMARY KEY ( `file_type` )
How can I do it?
As the name "primary" key says, there may be only one of that (ref: Highlander).
What you might want to try is a UNIQUE KEY, that acts just like a primary for most purpouses. Auto_increment doesn't seem to fulfill any purpouse if used a second time - what'ts the point of two fields carrying exactly the same information?
I believe in your case, what you need is a composite key. I do not know your table structure, but here is a general example taken from here,
CREATE TABLE track(
album CHAR(10),
disk INTEGER,
posn INTEGER,
song VARCHAR(255),
PRIMARY KEY (album, disk, posn)
)
In this case, there is a combination of 3 columns which avoid the duplicate records as you require. Please let me know if I have any mistakes in understanding your scenario.
The error message says it, I think:
the auto_increment column must be key.
So use this query first:
ALTER TABLE 'files' CHANGE 'id' 'id' INT( 10 ) UNSIGNED NOT NULL;
this will remove the auto_increment.
Also, I recommend the Uniqe key as suggested by other answer. I believe there should always (almost) be an Id column in each table.
We can Give Primary Key only once for a table. You can prefer UNIQUE KEY to prevent duplicate records
ALTER TABLE Persons
ADD UNIQUE (P_Id)
You can mark all the fields you want as primary keys, including the existing one. The system internally will drop the existing one and will set all you marked.

MySQL - How to create an auto increement field in the DB?

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)
)