i spent last few hours trying to find what is wrong in my code(?)
CREATE TABLE transaction (
id NUMERIC PRIMARY KEY,
user_id NUMERIC NOT NULL,
account_id NUMERIC NOT NULL,
category_id NUMERIC NOT NULL,
amount DOUBLE NOT NULL, date VARCHAR(25) NOT NULL,
description VARCHAR(25),
FOREIGN KEY(account_id) REFERENCES account(id),
FOREIGN KEY(user_id) REFERENCES user(id),
FOREIGN KEY(category_id) REFERENCES category(id)
);
to find what is wrong i was adding one column after another, and i noticed i cant create table with more than two foreign keys, is it limit or am i doing something wrong? can i walk this around somehow? im not interested in this case, because i realized i need to change design of my db anyway
regards
VARCHAR is wrong.
Should be TEXT
DOUBLE should be REAL
Related
I tried inserting values into my 'JobTypes' table but I keep getting a foreign key mismatch error. I think I've made an error with my foreign keys but I can't quite figure out where exactly as I'm quite new at this, can anyone help me out?
This is what I have so far:
CREATE TABLE Projects (
Proj_ID INTEGER,
Proj_name TEXT,
PRIMARY KEY (Proj_ID)
);
CREATE TABLE Employees (
Emp_ID INTEGER,
Proj_ID INTEGER,
Emp_fname TEXT,
PRIMARY KEY(Emp_ID, Proj_ID),
FOREIGN KEY(Proj_ID) REFERENCES Projects(Proj_ID)
);
CREATE TABLE HourRates (
Job_type TEXT,
Hour_rate TEXT,
PRIMARY KEY(Job_type)
);
CREATE TABLE JobTypes (
Emp_ID INTEGER,
Job_type TEXT,
PRIMARY KEY(Emp_ID)
FOREIGN KEY (Emp_ID) REFERENCES Employees(Emp_ID)
FOREIGN KEY (Job_type) REFERENCES HourRates(Job_type)
);
Every FKEY target must be either a primary key or have an explicit unique index defined. Employees(Emp_ID) does not have a unique constraint or index. If this column is unique, you need to add a unique constraint or define a simple PKEY. Otherwise, you will not be able insert any data into the JobTypes table. The error message generated is confusing, and SQLite should at least issue some kind of warning when you create the JobTypes table, but this is not how it works, perhaps, due to the need for backward compatibility. Anyhow, my guess is that your Employees table actually needs to be split into Employees & Employees_Projects (many-to-many) tables.
So i am trying to complete finance. Following is the .schema:
sqlite> .schema
CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT NOT NULL, hash TEXT NOT NULL, cash NUMERIC NOT NULL DEFAULT 10000.00);
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE history(
symbol TEXT, name TEXT, shares INTEGER, price NUMERIC, time DATETIME
);
CREATE UNIQUE INDEX username ON users (username);
When i try to add foriegn key to history table it always return error. Here is my code:
sqlite> ALTER TABLE history ADD COLUMN id INT;
sqlite> ALTER TABLE history ADD FOREIGN KEY(id) REFRENCES users(id);
Parse error: near "FOREIGN": syntax error
ALTER TABLE history ADD FOREIGN KEY(id) REFRENCES users(id);
^--- error here
I think based on what I see in the sqlite docs that the statement should be together with the ADD column:
ALTER TABLE history ADD COLUMN id INTEGER REFERENCES users(id);
But you please check me on this syntax! Another option is to take care of creating the constraint at the same time that you create the table.
CREATE TABLE history(
symbol TEXT,
name TEXT,
shares INTEGER,
price NUMERIC,
time DATETIME,
id INTEGER,
FOREIGN KEY (id)
REFERENCES users (id));
It might not be something you have realized (yet) but every database has its unique flavor of SQL, so despite there being a SQL standard there are often little differences in the syntax of SQL for specific db implementations. So you always have to beware of this when looking up commands for your sql db.
Further detail on Sqlite foreign key constraints can be found here:
https://www.sqlitetutorial.net/sqlite-foreign-key/
what'S wrong with the following statement?
ALTER TABLE submittedForecast
ADD CONSTRAINT FOREIGN KEY (data) REFERENCES blobs (id);
The error message I am getting is
Can't create table `fcdemo`.`#sql-664_b` (errno: 150 "Foreign key constraint is incorrectly formed")
This works for me on MariaDB 10.1.8:
CREATE TABLE `submittedforecast` (
`id` INT(11) NOT NULL,
`data` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX `data` (`data`)
) ENGINE=InnoDB;
CREATE TABLE `blobs` (
`id` INT(11) NOT NULL,
`content` BLOB NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
ALTER TABLE submittedForecast
ADD CONSTRAINT FOREIGN KEY (data) REFERENCES blobs (id);
Can you give your MariaDB version number and a complete example including the CREATE TABLE statements for submittedForecast and blobs?
No idea if you already solved this but make sure both engines and collations match between tables (e.g: latin1 to latin1 and InnoDB to InnoDB).
I was having the same issue and by switching these I managed to get it working.
I was getting the same issue and after looking at my table structure, found out that my child table's foreign key clause was not referencing the primary key of parent table. Once i changed it to reference to primary key of parent table, the error was gone.
I had the same error and is actually pretty easy to solve, you have name the constraint, something like this should do:
ALTER TABLE submittedForecast ADD CONSTRAINT `fk_submittedForecast`
FOREIGN KEY (data) REFERENCES blobs (id)
If you would like more cohesion also add at the end of the query
ON DELETE CASCADE ON UPDATE RESTRICT
This could also be a different fields error so check if the table key and the foreign key are of the same type and have the same atributes.
I had the same problem, too. When checking the definition of the fields, I noticed that one field was defined as INT and the other as BIGINT. After changing the BIGINT type to INT, I was able to create my foreign key.
Using the C API, I don't see a way to determine the foreign key constraints for a named table?
Given this example:
CREATE TABLE artist(
artistid INTEGER PRIMARY KEY,
artistname TEXT
);
CREATE TABLE track(
trackid INTEGER,
trackname TEXT,
trackartist INTEGER,
FOREIGN KEY(trackartist) REFERENCES artist(artistid)
);
sqlite3_table_column_metadata() will tell you it's a primary key, autoincrement, etc. but how
do I get the foreign key constraints?
FOREIGN KEY(trackartist) REFERENCES artist(artistid)
I want to be able to get a list back for table "track" that there are foreign keys back to table Artist column artistid?
I don't see an api to do this? I need to do this programmaticlly upon opening the database, for purposes of aggregation.
Thanks.
After using PRAGMA foreign_key_list(Valuation);
I got back:
PRAGMA foreign_key_list(Valuation);
0|0|Stock|StockId|Id|NO ACTION|NO ACTION|NONE
I understand I need to split on the vertical bar, but what are the first two columns? 0|0 ?
Please note that (foreign) keys can consist of multiple columns, so it would not make sense to return this as column information.
To get information about a table's foreign keys, use this:
PRAGMA foreign_key_list(table-name);
This pragma returns one row for each foreign key constraint created by a REFERENCES clause in the CREATE TABLE statement of table "table-name".
I get a syntax error near AUTOINCREMENT. What is the cause of this error?
CREATE TABLE person (
id INTEGER NOT NULL AUTOINCREMENT,
name TEXT NOT NULL
);
CREATE TABLE department (
id INTEGER NOT NULL AUTOINCREMENT,
name TEXT NOT NULL,
FOREIGN KEY (leader) REFERENCES person(id)
);
According to SQLite FAQ you have to declare either a INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT column to achieve that.
In SQLite you need not to specify AUTOINCREMENT if you are specifying a column as Primary Key...
SQLite AUTOINCREMENT : You Should Avoid Using It
Unless you create a table specifying the WITHOUT ROWID option, you get an implicit auto increment column called rowid.
The rowid column store 64-bit signed integer that uniquely identifies a row within the table.
It's an easy solution. Just use AUTOINCREMENT instead of AUTO_INCREMENT