phpmyadmin shows error double increment with two column not possible - mysql

So I tried to make these following two tables with phpmyadmin.
create table category (
catId int identity(1,1),
catName varchar(20),
Constraint pk_category
PRIMARY KEY(catId))
create table subcategory (
subCatId INT IDENTITY(1,1),
catId INT,
subCatName VARCHAR(20),
CONSTRAINT pk_subcategory
PRIMARY KEY(catId,subCatId),
CONSTRAINT fk_subcat_cat
FOREIGN KEY(catID)
REFERENCES category(catId))
When creating the subcategory it shows this query error:
1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key .
There aren't two auto incremented columns in subcategory table, only the 'subCatId' is. What should be done?

It is only available for the MyISAM Storage Engine Only one numeric
auto_increment value is allowed. Each auto_increment must be have
associated column to define uniqueness from other auto_increment
values within the same table.
This ref link may hep you more about this.
Note:
Don't do that, don't have a second auto incremented column at all. Do you really need a second auto incremented value? What for? A
description of the actual problem you are trying to solve would help
others help you better. I think you have only told here how you tried
to solve a problem and not what the actual problem is.

This is what MySQL states
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.
So according to your requirement you've following choices.
Make subCatId as Primary Key
Or Make the column as Unique

You appear to be using SQL Server syntax, but run against MySQL. Use the correct syntax and it should work:
CREATE TABLE category (
catId INT NOT NULL AUTO_INCREMENT,
catName VARCHAR(20),
PRIMARY KEY (catId)
)
CREATE TABLE subcategory (
subCatId INT NOT NULL AUTO_INCREMENT,
catId INT,
subCatName VARCHAR(20),
FOREIGN KEY (catId) REFERENCES category (catId),
PRIMARY KEY (subCatId)
);
IDENTITY in SQL Server roughly corresponds to AUTO_INCREMENT in MySQL.

You have
Constraint pk_category
PRIMARY KEY(catId)
Instead, just say
PRIMARY KEY(catId)

Related

Unable to establish multiple foreign keys on mysql, specifically tested on mariaDB and w3schools online mysql

I was about to create two tables (1st table: fooditem_tbl & 2nd table: orderitem_tbl). I was planning to create 2 foreign keys (ITEM_NAME,UNIT_PRICE) on the 2nd table. I wasn't able to run the query of the 2nd table(orderitem_tbl), due to an error which is near at "INDEX". I kept looking at my query, and I still don't know what's the cause of the error.
My first table , this one works
CREATE TABLE FOODITEM_TBL
(ITEM_ID INT AUTO_INCREMENT,
ITEM_NAME VARCHAR(50) UNIQUE,
UNIT_PRICE DOUBLE UNSIGNED,
ITEM_QUANTITY INT UNSIGNED,
IN_STOCK BOOLEAN,
PRIMARY KEY (ITEM_ID, ITEM_NAME, UNIT_PRICE));
The 2nd table, which is below fails to create
CREATE TABLE ORDERITEM_TBL(
ORDER_ID INT AUTO_INCREMENT,
ITEM_NAME VARCHAR(50) UNIQUE,
UNIT_PRICE DOUBLE UNSIGNED,
ITEM_QUANTITY INT UNSIGNED,
CUSTOMER_NAME VARCHAR(50),
ADDRESS VARCHAR(50),
CONTACT_NUMBER VARCHAR(50),
PRIMARY KEY (ORDER_ID),
INDEX (ITEM_NAME,UNIT_PRICE),
FOREIGN KEY (ITEM_NAME,UNIT_PRICE) REFERENCES
FOODITEM_TBL(ITEM_NAME,UNIT_PRICE)
) ENGINE = InnoDB;
P.S Please help
Q: What is causing the error? How can I fix it?
For InnoDB, there must be an index on the target table, on the target column(s). Datatypes of the referencing foreign key column(s) must match the datatypes of the target column(s).
Before creating the foreign key constraint, make sure a suitable index exists on the target table, e.g.
CREATE UNIQUE INDEX `FOODITEM_TBL_UX3` ON `FOODITEM_TBL` (`ITEM_NAME`, `UNIT_PRICE`) ;
Given that ITEM_NAME is unique in the target table, we know that the combination of ITEM_NAME and UNIT_PRICE will also be unique. I'm not sure why we wouldn't just define a foreign key constraint on just ITEM_NAME, but that doesn't really address the question that was asked.
Personally, I would avoid floating point datatypes (e.g. DOUBLE) for columns involved in foreign key constraints.

How to create a MySQL with Primary Key not an auto increment

Am trying to create the following table:
create table Cust (Name varchar(50) UNIQUE, Cat1 varchar(50), RowID int NOT NULL AUTO_INCREMENT, PRIMARY KEY (Name));
the error I get is
Incorrect table definition; there can be only one auto column and it
must be defined as a key
I want to index by the "Name", not the RowID. So even if I end it with:
...PRIMARY KEY (Name, RowID));
It fails. Of course ...PRIMARY KEY (RowID, Name)); works but is not what I want.
Can someone help me see the light please?
Thanks
You just need to make a KEY (aka index) for the auto-inc column. It doesn't have to be the primary key, but it must be the left-most column in some index.
create table Cust (
Name varchar(50),
Cat1 varchar(50),
RowID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (Name),
KEY (RowId)
);
Don't add the UNIQUE option to the name column. That creates an extra superfluous unique index, which you don't need. Any PRIMARY KEY is already unique.
I'll comment that auto-inc is not the same thing as rowid. Don't expect auto-inc to have consecutive values.

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)

Allow to assign only one value between two FK columns MYSQL

I tried to find some solution but I couldn't.
Let's supose we have the table bellow and each row of this table needs to be assigned only to one FK (columnfk1 or columnfk2) (Doesn't make sense be assigned to both OR none):
CREATE TABLE example(
id INT UNIQUE AUTO_INCREMENT
,name VARCHAR(255)
,columnfk1 INT
,columnfk2 INT
,FOREIGN KEY (columnfk1) REFERENCES example1(columnfk1)
,FOREIGN KEY (columnfk2) REFERENCES example2(columnfk2)
);
Is there some rule to warrant that each row will have one FK assigned?
I'm using MYSQL 5
Unfortunately, MySQL doesn't support the check constraint, because this can easily be handled using such a constraint:
check (columnfk1 is null or columnfk2 is null);
So, the only way that you can implement this constraint in the database with this data structure is to use a trigger.

MySQL Check Constraints

I have a database table like so:
-- Table KNOWN_AS
create table `known_as`(
id int not null auto_increment,
person_id int not null,
default_name boolean not null,
first_name varchar(100) not null,
middle_name_1 varchar(100),
middle_name_2 varchar(100),
middle_name_3 varchar(100),
last_name varchar(100),
primary key(id),
foreign key(person_id) references `person`(id)
) engine=innodb;
When inserting values, I want to check that each unique "person_id" has exactly one true "default_name".
All googling etc I've done so far has resulted in pages explaining how to keep a value non-negative, or how to make sure a value is unique - not how to check one value is unique amongst multiple entries (but not ALL).
Any help / pointers much appreciated!!
I want to check that each unique "person_id" has exactly one true "default_name"
Why not store default_name as a NOT NULL column in the person table?
how to check one value is unique amongst multiple entries (but not ALL)
Define a UNIQUE index over the composite (person_id, default_name):
ALTER TABLE known_as ADD UNIQUE INDEX (person_id, default_name);