near ",": syntax error using foreign keys [closed] - sqlite

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I keep getting the issue syntax error on the CREATE TABLE lines for all three tables that use the foreign key statements. I've been troubleshooting for a while and I'm stressing out as it's my first programming uni assignment.
PRAGMA foreign_keys = ON;
CREATE TABLE Customer_info (
customer_id VARCHAR(32) PRIMARY KEY,
name VARCHAR(255),
address VARCHAR(255),
login_name VARCHAR(255)
);
CREATE TABLE Payment_method (
payment_id VARCHAR(32) PRIMARY KEY,
billing_adress VARCHAR(255),
card_holder_name VARCHAR(255),
credit_card_nr INT,
cvs INT,
exp_date DATE
);
CREATE TABLE Product_list (
p_name VARCHAR(255) PRIMARY KEY,
p_price INT,
publisher VARCHAR(255),
developer_studio VARCHAR(255)
);
CREATE TABLE Games (
game_name VARCHAR(255) PRIMARY KEY,
game_desc VARCHAR(255),
FOREIGN KEY (game_name),
REFERENCES Product_list (p_name)
);
CREATE TABLE Software (
soft_name VARCHAR(255) PRODUCT KEY,
soft_desc VARCHAR(255),
FOREIGN KEY (soft_name),
REFERENCES Product_list (p_name)
);
CREATE TABLE Prof_info (
profile_id VARCHAR(32) PRIMARY KEY,
nickname VARCHAR(255),
display_name VARCHAR(255),
description VARCHAR(255),
FOREIGN KEY (profile_id),
REFERENCES Customer_info (customer_id)
);
CREATE TABLE Groups (
group_id VARCHAR(32) PRIMARY KEY,
g_name VARCHAR(255),
g_desc VARCHAR(255)
);
Also I apologise if these are easy questions but I am sleep deprived, stressed & running out of time.
edit: removed the second question I had at the end as it was pointed out to me this isn't supposed to be done.

foreign key ... references is a single expression.
FOREIGN KEY (profile_id) REFERENCES Customer_info(customer_id)
SQLite does not require a separate foreign key clause. You can do this inline.
-- A primary key that is also a foreign key is a bit odd.
game_name VARCHAR(255) PRIMARY KEY REFERENCES Customer_info(customer_id)
See table-constraint and foreign-key-clause in the SQLite create table grammar.
Also would it be possible to state 2 foreign keys referencing different tables within the same table? How would I go about doing this?
Yes, just declare two foreign keys. Perhaps you're confused because you're making your foreign keys also your primary keys.
Here's a sketch of a better design. All of the pieces get their own table. All tables get an auto-incremented integer primary key which is assured to be unique and unchanging. Column names do not use prefixes, fully qualify them in queries if necessary. Table names are normalized as plurals.
create table publishers (
id integer primary key,
name varchar(255) not null
);
create table developers (
id integer primary key,
name varhcar(255) not null
);
create table products (
id integer primary key,
name varchar(255) not null,
price numeric(6,2),
publisher_id integer references publishers(id),
developer_id integer references developers(id)
);
create table games (
id integer primary key,
name varchar(255) not null,
description varchar(255),
product_id integer references products(id)
);

Related

Sqlite foreign key mismatch?

I've read this question and understood the referenced foreign keys to be unique, but somehow the insertion to table are still throwing foreign key mismatch errors:
CREATE TABLE medication (
med_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
med_name VARCHAR (20) NOT NULL,
dosage VARCHAR (10)
);
CREATE TABLE disease (
dis_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
disease_name VARCHAR (20) NOT NULL
);
CREATE TABLE dis_med (
disease_id int NOT NULL,
medication_id int NOT NULL,
CONSTRAINT PK_dis_med PRIMARY KEY (disease_id, medication_id),
CONSTRAINT FK_dis FOREIGN KEY (disease_id) REFERENCES disease (dis_id),
CONSTRAINT FK_med FOREIGN KEY (medication_id) REFERENCES medication (med_id));
CREATE TABLE user_disease (
user_id REFERENCES user (user_id),
dis_id REFERENCES disease (dis_id),
med_id REFERENCES dis_med(medication_id),
CONSTRAINT PK_dis_user PRIMARY KEY (user_id, dis_id)
);
Through the list in the question I cited:
the parent table (medication, disease) exists.
the parent columns exist
the child table references all of the primary key columns in the parent table
Update1
I was able to insert data and bypass the error by altering the user_disease table by composite foreign key. I'd appreciate it if someone can point out what's the best design here. Many thanks in advance!
CREATE TABLE user_disease (
user_id REFERENCES user (user_id),
dis_id REFERENCES disease (dis_id),
med_id REFERENCES dis_med(medication_id),
CONSTRAINT FK_dis_med FOREIGN KEY REFERENCES dis_med(disease_id, medication_id),
CONSTRAINT PK_dis_user PRIMARY KEY (user_id, dis_id)
);
From SQLite Foreign Key Support/3. Required and Suggested Database Indexes:
Usually, the parent key of a foreign key constraint is the primary key of the parent table.
If they are not the primary key, then the parent key columns must be collectively
subject to a UNIQUE constraint or have a UNIQUE index.
With this:
CREATE TABLE user_disease (
...........................
med_id REFERENCES dis_med(medication_id),
...........................
);
the column med_id of user_disease references the column medication_id of dis_med, which is not the PRIMARY KEY of dis_med and there is no UNIQUE constraint for it. It just references med_id of medication .
Why do you need the column med_id in user_disease?
You have dis_id referencing disease, which may also be used to retrieve from dis_med (all) the row(s) from dis_med for that disease.

Unable to insert data into table with foreign key in oracle11g

CREATE TABLE Client_master (
Client_no varchar(6) PRIMARY KEY,
Name varchar(15) NOT NULL,
City varchar(15),
Pincode number(8),
State varchar(15),
Bal_due Number(10,2),
CHECK(Client_no LIKE 'C%'));
INSERT INTO
Client_master(Client_no,Name,City,Pincode,State,Bal_due)
VALUES('C00001','Ivan Bayross','Bombay','400054','Maharashtra',15000);
1 row created.
CREATE TABLE Sales_order(
Order_no varchar(6) PRIMARY KEY REFERENCES Client_master (Client_no),
Order_date date,
Client_no varchar(6),
Dely_type char(1) DEFAULT 'f',
Billed_yn char(1),
Salesman_no varchar(6),
Dely_date date,
Order_status varchar(10),
CHECK(Order_no LIKE 'O%'),
CHECK(Order_status IN ('inprocess','backorder','cancelled')),
CHECK(Dely_date>Order_date));
INSERT INTO Sales_order(Order_no, Order_date, Client_no, Dely_type, Billed_yn, Salesman_no, Dely_date, Order_status)
VALUES('C19001','12-jan-96','C00001','f','n','S0001','20-jan-96','inprocess');
INSERT INTO
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.SYS_C007155) violated - parent key not
found
Please help me insert data into Child table.
What's this SYSTEM.SYS_C007155 error? Why this error message "parent key is not found"?
The parent key not found is because you have a foreign key in your sales_order table which is pointing to the client_master, and there is no matching key value - exactly what it says. But it's because you've done something odd:
Sales_order(Order_no varchar(6) PRIMARY KEY REFERENCES Client_master (Client_no)
You've made the order_no the primary key for this table, but also made it a foreign key to the client_no on the other table. Your insert uses order_no of 'C19001', which doesn't match the client_no you previously inserted into the parent table.
You almost certainly wanted sales_order.client_no to be a foreign key to client_master.client_no, so you would have the references... against that column:
CREATE TABLE Sales_order(Order_no varchar(6) PRIMARY KEY,
Order_date date,
Client_no varchar(6) REFERENCES Client_master (Client_no),
...
You have a further problem as the check constraint you have against the order_no is CHECK(Order_no LIKE 'O%') but as mentioned the value you're putting in is 'C19001', which doesn't match the pattern. Presumably you meant the insert to be for 'O19001'; if not then the constraint is defined incorrectly. I actually hit that check constraint before the primary key constrain in 11gR2, so you may just have changed that while posting the question.
You can look in the user_constraints and user_cons_columns views to see what a constraint is doing, but you'll find it easier if you name your constraints instead of letting Oracle give them default names like SYS_C007155:
Specify a name for the constraint. If you omit this identifier, then Oracle Database generates a name with the form SYS_Cn. Oracle stores the name and the definition of the integrity constraint in the USER_, ALL_, and DBA_CONSTRAINTS data dictionary views (in the CONSTRAINT_NAME and SEARCH_CONDITION columns, respectively).
For example:
CREATE TABLE Client_master (
Client_no varchar(6),
Name varchar(15) NOT NULL,
City varchar(15),
Pincode number(8),
State varchar(15),
Bal_due Number(10,2),
CONSTRAINT Client_master_pk PRIMARY KEY (Client_no),
CONSTRAINT Client_master_chk_no CHECK(Client_no LIKE 'C%'));
INSERT INTO Client_master(Client_no,Name,City,Pincode,State,Bal_due)
VALUES('C00001','Ivan Bayross','Bombay','400054','Maharashtra',15000);
1 row inserted.
CREATE TABLE Sales_order (
Order_no varchar(6),
Order_date date,
Client_no varchar(6),
Dely_type char(1) DEFAULT 'f',
Billed_yn char(1),
Salesman_no varchar(6),
Dely_date date,
Order_status varchar(10),
CONSTRAINT Sales_order_pk PRIMARY KEY (Order_no),
CONSTRAINT Sales_order_fk_client FOREIGN KEY (Client_no)
REFERENCES Client_master (Client_no),
CONSTRAINT Sales_order_chk_no CHECK (Order_no LIKE 'O%'),
CONSTRAINT Sales_order_shk_status CHECK
(Order_status IN ('inprocess','backorder','cancelled')),
CONSTRAINT Sales_order_chk_dates CHECK(Dely_date>Order_date));
INSERT INTO Sales_order(Order_no, Order_date, Client_no, Dely_type, Billed_yn, Salesman_no, Dely_date, Order_status)
VALUES('O19001','12-jan-96','C00001','f','n','S0001','20-jan-96','inprocess');
1 row inserted.
If you do have a violation, e.g. with your original order_no value, you'd see a more useful message:
INSERT INTO Sales_order(Order_no, Order_date, Client_no, Dely_type, Billed_yn, Salesman_no, Dely_date, Order_status)
VALUES('C19001','12-jan-96','C00001','f','n','S0001','20-jan-96','inprocess');
ORA-02290: check constraint (YOUR_SCHEMA.SALES_ORDER_CHK_NO) violated
so you can have a better idea which constraint is violated, and what that means, without having to loo in the data dictionary. SALES_ORDER_CHK_NO is easier to interpret than SYS_C007155 Use names that make sense for you of course.
You can name inline constraints too:
CREATE TABLE Client_master (
Client_no varchar(6) CONSTRAINT Client_master_pk PRIMARY KEY,
...
CREATE TABLE Sales_order (
Order_no varchar(6) CONSTRAINT Sales_order_pk PRIMARY KEY,
Order_date date,
Client_no varchar(6) CONSTRAINT Sales_order_fk_client REFERENCES Client_master (Client_no),
...
but it might be clearer and easier to maintain with them all grouped together at the end.

In sqlite is it possible to cross-reference to a foreign Key

I will apologise now coming from a c++ opp design pattern
For the example three table database (non-Key fields omitted) is it possible sane to cross-reference another table in the REFERENCES section?
CREATE TABLE `Attempt` (
`attempt_ID` INTEGER PRIMARY KEY AUTOINCREMENT,
`username` TEXT NOT NULL,
`assessment_ID` INTEGER NOT NULL,
`timestamp` INTEGER NOT NULL,
FOREIGN KEY(`username`) REFERENCES Students ( username ),
FOREIGN KEY(`assessment_ID`) REFERENCES Assessments ( assessment_ID )
);
CREATE TABLE `Questions` (
`assessment_ID` INTEGER NOT NULL,
`question_number` INTEGER NOT NULL,
PRIMARY KEY(assessment_ID,question_number)
);
CREATE TABLE "Responses" (
`attempt_ID` INTEGER,
`question_number` INTEGER,
PRIMARY KEY(attempt_ID,question_number),
FOREIGN KEY(`attempt_ID`) REFERENCES Attempt ( attempt_ID )
);
The obvious solution is to include the assessment_ID in every question.row
but if we already know the attempt_ID then we can lookup the assessment ID ?
CREATE TABLE "Responses" (
`attempt_ID` INTEGER,
`assessment_ID` INTEGER,
`question_number` INTEGER,
PRIMARY KEY(attempt_ID,question_number),
FOREIGN KEY(`attempt_ID`) REFERENCES Attempt ( attempt_ID )
FOREIGN KEY(`assessment_ID`,`question_number`) REFERENCES `Questions` ( `assessment_ID`,`question_number` )
);
Sub_Reference maybe ?
What you seem to want to do isn't "sane".
This is what you seem to want to do in the Response table.
FOREIGN KEY(question_number)
REFERENCES Questions(assessment_ID, question_number)
That's not "sane". (That's not SQL.) One column (question_number) cannot reference two columns (assessment_ID, question_number).
It's hard to say what you need to do to fix this. The difference between an attempt and a response isn't clear. If a response is a response to a question, then your Responses table should probably look something like this.
CREATE TABLE responses (
attempt_ID integer not null,
assessment_ID integer not null,
question_number integer not null,
primary key (attempt_ID, assessment_ID, question_number),
foreign key (attempt_ID) references Attempt (attempt_ID ),
foreign key (assessment_ID, question_number)
references questions (assessment_ID, question_number)
);
The obvious solution is to include the assessment_ID in every
question.row but if we already know the attempt_ID then we can lookup
the assessment ID?
Whether you can find some way to navigate to the information you want isn't really relevant. In fact, this kind of thing is a problem that the relational model of data was developed to solve. Nowadays, we design databases based on functional dependencies and normalization.
Also, if you're designing a new database in SQLite, you probably shouldn't write the code using MySQL syntax.

SQLite3 syntax error trying to create foreign key

I am trying to create a bunch of tables in sqlite3 and I am getting an error that I can't fix. Something to do with my syntax for sqlite3 for foreign keys but can't figure it out.
CREATE TABLE students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name VARCHAR(64),
last_name VARCHAR(64)
);
CREATE TABLE classes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
class_name VARCHAR(64)
);
CREATE TABLE students_classes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
students_id INTEGER,
classes_id INTEGER,
FOREIGN KEY (students_id) REFERENCES students(id),
FOREIGN KEY (classes_id) REFERENCES classes(id)
);
CREATE TABLE teachers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
classes_id INTEGER,
first_name VARCHAR(64),
last_name VARCHAR(64),
FOREIGN KEY classes_id REFERENCES classes(id)
);
CREATE TABLE grades (
id INTEGER PRIMARY KEY AUTOINCREMENT,
students_id INTEGER,
grade_num INTEGER,
FOREIGN KEY students_id REFERENCES students(id)
);
Error: near "students_id": syntax error
SQLite Docs seem to indicate that column name(s) in FOREIGN Key constraint must be inside parenthesis:
I'm not sure how strictly it's actually enforced. My SQL Fiddle shows that using parenthesis should solve your problem.
Please note that SQL Fiddle for SQLite is emulated, so your result might still vary.

SQLite3, creating a relation one to many

I'm creating an sqlite3 database with two tables. This is how I want it to be:
create table user(
id NOT NULL,
name varchar(40),
password varchar(40)
)
create table users_data(
id NOT NULL
note_name varchar(40),
note_description varchar(255),
)
and I want it to use one to many relation, so that one user has many note_name and note_description. How do I do this using SQlite?
You add the user id to the table users_data, because every users_data belongs to a user. That's it. Then let the database system help you guarantee data integrity with primary and foreign keys:
create table user(
id integer PRIMARY KEY,
name varchar(40),
password varchar(40)
);
create table users_data(
id integer PRIMARY KEY,
id_user integer NOT NULL,
note_name varchar(40),
note_description varchar(255),
FOREIGN KEY(id_user) REFERENCES user(id)
);
Read more on foreign keys here: https://www.sqlite.org/foreignkeys.html

Resources