I'm trying to create a table to capture details of subtype variations using auto_increment but I keep get an error saying:
1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
However, I'm unsure how to capture it all correctly as I want the auto_increment of subtypes to start from 1 for each TypeId....is this not possible?
CREATE TABLE IF NOT EXISTS `types` (
`TypeID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`TypeFeatures` TEXT NOT NULL,
PRIMARY KEY (`TypeID`))
ENGINE = InnoDB;
insert into types
(TypeFeatures)
values
(1,'Type1'),
(2,'Type2'),
(3,'Type3')
;
CREATE TABLE IF NOT EXISTS `subtypes` (
`Type_ID` INT UNSIGNED NOT NULL,
`SubtypeID` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`SubtypeFeatures` TEXT NOT NULL,
PRIMARY KEY(`Type_ID`,`SubtypeID`))
ENGINE = InnoDB;
insert into subtypes
(Type_ID,subtypeID,SubtypeFeatures)
values
(1,1,'Subtype1'),
(1,2,'Subtype2'),
(2,1,'Subtype1'),
(3,1,'Subtype1')
;
Thanks in advance for any suggestions!
Bendy
Related
am on a MySQL database and am using MySQL workbench.
here's my table DDL
CREATE TABLE `contract_validator` (
`id` bigint(20) NOT NULL,
`created` datetime(6) NOT NULL,
`user_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I want to make an id column in a table auto_increment, i did so using
SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE validator MODIFY id bigint AUTO_INCREMENT;
SET FOREIGN_KEY_CHECKS = 1;
I have two issues, i want to set the auto_increment to max(id) + 1
i have tried:
SELECT #validator_id := max(id) + 1 FROM validator;
ALTER TABLE validator AUTO_INCREMENT = #validator_id ;
but i get syntax error,
the second issue is that when i try to insert a line i get an error
Error Code: 1364. Field 'id' doesn't have a default value
I don't know what I did wrong,
thank you for your help.
Normally, you don't have to take care of AUTO_inCREMENT value.
When you update id column to make it auto increment, MySQL will automatically set AUTO_INCREMENT value to the MAX + 1 value, event if there are gaps in in column numerotation.
SQL for setting auto increment on id field:
ALTER TABLE `test` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT, add PRIMARY KEY (`id`);
I'm using MySQL Workbench.
I would like to create a table named courseInfo and I want to put a column named moduleCode in it, but I want it to always be similar in format: CFSM H0000 where the four zeros are a number that increases starting with 0000.
For example:
CFSM H0001
CFSM H0002
[..]
You cannot auto-increment character type columns in MySQL, as auto-increment is only possible on integer type columns. One (alphanumeric) auto-incrementing moduleCode column would therefore not be possible. However, you could try splitting up the moduleCode into two columns, for example like so:
CREATE TABLE `courseInfo` (
`prefix` CHAR(6) NOT NULL DEFAULT 'CFSM H',
`id` SMALLINT(4) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
KEY (`id`)
) AUTO_INCREMENT = 0;
Where prefix could for example be "CFSM H" and id could be 0001
Then, upon executing SELECT statements, you could merge the prefix column with the id column into a moduleCode column with CONCAT, e.g.:
SELECT CONCAT(`prefix`, `id`) as `moduleCode` FROM `courseInfo`;
An alternative approach (from MySQL version 5.7 and up) seems to be the use of a generated column, for example:
CREATE TABLE `courseInfo` (
`prefix` CHAR(6) NOT NULL DEFAULT 'CFSM H',
`id` SMALLINT(4) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`moduleCode` CHAR(10) AS (CONCAT(`prefix`, `id`)),
KEY (`id`)
) AUTO_INCREMENT = 0;
However, the above example of a generated column would not work, because moduleCode is dependent on an auto-increment column, and the auto-increment is not executed yet at the time the generated column is computed. See also: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html. It would throw an ER_GENERATED_COLUMN_REF_AUTO_INC error.
You could therefore use the first solution, or try to add moduleCode as a column and use an AFTER INSERT trigger to update its value:
CREATE TABLE `courseInfo` (
`prefix` CHAR(6) NOT NULL DEFAULT 'CFSM H',
`id` SMALLINT(4) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`moduleCode` CHAR(10),
KEY (`id`),
UNIQUE KEY `unique_index` (`prefix`,`id`)
) AUTO_INCREMENT = 0;
DELIMITER //
CREATE TRIGGER `addModuleCode` AFTER INSERT ON `courseInfo`
FOR EACH ROW
BEGIN
UPDATE `courseInfo` SET `moduleCode` = CONCAT(NEW.`prefix`, NEW.`id`) WHERE `prefix` = NEW.`prefix` AND `id` = NEW.`id`;
END;//
DELIMITER ;
I'm creating a mySQL database for a small blog. This blog will have articles of different "types", like "public interest", "DIY", etc.
My question is about how to organize the database structure: should I create a table for the articles, a table for the types, and a third table that connect the two of them? Or should I just create the first two tables and add a field in the articles table that points out to the id number of the types table?
Option 1:
CREATE TABLE articles(
id int unsigned not null auto_increment primary key,
title varchar(300) NULL,
body TEXT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE articleType(
id int unsigned not null auto_increment primary key,
name char(200) NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `articleType` (`name`) VALUES
('public interest'),
('DIY')
CREATE TABLE articlesArticleType (
ID int unsigned not null auto_increment primary key,
typeID int not null,
articleID int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Option 2:
CREATE TABLE articles(
id int unsigned not null auto_increment primary key,
title varchar(300) NULL,
body TEXT NULL,
articleType int NOT NULL DEFAULT 1
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE articleType(
id int unsigned not null auto_increment primary key,
name char(200) NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `articleType` (`nombre`) VALUES
('public interest'),
('DIY')
In the second case I just need two tables. Which way is more efficient and preserves data integrity?
First and foremost it is important to decide on the cardinality of relationship between the 2 tables - Articles and Types as it will influence the choice of the tables structure. Broadly there are 3 cardinalities possible:
One to One
One to Many
Many to Many
Option 1 will satisfy One to Many and Many to Many cardinalities while Option 2 will satisfy One to One cardinality.
I'm having some problems with this piece of mySQL code that is not wanting to get fixed
CREATE TABLE `DatabaseMGR`
(
`databaseID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`primCat` INT UNSIGNED NOT NULL,
`databaseName` VARCHAR(20),
UNIQUE KEY (`databaseID`),
PRIMARY KEY (`databaseID`),
INDEX `databaseID`
)ENGINE = InnoDB;
It says that there is an error at line 1 with the regular "check your mysql syntax for right usage" error in response to ` usage. Is there something I'm missing? I'm new to sql so I might be missing something obvious.
Thanks.
The main point for your problem is at the line you are defining the index. In create table statement, you should use it with this syntax:
create table table_name (
...
index `INDEX_NAME` (`INDEX_COLUMN`)
);
So you can fix your problem by changing your code to below:
CREATE TABLE `DatabaseMGR`
(
`databaseID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`primCat` INT UNSIGNED NOT NULL,
`databaseName` VARCHAR(20),
UNIQUE KEY (`databaseID`),
PRIMARY KEY (`databaseID`),
INDEX `ix_databaseID` (`databaseID`) # Note the change on this line
)ENGINE = InnoDB;
However, in MySQL primary key column gets an index by default, so you can leave out that line totally, that results in the following code:
CREATE TABLE `DatabaseMGR`
(
`databaseID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`primCat` INT UNSIGNED NOT NULL,
`databaseName` VARCHAR(20),
UNIQUE KEY (`databaseID`),
PRIMARY KEY (`databaseID`)
)ENGINE = InnoDB;
To improve more:
databaseID is already a primary key, so you do not have to make define it unique again, since: primary key = unique + not null
Since MySQL is case insensitive, you should not use camel case names. So instead of databaseID, better to say database_id. There are more naming convention you can go through, though I will not mention here.
So for final table defination I suggest:
CREATE TABLE `database_mgr`
(
`database_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`prim_cat` INT UNSIGNED NOT NULL,
`database_name` VARCHAR(20),
PRIMARY KEY (`databaseID`)
)ENGINE = InnoDB;
What is wrong with this SQL,
-> );
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column
SQL
CREATE TABLE TABLENAME12
(
TAB_ID INT NOT NULL AUTO_INCREMENT,
NAME_FIRST NVARCHAR(200),
TYPE NVARCHAR(200)
);
I am using mysql, how can I solve this. I am trying to create a table. and I get this error
You must specify AUTO_INCREMENT column as PRIMARY KEY try:
CREATE TABLE TABLENAME12
(
TAB_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
NAME_FIRST NVARCHAR(200),
TYPE NVARCHAR(200)
);
As Error Says.
"Incorrect table definition; there can be only one auto column and it must be
defined as a key"
http://sqlfiddle.com/#!2/7e064
Add primary key on auto_increment column.
CREATE TABLE TABLENAME12
(
TAB_ID INT NOT NULL AUTO_INCREMENT,
NAME_FIRST NVARCHAR(200),
TYPE NVARCHAR(200),
PRIMARY KEY (TAB_ID)
);
The simular result you'll get with
CREATE TABLE TABLENAME12
(
TAB_ID INT NOT NULL AUTO_INCREMENT,
NAME_FIRST NVARCHAR(200),
TYPE NVARCHAR(200),
PRIMARY KEY( TAB_ID )
);
This is also usable with other db's as mysql except the type definition int and nvarchar.
if you use varchar and integer instead you are compatible with postgresql.