phpmyadmin,mysql - Token Mismatch but i successfully created a table - mysql

I am doing a table with a foreign key, here it is.
create table PM_Team_Members
(
PM_Team_Members_ID int NOT NULL auto_increment PRIMARY KEY,
PM_Team_Members_firstName varchar(50) not null,
PM_Team_Members_middleName varchar(50) not null,
PM_Team_Members_lastName varchar(50) not null,
PM_Team_Members_address varchar(255) not null,
PM_Team_Members_contact numeric not null,
PM_Spec_id int,
constraint fk_PM_id foreign key (PM_Spec_id) references PM_Specialization(PM_Spec_id)
)
and this is the reference table
create table PM_Specialization
(
PM_Spec_ID int auto_increment PRIMARY KEY,
PM_Spec_Specialization varchar(50) not null,
PM_Spec_Description varchar(255) not null
)
When I click GO for the query. It said that MySQL returned an empty result set (i.e. zero rows). (Query took 0.2162 sec) which I assume that it go correct. But then a pop-up show that Error: Token Mismatch
How is that? Did I do something wrong or is it some kind of a bug?

This part references PM_Specialization(PM_Spec_id) )
You have PM_Spec_ID not PM_Spec_id in your PM_Specialization table.

Related

Created table with generated columns throw an error

I am trying to build my table in MySQL following this answer. Hower i keep getting errors in MySQL even launching the same query used as example in the answer
this query is from documentation and works correctly
CREATE TABLE triangle (
sidea DOUBLE,
sideb DOUBLE,
sidec DOUBLE AS (SQRT(sidea * sidea + sideb * sideb)) stored
);
this query comes from the answer i've linked and it gives me an error
CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
this is the query i am trying to execute based on the answer which is giving me also an error
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) NOT NULL AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique,
PRIMARY KEY (id),
INDEX (first));
can you help me figuring out why it's not working?
Note that SHA2(concat_ws(',',first, second, third, fourth), 256) this works in a normal select statement
EDIT
this is the error i am getting
Query execution failed
Reason:
Errore SQL [1064] [42000]: (conn:28) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT ' at line 5
Query is : CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8
You have to add the NOT NULL after the AS part (computed column definition) of the column like this:
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique NOT NULL,
PRIMARY KEY (id),
INDEX (first)
);
Also have a look at the official document of CREATE TABLE. There you can find the description of the datatype syntax. The definition of the computed column is part of the datatype.

errno: 150 "Foreign key constraint is incorrectly formed" Nothing helps

sorry for maybe stupid question, but I stack with my sql query. Tried out many methods to avid it but still have error 150. I've created 3 tables and one with foreighn keys:
Table users
create table users(
id int(11) primary key auto_increment,
unique_id varchar(23) not null unique,
name varchar(50) not null,
cname varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);
Table behaviours
CREATE TABLE behaviours (
id int(2) AUTO_INCREMENT PRIMARY KEY,
bName varchar(100) NOT NULL
);
Table severytys
CREATE TABLE severitys (
id int(2) AUTO_INCREMENT PRIMARY KEY,
severity varchar(10) NOT NULL
);
And here it shows errors:
CREATE TABLE child_behaviours(
id int(11) primary key auto_increment,
name varchar(50) not null,
cname varchar(50) not null,
bName varchar(100) NOT NULL,
severity varchar(10) NOT NULL,
start_at datetime,
stop_at datetime null,
FOREIGN KEY (id) REFERENCES users(id),
FOREIGN KEY (bName) REFERENCES behaviours(bName),
FOREIGN KEY (severity) REFERENCES severitys(severity)
);
Will be really pleasent to have an answer to this issue
You can only create a FK on a column that is both UNIQUE and NOT NULL. This is usually, but not always, the primary key of the referenced table.
If you reference anything other than the PK, you need a really really good reason.
(I'd even add to that, that you need a really really good reason to use anything other than an INT or a couple INTs as a PK...)
Now, child_behaviours has several errors:
Duplication of columns already present in users. If you reference users by its id, there is no need to duplicate the columns.
Reference to behaviors(bName) instead of using its id
same thing for reference to severity
Also, there is the generic mistake of using "id" columns. Please call them "user_id", "severity_id", etc, and then call them the same in your references. This will make your life much easier, and avoid stuff like:
SELECT foo.id AD foo_id, bar.id AS bar_id FROM foo JOIN bar ON ...
You have to refer to a primary key, when building a foreign key
CREATE TABLE child_behaviours(
userId int not null,
behaviourId int NOT NULL,
severityId int NOT NULL,
start_at datetime not null,
stop_at datetime,
FOREIGN KEY (userId) REFERENCES users(id),
FOREIGN KEY (behaviourId) REFERENCES behaviours(id),
FOREIGN KEY (severityId) REFERENCES severitys(id)
);

Why can't I reference a primary key?

The first two tables successfully pass, but as soon as the mysql checker reach the third table, it informs me that there is a problem with the reference key.
Here is my code:
CREATE TABLE USER(
Username VARCHAR(20) NOT NULL,
Email VARCHAR(50) NOT NULL,
Picture VARCHAR(30),
Points INT(5) DEFAULT 1 NOT NULL,
Password VARCHAR(50) NOT NULL,
Firstname VARCHAR(25) NOT NULL,
Lastname VARCHAR(25) NOT NULL,
PRIMARY KEY(Username)
);
CREATE TABLE THREAD (
ThreadID INT(9) NOT NULL AUTO_INCREMENT,
Title VARCHAR(255) NOT NULL,
PostID int(9) NOT NULL,
PRIMARY KEY (ThreadID)
);
CREATE TABLE POST(
ThreadID INT(9) NOT NULL,
PostID INT(9) NOT NULL AUTO_INCREMENT,
Content TEXT NOT NULL,
NumberOfLines INT(5) NOT NULL,
PRIMARY KEY(ThreadID, PostID),
FOREIGN KEY ThreadID(ThreadID) REFERENCES THREAD(ThreadID)
);
The error I am getting is the following:
#1064 - 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 '
PRIMARY KEY(ThreadID, PostID),
FOREIGN KEY fk_ThreadID(ThreadID) REFE' at line 6
I tried creating the PRIMARY KEY(ThreadID, PostID) as separate entities, but this doesn't seem to work.
Thank you.
Adding to #Schwern, its giving error due to wrong usage of primary key. Below create table will work.
create table a (
id bigint(20) NOT NULL AUTO_INCREMENT,
col2 varchar(10), primary key (id, col2)
) engine=innodb;
So auto-increment column needs to be first one in the primary key.
We have extensively used this kind of primary key to partition the table on col2 - to range partition on a column, the column need to be part of primary key.
Also to mention, you can achieve additional unique key at the cost of an extra index
MySQL isn't being terribly informative there. SQLFiddle gives it straight.
Incorrect table definition; there can be only one auto column and it must be defined as a key
You can't have a multi-column key where one of them is AUTO_INCREMENT The AUTO_INCREMENT column must be first in a multi-column key in an InnoDB table. Either remove AUTO_INCREMENT from PostID, or make PostID alone the PRIMARY KEY and protect a post from appearing in the same thread twice with UNIQUE(ThreadID, PostID).
CREATE TABLE POST(
PostID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
ThreadID INT NOT NULL REFERENCES Thread(ThreadID),
Content TEXT NOT NULL,
NumberOfLines INT NOT NULL,
UNIQUE(ThreadID, PostID)
);
UPDATE: The best answer is #georgecj11's to put PostID first in the key as it only uses a single index.

Multiple Errors in Uploading Database Columns MySQL

I am new to MySQL and am uploading a database for the first time. I am not uploading the data, just the columns for now. The issue is that I get an error for every line of code provided:
#1064 - You have an error in your SQL syntax;
I can understand if I was getting the message for just one or two lines of the code, but I am beginning to think I misformatted all the database file since I'm getting that error message for every line.
I previously changed the primary/foreign keys
CONSTRAINT `PurchasePK` PRIMARY KEY (`P_ORDERNO`)
CONSTRAINT `PurchaseFK` FOREIGN KEY (`SUPPLY_CODE`)
to: P_ORDERNO int(5) NOT NULL auto_increment PRIMARY KEY,
And still get errors.
I would appreciate if someone took a look at the contents of the .sql file below and let me know if there is something I am missing that is giving me consistent errors.
-- Table structure for table `Purchase`
CREATE TABLE IF NOT EXISTS `Purchase` (
`P_ORDERNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_CODE` int(5) NOT NULL auto_increment FOREIGN KEY,
`P_ORDER_DATE` timestamp NOT NULL,
`P_ORDER_AMT` int(5) NOT NULL,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Vendor`
CREATE TABLE IF NOT EXISTS `Vendor` (
`VENDORNO` int(5) NOT NULL auto_increment PRIMARY KEY,
`VENDOR_NAME` varchar(50) NULL,
`VENDOR_STREET` varchar(50) NULL,
`VENDOR_CITY` varchar(50) NULL,
`VENDOR_STATE` varchar(2) NULL,
`VENDOR_ZIP` varchar(3) NULL,
`VENDOR_AREA_CODE` varchar(5) NULL,
`VENDOR_PHONE` varchar(10) NULL,
);
ENGINE = InnoDB;
-- Table structure for table `Supply`
CREATE TABLE IF NOT EXISTS `Supply` (
`SUPPLY_CODE` int(5) NOT NULL auto_increment PRIMARY KEY,
`SUPPLY_DESC` varchar(50) NULL,
`SUPPLY QTY` int(5) NOT NULL,
);
ENGINE = InnoDB;
You have trailing commas
`SUPPLY QTY` int(5) NOT NULL,
^---here
);
in each of the table definitions. That makes the DB server expect another field definition, but instead it runs into );

Why am I getting error number 150 from this MySQL statement?

I've checked that the data type of referenced table is exactly same as foreign key on this table below, and I'm still not sure? The SQL document isn't exactly clear.
P_id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(128) NOT NULL,
price DECIMAL(5,2) UNSIGNED NOT NULL,
descr TEXT,
imgName VARCHAR(50),
stock INT UNSIGNED NOT NULL DEFAULT '0',
PG_id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY (P_id),
FOREIGN KEY (PG_id) REFERENCES prodGroups(PG_id)
There could only be one AUTO INCREMENT column. So, I guess, one in the PG_id INT AUTO_INCREMENT NOT NULL is just a copy/paste went wrong :)