Error when importing schema.sql - mysql

I'm new to sql, currently I am using phpMyAdmin via XAMP, I think that uses mysql so correct me if I'm wrong by saying that. Anywhos, I'm trying to import a schema.sql data file into my database I created called "test" but I got an error upon importing it:
It says
Import has been successfully finished, 1 queries executed.
(schema.sql)
But then it also gives me an error message:
CREATE TABLE `population` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`location` varchar(150) NOT NULL,
`slug` varchar(150) NOT NULL,
`population` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
MySQL said: Documentation
1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
and :
Notice in .\import.php#704 Undefined variable: import_text Backtrace
I'm not sure what the issue is. The database I created is completely and has nothing in it.

Column id is the auto-column in question; auto-columns need to be defined as a key, for example as a unique key or a primary key. In your case, a primary key is a good idea because - well, it's your id-column.
CREATE TABLE `population` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`location` varchar(150) NOT NULL,
`slug` varchar(150) NOT NULL,
`population` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Related

MySQL - SQLite - Converting is spitting out SET error

I am trying to convert the following sql into sqlite via sqlite3.exe but it keeps giving me this error:
Error: near line 1: near "SET": syntax error
I'm not entirely sure what this means or why. Here is my sql script:
SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO',
time_zone = '+00:00';
CREATE TABLE `px` (
`x` int(11) NOT NULL,
`y` int(11) NOT NULL,
`colour` varchar(30) NOT NULL,
`time` varchar(30) NOT NULL,
`id` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `px`
ADD PRIMARY KEY (`id`);
ALTER TABLE `px`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
I'm sure i'm overlooking a simple syntax error. Any help would be much appreciated!
You can 't just run a MySQL script in SQLite. These are two different databases, whose syntax differ.
In SQLite, a relatively close syntax to the MySQL script would be:
CREATE TABLE `px` (
`x` integer NOT NULL,
`y` integer NOT NULL,
`colour` varchar(30) NOT NULL,
`time` varchar(30) NOT NULL,
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT
);

Issue with MySQL to H2 DDL

I have a table created in MySQL that is initiated and it works fine. But I have defined an in memory database with H2 for testing purposes that uses the same DDL to clone the table that I can test my app with.
CREATE TABLE `foo` (
`fieldA` BIGINT(20) NOT NULL AUTO_INCREMENT,
`fieldB` BIGINT(20) NOT NULL,
`fieldC` TIMESTAMP NOT NULL,
`fieldD` TIMESTAMP NOT NULL,
`fieldE` VARCHAR(40) NOT NULL,
`fieldF` VARCHAR(40) NOT NULL,
`fieldG` CHAR(3) NOT NULL,
PRIMARY KEY (`id`),
KEY `fieldF` (`fieldF`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
But when i try to initialise the table for my tests it spits back this error:
Caused by: org.h2.jdbc.JdbcSQLException: Unknown data type: "fieldF";
That's about as helpful as the error gets. I've explicitly set field F to VARCHAR(40), so what have do i need to do more?
if it helps, I'm creating the table in Spring Boot configuration here:
#Configuration
#EnableAutoConfiguration
#Import(ImportDataJobConfig.class)
public class TestJobConfiguration {
#Bean
public DataSource tlDataSource() {
return new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.setType(H2)
.setScriptEncoding("UTF-8")
.ignoreFailedDrops(true)
.addScript("sql/ve/init.sql")
.build();
}
}
Thanks!
Using h2-1.4.196 MySQL mode, your code snippet gives the following error:
Column "ID" not found (...)
Your DDL is wrong around id and needs to be fixed.
In order to fix the error you're facing, I would suggest:
Use the MySQL mode in your JDBC URL like jdbc:h2:mem:;mode=mysql
It seems your real primary key is fieldA, not id. Please change your SQL DDL accordingly:
CREATE TABLE `foo` (
`fieldA` BIGINT(20) NOT NULL AUTO_INCREMENT,
`fieldB` BIGINT(20) NOT NULL,
`fieldC` TIMESTAMP NOT NULL,
`fieldD` TIMESTAMP NOT NULL,
`fieldE` VARCHAR(40) NOT NULL,
`fieldF` VARCHAR(40) NOT NULL,
`fieldG` CHAR(3) NOT NULL,
PRIMARY KEY (`fieldA`),
KEY `fieldF` (`fieldF`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

MySQL Workbench wont make constraint when parent table has Generated Virtual columns

SETUP
MySQL Workbench (ver 6.3.9)
MySQL 5.7.21
My setup is simple.. I have 2 tables:
CREATE TABLE `UserDevices` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`UserID` int(11) DEFAULT NULL,
`UUID` binary(16) DEFAULT NULL,
`DeviceName` varchar(45) DEFAULT NULL,
`DeviceType` tinyint(3) NOT NULL DEFAULT '1',
`CreatedDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`TimeStamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `UserInfo` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`UUID` binary(16) DEFAULT NULL,
`UUIDText` varchar(40) GENERATED ALWAYS AS (insert(insert(insert(insert(hex(`UUID`),9,0,'-'),14,0,'-'),19,0,'-'),24,0,'-')) VIRTUAL,
`FirstName` varchar(45) DEFAULT NULL,
`LastName` varchar(45) DEFAULT NULL,
`FullName` varchar(90) GENERATED ALWAYS AS (concat(`FirstName`,' ',`LastName`)) VIRTUAL,
`Email` varchar(120) DEFAULT NULL,
`Status` tinyint(3) DEFAULT '0',
`AccountType` tinyint(3) DEFAULT '1',
`CreatedDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`TimeStamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
PROBLEM
When working inside Workbench I'm simply trying to Make a Foreign key constraint in table "UserDevices" on Column "UserID" Pointed at Table "UserInfo" Column "ID". When selecting "UserInfo" as the Referenced Table. I can not put a check next to UserID.. Also no columns show up in the drop down list under Referenced Column..
QUESTION
I understand there are a number of reasons this scenario would happen. But I'm not seeing Any data type mismatch or such that would explain this. What is making it so I can't select UserID.ID?
P.S. Setting up another table named "DeviceMeasurements" with a Column "DeviceID" I'm completely successful at setting up the constraint exactly as expected.
UPDATE
On a hunch since this is my first time playing around with Generated Virtual Columns. I went into the table and removed columns "UUIDText" and "FullName". NOW I can build my constraints as desired. But my question stands. Why can't I build constraint with the tables built as above!?
UPDATE 2
This has been confirmed as a bug in WorkBench. Manually adding the constraint via SQL code is a valid work around currently. Please see accepted answer.
Can confirm, this is a bug in WB. Have raised it with MySQL dev team.
Bug link

Mysql DDL works on local wamp but does not import on online server

I am using this schema on localhost wamp MySQL server and it works fine:
CREATE TABLE `tblcustomers` (
`customerid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`customername` varchar(50) NOT NULL,
`customerphone` varchar(11) DEFAULT NULL,
`customeraddress` varchar(255) DEFAULT NULL,
`registrationdate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `credid` (`customerid`),
UNIQUE KEY `credname` (`customername`),
UNIQUE KEY `customerid` (`customerid`)
) ENGINE=InnoDB AUTO_INCREMENT=110 DEFAULT CHARSET=latin1;
MySQL said: Documentation
#1067 - Invalid default value for 'registrationdate'
When I import the dump file on online server I get the message above. How to deal with it?
change datatype for registrationdate FROM DATETIME to TIMESTAMP and you should be through

Create Table Syntax Error

I received a MySQL data dump and am trying to insert the data into a set of temporary tables. The creation statement for the first table is shown below. When I run this I receive the error: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''temp_books'( 'ID'int( 11 ) NOT NULL AUTO_INCREMENT, 'start'varchar( 20 ) ' at line 1". I've checked the documentation for MySQL syntax, and I don't see that the problem is.
CREATE TABLE 'temp_books' (
'ID' int(11) NOT NULL AUTO_INCREMENT,
'start' varchar(20) NOT NULL,
'customer_id' int(11) NOT NULL DEFAULT '0',
'total_num' int(11) NOT NULL,
'amount' double(5,2) NOT NULL DEFAULT '0.00',
'changed' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('ID'),
UNIQUE KEY 'start' ('start')
) ENGINE=MyISAM AUTO_INCREMENT=4853 DEFAULT CHARSET=latin1;
You shouldn't put single-quotes on your identifiers. If you're going to quote them use the "back tick" character (“`”). You can also use double-quotes but you have to specify that mode:
SET sql_mode='ANSI_QUOTES';
http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
I've ALWAYS had issues with CREATE TABLE. Not sure why. Takes some trial-and-error.
Try this:
CREATE TABLE temp_books (
ID int(11) NOT NULL AUTO_INCREMENT,
start varchar(20) NOT NULL,
customer_id int(11) NOT NULL DEFAULT '0',
total_num int(11) NOT NULL,
amount double(5,2) NOT NULL DEFAULT '0.00',
changed timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (ID),
UNIQUE KEY start (start)
) ENGINE=MyISAM AUTO_INCREMENT=4853 DEFAULT CHARSET=latin1;
I had to delete the quote marks, as well as the default for the changed field, as well as the default charset. Hopefully that won't affect the data.
Here's another way of writing it that might work for some: (left away most of the columns for brevity)
create table temp_books
(
id int not null,
start varchar(255) null,
constraint six_cb_datasource_pk
primary key (id)
);