web sql database foreign key support - html

I'm testing this statement in Safari 5.0.5, but I get an error before FOREIGN:
CREATE TABLE IF NOT EXISTS Idea (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP NOT NULL,
sketchID INTEGER,
categoryID INTEGER NOT NULL,
FOREIGN KEY (sketchID) REFERENCES (Sketch),
FOREIGN KEY (categoryID) REFERENCES (Category));
I get the following error message:
SQLStatementError 1 [DATABASE] near "(": syntax error
Where is the error in this SQL statement?

keeping in mind that even with the correct syntax, foreign key feature is not enabled and could not in web database. because foreign key feature is disabled by default in sqlite3, you have to manually enable it via statement "PRAGMA foreign_keys = ON". Unfortunately, PRAGMA statement is disabled in web database.
good luck!

(Adding my comment as an answer)
As Neil pointed out, you are closing the bracket at the wrong position.
Additionally the syntax for the foreign key is wrong, the following should work (provided the HTML5 SQL dialect is standard compliant)
CREATE TABLE IF NOT EXISTS Idea
(
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP NOT NULL,
sketchID INTEGER,
categoryID INTEGER NOT NULL,
FOREIGN KEY (sketchID) REFERENCES Sketch (sketchId),
FOREIGN KEY (categoryID) REFERENCES Category (categoryId)
);

You need to replace the ) with a , after categoryID INTEGER NOT NULL) so your statement will become:
CREATE TABLE IF NOT EXISTS Idea (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP NOT NULL,
sketchID INTEGER,
categoryID INTEGER NOT NULL,
FOREIGN KEY (sketchID) REFERENCES Sketch (sketchID),
FOREIGN KEY (categoryID) REFERENCES Category (categoryID));
Note the additional bracket at the end of the statement.

Related

MySQL Foreign key constraint are incompatible

MySQL Version 8.0.17
The full error reads:
Referencing column 'groupLineId' and referenced column 'groupLineId' in foreign key constraint 'salesItemLine-groupLine' are incompatible
I am trying to link two tables via the groupLineId which are both NOT NULL VARCHAR(12). I am not sure why I am getting the error. I have several other foreign key relationships like this in my DB.
I am using the following code to generate the two tables. (Note: code for invoice table not shown)
CREATE TABLE IF NOT EXISTS `reports`.`groupLine` (
`groupLineId` VARCHAR(12) NOT NULL,
`lineNum` INT NOT NULL,
`invoiceId` VARCHAR(12) NOT NULL,
PRIMARY KEY (`groupLineId`, `lineNum`, `invoiceId`),
INDEX `groupLine-invoice_idx` (`invoiceId` ASC) VISIBLE,
CONSTRAINT `groupLine-invoice`
FOREIGN KEY (`invoiceId`)
REFERENCES `reports`.`invoice` (`invoiceId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `reports`.`salesItemLine` (
`groupLineId` VARCHAR(12) NOT NULL,
`lineNum` INT NOT NULL,
`description` VARCHAR(256) NULL,
`amount` DECIMAL NULL,
`detailType` VARCHAR(45) NULL,
PRIMARY KEY (`groupLineId`, `lineNum`),
INDEX `salesItemLine-groupLine_idx` (`groupLineId` ASC) VISIBLE,
CONSTRAINT `salesItemLine-groupLine`
FOREIGN KEY (`groupLineId`)
REFERENCES `reports`.`groupLine` (`groupLineId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
If anyone comes across this my problem was how I was making changes and using the "Forward Engineer" feature of MySQL Workbench. I had originally created the database with groupLineId ID as type INT. I then changed the model to make the groupLineId in both tables to VARCHAR(12). Then when I would run "Forward Engineer" it would first write the new groupLine table and change the type of groupLineId to VARCHAR(12) this would then break the existing FROGIEN key with salesItemLine table which has not been updated and still has the type of groupeLineId as INT.
The solution was to DROP both tables before rerunning the forward engineering. (Or at least manually dropping the existing constraints)

Converting MySQL code to PostgreSQL using foreign keys

I am trying to create tables with a primary key and foreign key in Postgresql. Originally was created in MySQL but I do not know how to convert can someone assist.
I converted the first table but the instructor table is giving me problems
CREATE TABLE instructor_detail (
id SERIAL PRIMARY KEY,
youtube_channel VARCHAR(128) NULL,
hobby VARCHAR(45) NULL
);
DROP TABLE IF EXISTS instructor;
`CREATE TABLE instructor (
id SERIAL PRIMARY KEY,
first_name VARCHAR(45) NULL,
last_name VARCHAR(45) NULL,
email VARCHAR(45) NULL,
instructor_detail_id INT NULL,
CREATE INDEX fk_detail_idx ON instructor (instructor_detail_id), CONSTRAINT fk_detail FOREIGN KEY (instructor_detail_id) REFERENCES instructor_detail (id) ON DELETE NO ACTION ON UPDATE NO ACTION
);`
ERROR: syntax error at or near "CREATE"
LINE 7: CREATE INDEX fk_detail_idx ON instructor (instructor_detai...
The foreign key constraint will work in PostgreSQL, no change is required.
The KEY clause is a MySQL extension to create an index within CREATE TABLE, which PostgreSQL does not support.
You'll have to convert the KEY clause to a separate CREATE INDEX statement in PostgresSQL.

Not sure why this syntax error is happening

Hi I'm not very familiar with MySQL as I have only started using it today and I keep getting this syntax error and am not really sure what the problem is. I have attached a screenshot of the code and also pasted it below with the error in bold.
I'm sorry if this is a silly error that is easily fixed I'm just not sure how to fix it and would be very appreciative of any help.
CREATE TABLE copy (
`code` INT NOT NULL,
isbn CHAR(17) NOT NULL,
duration TINYINT NOT NULL,
CONSTRAINT pkcopy PRIMARY KEY (isbn, `code`),
CONSTRAINT fkcopy FOREIGN KEY (isbn) REFERENCES book (isbn));
CREATE TABLE student (
`no` INT NOT NULL,
`name` VARCHAR(30) NOT NULL,
school CHAR(3) NOT NULL,
embargo BIT NOT NULL,
CONSTRAINT pkstudent PRIMARY KEY (`no`));
CREATE TABLE loan (
`code` INT NOT NULL,
`no` INT NOT NULL,
taken DATE NOT NULL,
due DATE NOT NULL,
`return` DATE NULL,
CONSTRAINT pkloan PRIMARY KEY (taken, `code`, `no`),
CONSTRAINT fkloan FOREIGN KEY (`code`, `no`) REFERENCES copy, student **(**`code`, `no`));
Create the tables first, then use the ALTER TABLE statement to add the foreign keys one by one. You won't be able to call two different tables on the foreign key, so you'll have to use an ID that maps to both. Here is an example to add the foreign keys after the table has been created:
Add a new table named vendors and change the products table to include the vendor id field:
USE dbdemo;
CREATE TABLE vendors(
vdr_id int not null auto_increment primary key,
vdr_name varchar(255)
)ENGINE=InnoDB;
ALTER TABLE products
ADD COLUMN vdr_id int not null AFTER cat_id;
To add a foreign key to the products table, you use the following statement:
ALTER TABLE products
ADD FOREIGN KEY fk_vendor(vdr_id)
REFERENCES vendors(vdr_id)
ON DELETE NO ACTION
ON UPDATE CASCADE;

I don't get whats wrong with the mysql syntax

CREATE TABLE hoofdtoonder
(
id INT NOT NULL,
idondersoorten INT FOREIGN KEY REFERENCES `ondersoort`(`id`) NOT NULL,
)
//making table but the error is with the references its on a mysql database someone please help
It says error at FOREIGN KEY REFERENCES ondersoort(id) NOT NULL. But I don't know what's wrong with the syntax.
There are several things wrong here:
First, for an inline constraint, you don't need to specify foreign key, just references.
The not null clause should come before the references clause.
You have a redundant comma at the end of the last column's specification.
To put it all together:
CREATE TABLE hoofdtoonder (
id INT NOT NULL,
idondersoorten INT NOT NULL REFERENCES `ondersoort`(`id`)
);
MySQL does not support inline foreign key references.
It's true that the SQL language allows for syntax like #Mureinik suggested:
idondersoorten INT NOT NULL REFERENCES `ondersoort`(`id`)
But you will find that MySQL parses this and ignores it. InnoDB does not support inline foreign key syntax. If you now run SHOW CREATE TABLE hoofdtoonder, it'll show this:
CREATE TABLE `hoofdtoonder` (
`id` int(11) NOT NULL,
`idondersoorten` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Where did the REFERENCES go? It was silently discarded. This is actually a beef I have with MySQL, that it recognizes some valid constraint syntax, but ignores it. It doesn't even show you a warning. It just defines the table without the constraint.
In MySQL, you must declare a foreign key as a table-level constraint, like this:
CREATE TABLE hoofdtoonder (
id INT NOT NULL,
idondersoorten INT NOT NULL,
FOREIGN KEY (idondersoorten) REFERENCES `ondersoort`(`id`)
);

How to code a foreign key in sql?

Hi this is the table I am trying to create:
CREATE TABLE images
(
id PRIMARY KEY NOT NULL INT,
product_id FOREIGN KEY NOT NULL INT,
src varchar(255) NOT NULL
)
But its not letting me (I am getting a syntax error). Anyone have any ideas?
CREATE TABLE IMAGES(
Id int NOT NULL,
PRODUCT_ID int NOT NULL,
src varchar(255) NOT NULL,
PRIMARY KEY (Id),
FOREIGN KEY (P_Id) REFERENCES PRODUCTS(P_Id)
)
and make sure you build the Products table first , and do the reference foreign key
Check out InnoDB Foreign Key Constraints for the right syntax to use. In particular, you need to declare the column you're referencing when you create a foreign key.
Additionally, since you're using MySQL, make sure your tables use InnoDB, otherwise the foreign keys won't actually get enforced.