0

I have a syntax error in my SQL queries,what's wrong?

this is the query:

    CREATE TABLE post_cat(
    cat_id int(2) AUTO_INCREMENT NOT null PRIMARY key,
    cat_text varchar(255),
    post_cat varchar,
    FOREIGN KEY (post_cat) REFERENCES post(post_cat)
    )
3
  • 5
    What is your error? Commented Dec 10, 2017 at 18:20
  • #1005 - Can't create table telegram.post_cat (errno: 150 "Foreign key constraint is incorrectly formed") (Details…) Commented Dec 10, 2017 at 18:38
  • Add your query for adding the post table which hold the post_cat before this query. Commented Dec 10, 2017 at 19:21

2 Answers 2

3

All varchars need a length specification.

CREATE TABLE post_cat(
cat_id int(2) AUTO_INCREMENT NOT null PRIMARY key,
cat_text varchar(255),
post_cat varchar(1000),
FOREIGN KEY (post_cat) REFERENCES post(post_cat)
);
Sign up to request clarification or add additional context in comments.

1 Comment

now I have new error MySQL said: Documentation #1005 - Can't create table telegram.post_cat (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
2

You need a length for the varchar:

CREATE TABLE post_cat (
    cat_id int(2) AUTO_INCREMENT NOT null PRIMARY key,
    cat_text varchar(255),
    post_cat varchar(255),
    FOREIGN KEY (post_cat) REFERENCES post(post_cat)
);

The length should match the definition in the post table.

Here is a SQL Fiddle showing the table creation.

2 Comments

now I have new error MySQL said: Documentation #1005 - Can't create table telegram.post_cat (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
post_cat seems like a really strange primary key for a table called post. Nevertheless, the code works with the correct column definitions, as shown in the SQL Fiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.