The first table was created with no problems:
mysql > CREATE TABLE nodes(
-> id varchar(10) PRIMARY KEY,
-> user text,
-> uid varchar(10),
-> version tinyint,
-> changeset smallint,
-> timestamp timestamp
-> );
It is when I tried to create the second table that MySQL is outputting an error:
mysql > CREATE TABLE node_tags(
-> id varchar(10),
-> key text,
-> value text,
-> type text,
-> CONSTRAINT pk_node_tag PRIMARY KEY (id, key),
-> CONSTRAINT fk_node_tags_id FOREIGN KEY (id)
-> REFERENCES nodes (id)
-> );
ERROR 1064 (42000): 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 '
value text,
type text,
CONSTRAINT pk_node_tag PRIMARY KEY (id, key),
CONSTRAINT' at line 3
This succeeds. Key is a reserved word, so it needs back-ticks.
Also, a text in an index limits it size as to how much of it can be indexed. It is influenced by your character set in force.
So the following runs thru:
drop table if exists nodes;
CREATE TABLE nodes
( id varchar(10) PRIMARY KEY,
user text,
uid varchar(10),
version tinyint,
changeset smallint,
timestamp timestamp
)engine=innodb;
drop table if exists node_tags;
CREATE TABLE node_tags
( id varchar(10) not null,
`key` text,
value text,
type text,
CONSTRAINT pk_node_tag PRIMARY KEY (id, `key`(255)),
CONSTRAINT fk_node_tags_id FOREIGN KEY (id) REFERENCES nodes (id)
)engine=innodb;
I would highly suggest not having a TEXT in a primary key anyway.
Reserved keywords such as 'key, value, timestamp etc' shouldn't be preferred while creating columns. This induces bugs and isn't scalable.
As far as this is concerned, using backticks key will solve the issue here.
because key is the reserved word in MySQL. please refer to this document to see list of the reserved words: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
Related
I just have started learning MySQL. So I use datagrip and create a table with this query:
CREATE TABLE customer
(
customerid CHAR(5) NOT NULL,
PRIMARY KEY (customerid),
CONSTRAINT cekcustomerid CHECK ( customerid LIKE 'CU[0-9][0-9][0-9]')
);
then I added the data with
INSERT INTO customer
VALUE ('CU001');
[2021-05-09 21:10:28] [HY000][3819] Check constraint 'cekcustomerid' is violated.
The data that I input is corresponding with the check constraint. Can anyone please tell me what am I doing wrong?
You are using SQL Server syntax not applicable to MySql.
In MySql [] are not wildcard characters when used with the operator LIKE.
You can use REGEXP:
CREATE TABLE customer
(
customerid CHAR(5) NOT NULL,
PRIMARY KEY (customerid),
CONSTRAINT cekcustomerid CHECK (customerid REGEXP '^CU[0-9]{3}$')
);
See the demo.
CREATE TABLE nodes (
id INTEGER PRIMARY KEY NOT NULL,
lat REAL,
lon REAL,
user TEXT,
uid INTEGER,
version INTEGER,
changeset INTEGER,
timestamp TEXT
); # this worked
CREATE TABLE nodes_tags (
id INTEGER,
key TEXT,
value TEXT,
type TEXT,
FOREIGN KEY (id) REFERENCES nodes(id)
); # this did not work
CREATE TABLE ways (
id INTEGER PRIMARY KEY NOT NULL,
user TEXT,
uid INTEGER,
version TEXT,
changeset INTEGER,
timestamp TEXT
); # this worked
CREATE TABLE ways_tags (
id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT,
FOREIGN KEY (id) REFERENCES ways(id)
); # this did not work
CREATE TABLE ways_nodes (
id INTEGER NOT NULL,
node_id INTEGER NOT NULL,
position INTEGER NOT NULL,
FOREIGN KEY (id) REFERENCES ways(id),
FOREIGN KEY (node_id) REFERENCES nodes(id)
); # this did not work
Error Code: 1064. 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 ' value TEXT, type TEXT, FOREIGN KEY
(id) REFERENCES nodes(id) )' at line 3
comma not valid input at this position
Key is a Reserved Keyword in MySQL. You should really avoid using it as a Table/Column Name. Consider naming it to something else; otherwise you will have to use backticks around it.
CREATE TABLE nodes_tags (
id INTEGER,
`key` TEXT, -- I'd prefer renaming it. eg: node_key
value TEXT,
type TEXT,
FOREIGN KEY (id) REFERENCES nodes(id)
);
CREATE TABLE ways_tags (
id INTEGER NOT NULL,
`key` TEXT NOT NULL, -- I'd prefer renaming it. eg: ways_tags_key
value TEXT NOT NULL,
type TEXT,
FOREIGN KEY (id) REFERENCES ways(id)
);
I need a little help with the SQL code below. I am using MySQL 5.6:
CREATE TABLE REGION (
REGION_CD varchar(200),
REGION_NAME Text (50),
Constraint REGION_PK Primary Key (REGION_CD(200)));
CREATE TABLE CUSTOMER (
CUST_ID long (50)
CUST_NAME Text (50),
REGION_CD varchar (2),
Constraint Customer_PK Primary Key
(CUST_ID(50)),
Constraint C_REGION_CD_FK
Foreign Key (REGION_CD)
References REGION);
This is my error message:
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 '(50) CUST_NAME Text (50), REGION_CD varchar (2), Constraint
Customer_PK Pr' at line 2
Fields involved in foreign key relationships must have identical type-definitions. You don't have that:
REGION_CD varchar(200),
REGION_CD varchar (2),
The "foreign" field can be nullable while the parent field isn't, but otherwise type and size definitions MUST be the same.
The reason: since your parent record has a longer field limit, you could have something like ab12, ab34, ab56. If your child record has ab in it, WHICH of those 12, 34, 56 variants should match?
I need assistance, my 'Key column' says it does not exist when it clearly does.
CREATE TABLE PUBLISHER (
LISHER_ID INT PRIMARYKEY,
LISHER_NAME VARCHAR(5NNULL,
LISHER_ADDRESS VARCHAR(NOT NULL,
LISHER_PHONE VARCHAR(5) NULL,
LISHER_EMAIL VARCHAR(4) NULL);
The column you name in the FOREIGN KEY clause has to exist in the table on which you're defining the constraint - in this case, it has to be a column in the table CONTRACT.
At a guess, I think you probably want
CREATE TABLE CONTRACT (
...
CONSTRAINT FK_CONTRACT_PUBLICATION FOREIGN KEY (PUBLICATION_PUB_ID)
REFERENCES PUBLICATION (PUB_ID));
You appear to have two mistakes, both from what appears to be copy/paste. In your CONTRACT table, you named the column PUBLICATION_PUB_ID, but in your index definition and your foreign key constraint you tried to reference it as just PUB_ID.
mysql> CREATE TABLE CONTRACT (
-> CON_NUMBER INT NOT NULL PRIMARY KEY,
-> CON_STARTDATE DATETIME NOT NULL,
-> CON_ENDDATE DATETIME NOT NULL,
-> PUBLICATION_PUB_ID INT NOT NULL,
-> INDEX PUBLICATION (PUBLICATION_PUB_ID),
-> CONSTRAINT FK_CONTRACT_PUBLICATION FOREIGN KEY (PUBLICATION_PUB_ID) REFERENCES PUBLICATION (PUB_ID)) ;
first table:
create table login(
id int auto_increment,
user varchar(20),
pass varchar(20),
primary key(id)
);
first table is created with primary key..
second table:
create table check(
cid int,
rid int,
usname varchar(20),
word varchar(20),
FOREIGN KEY(rid) REFERENCES login(id)
);
Error:
ERROR 1064 (42000): 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 'check
(cid int, rid int, usname varchar(20), word varchar(20), FOREIGN KEY(rid) R' at
line 1
The problem isn't the foreign key. check is a reserved word in MySQL. So you need to choose another name or escape it:
create table `check`(
cid int,
rid int,
usname varchar(20),
word varchar(20),
FOREIGN KEY(rid) REFERENCES login(id)
);
Ironically, although it is a reserved word, it is not actually used. Alas, I do wish that MySQL supported check constraints.
By the way, here is a SQL Fiddle.
check is a key word in MySQL; either surround it with backticks, `, or choose a different name.
Change the name of your table check. Check is a reserved word in MySQL