Click here to Skip to main content
15,902,853 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I get the error
Quote:
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 'REFERENCES student (no), FOREIGN KEY c_no REFERENCES course (no) )' at line 6
when i try to run the table score.

Quote:
CREATE TABLE Persons
(
    id_P int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255)
);

CREATE TABLE dept
(
    no int(30) NOT NULL,
    name varchar(50) NOT NULL,
    PRIMARY KEY (no)
);

CREATE TABLE student
(
    no int(50) NOT NULL,
    name varchar(255),
    gender varchar(10) check (gender in('female', 'male')),
    age int(10),
    d_no int(20) NOT NULL,
    PRIMARY KEY (no),
    FOREIGN KEY (d_no) REFERENCES dept(no) 
);

CREATE TABLE course
(
    no int(100) NOT NULL,
    name varchar(100) NOT NULL,
    credit int NOT NULL,
    PRIMARY KEY (no)
);

CREATE TABLE score 
(
    s_no int(40) NOT NULL,
    c_no int(40) NOT NULL,
    score int(40) NOT NULL,
    FOREIGN KEY s_no REFERENCES student (no),
    FOREIGN KEY c_no REFERENCES course (no)
);


What I have tried:

i have read the code over and over but to no avail.
Posted
Updated 11-Nov-21 22:04pm

1 solution

Compare
SQL
CREATE TABLE student
(
    no int(50) NOT NULL,
    name varchar(255),
    gender varchar(10) check (gender in('female', 'male')),
    age int(10),
    d_no int(20) NOT NULL,
    PRIMARY KEY (no),
    FOREIGN KEY (d_no) REFERENCES dept(no) 
);
which works, with
SQL
CREATE TABLE score 
(
    s_no int(40) NOT NULL,
    c_no int(40) NOT NULL,
    score int(40) NOT NULL,
    FOREIGN KEY s_no REFERENCES student (no),
    FOREIGN KEY c_no REFERENCES course (no)
);
which does not work. Spot the difference? Parentheses.
SQL
CREATE TABLE score 
(
    s_no int(40) NOT NULL,
    c_no int(40) NOT NULL,
    score int(40) NOT NULL,
    FOREIGN KEY (s_no) REFERENCES student (no),
    FOREIGN KEY (c_no) REFERENCES course (no)
);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900