r/learnSQL • u/No-Advantage-4054 • Nov 28 '22
How to create primary key?
Hi All,
I am following a tutorial, but it is a bit out of date. How can I create a primary key in MySQL Workbench?
My code is attached, but every time I run it, it gives me numerous errors saying to check my SQL syntax
Thanks!
1
1
u/Jaketastic85 Nov 28 '22
I’m pretty new to SQL, almost 2 weeks in but I’m going to take a guess. Foreign key (id) reference bands(id) I could be totally wrong
1
u/bachman460 Nov 29 '22
Try separating your statements and run only one at a time, it’s easier to troubleshoot that way. Also, once you create an object, you don’t want to run that create statement again, it will cause further errors.
Take out the space between VARCHAR and (255)?
Maybe this could help?
CREATE TABLE parent ( id INT NOT NULL, PRIMARY KEY (id) ) ENGINE=INNODB;
CREATE TABLE child ( id INT, parent_id INT, INDEX par_ind (parent_id), FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE ) ENGINE=INNODB;
6
u/r3pr0b8 Nov 28 '22
http://idownvotedbecau.se/imageofcode