I have a problem with foreign key constraintt - oracle11g

ALTER TABLE class_s19_BM ADD CONSTRAINT fk_std_class FOREIGN KEY(std_id) REFERENCES student_s19_BM (std_id); /
ERROR at line 1:
ORA-00904: "STD_ID": invalid identifier

Related

AUTOINCREMENT and order of arguments in INTEGER PRIMARY KEY

In SQLite, the following works:
CREATE TABLE "b" (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL);
but the following throws an error:
CREATE TABLE "c" (id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL);
the error being:
Execution finished with errors.
Result: near "AUTOINCREMENT": syntax error
At line 6:
CREATE TABLE "c" (id INTEGER AUTOINCREMENT
Why changing the order of the arguments throws a syntax error?
As specified in the docs, in SQLite AUTOINCREMENT must go after PRIMARY KEY.

Strange org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (foreign key mismatch -

I'm running into org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (foreign key mismatch - ... with a statement, that proceeds without any complaints using the normal SQLite-frontend. This creates the crucial part of my database:
CREATE TABLE IF NOT EXISTS artists (
aid INTEGER PRIMARY KEY AUTOINCREMENT,
aname VARCHAR(200) NOT NULL,
CONSTRAINT one UNIQUE(aname)
);
CREATE TABLE IF NOT EXISTS discs (
did INTEGER PRIMARY KEY AUTOINCREMENT,
testAddCD1 BIGINT(10) NOT NULL,
dtitle VARCHAR(125) NOT NULL,
dreleaseyear YEAR(4) DEFAULT NULL,
dlang VARCHAR(3) DEFAULT NULL
);
CREATE TABLE IF NOT EXISTS tracks (
discs_did INTEGER NOT NULL,
tnumber INT(4) NOT NULL,
ttitle VARCHAR(125) NOT NULL,
tseconds INT(4) NOT NULL,
CONSTRAINT pk PRIMARY KEY(discs_did, tnumber),
CONSTRAINT fk FOREIGN KEY(discs_did) REFERENCES discs(did) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT val CHECK(tseconds> 0)
);
CREATE TABLE IF NOT EXISTS track_by_artist (
discs_did INTEGER NOT NULL,
tracks_tnumber INT(4) NOT NULL,
artists_aid INTEGER NOT NULL,
CONSTRAINT pk PRIMARY KEY(discs_did, tracks_tnumber, artists_aid),
CONSTRAINT fk1 FOREIGN KEY(discs_did) REFERENCES discs(did) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT fk2 FOREIGN KEY(tracks_tnumber) REFERENCES tracks(tnumber) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT fk3 FOREIGN KEY(artists_aid) REFERENCES artists(aid) ON DELETE RESTRICT ON UPDATE RESTRICT
The database gets created and the JDBC-driver inserts an artist, a disc and the disc's tracks - all good. The final insert should assign an artist to a track and looks like
INSERT INTO track_by_artist (discs_did, tracks_tnumber, artists_aid) VALUES (1, 1, 1);
Using the JDBC this yields
SQLite-Error #1
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (foreign key mismatch - "track_by_artist" referencing "tracks")
at org.sqlite.core.DB.newSQLException(DB.java:1012)
at org.sqlite.core.DB.newSQLException(DB.java:1024)
at org.sqlite.core.DB.throwex(DB.java:989)
at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
at org.sqlite.core.NativeDB.prepare(NativeDB.java:134)
at org.sqlite.core.DB.prepare(DB.java:257)
at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:45)
at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:25)
at org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:35)
at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:241)
at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:205)
Issuing the same SQL-Insert with SQLite's text-frontend works like cream.
I'm a little lost and don't know what to do about my Java-code.
Some advise, pls?
Chris
The problem is that in track_by_artist you defined this foreign key constraint:
CONSTRAINT fk2 FOREIGN KEY(tracks_tnumber) REFERENCES tracks(tnumber) ON DELETE RESTRICT ON UPDATE RESTRICT
although tnumber in tracks is not UNIQUE (and it shouldn't be).
A foreign key's parent must be defined as UNIQUE.
In tracks the PRIMARY KEY is defined as the combination of discs_did and tnumber, which makes sense, so the combination of these 2 columns is unique.
What you can do is define in track_by_artist a composite foreign key for the columns discs_did and tracks_tnumber that reference discs_did and tnumber of tracks:
CREATE TABLE IF NOT EXISTS track_by_artist (
discs_did INTEGER NOT NULL,
tracks_tnumber INT(4) NOT NULL,
artists_aid INTEGER NOT NULL,
CONSTRAINT pk PRIMARY KEY(discs_did, tracks_tnumber, artists_aid),
CONSTRAINT fk1 FOREIGN KEY(discs_did, tracks_tnumber) REFERENCES tracks(discs_did, tnumber) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT fk2 FOREIGN KEY(artists_aid) REFERENCES artists(aid) ON DELETE RESTRICT ON UPDATE RESTRICT
);
This way you don't need a separate foreign key definition for discs_did.

how can enable,disable constraints if table has partitions by reference?

I am using oracle12C as database and using get_ddl for getting ddl of database objects.Now i have two tables Table1 and Table2 and Table1 has partition, Table2 is using primary constraint of Table1 as reference constraint and also using PARTITIONED BY REFERENCE.For eg :-
TABLE1 :-
create table parent_emp(
empno number primary key,
job varchar2(20),
sal number(7,2),
deptno number(2)
)
partition by list(job)(
partition p_job_dba values ('DBA'),
partition p_job_mgr values ('MGR'),
partition p_job_vp values ('VP')
);
TABLE2 :-
CREATE TABLE "SECONDARYUSER"."REFERENCE_EMP"(
"ENAME" VARCHAR2(10),
"EMP_ID" NUMBER,
"EMPNO" NUMBER,
CONSTRAINT "FK_EMPNO" FOREIGN KEY ("EMPNO")
REFERENCES "SECONDARYUSER"."PARENT_EMP" ("EMPNO") ENABLE
)
PARTITION BY REFERENCE ("FK_EMPNO")(
PARTITION "P_JOB_DBA" ,
PARTITION "P_JOB_MGR" ,
PARTITION "P_JOB_VP" )
My issue is I want to disable-enable constraints from Table1 and Table2 but when I execute following script to disable Table1 primary key:
alter table parent_emp disable constraint SYS_C0010720 cascade;
it gives following error:-
02297. 00000 - "cannot disable constraint (%s.%s) - dependencies exist"
*Cause: an alter table disable constraint failed becuase the table has
foriegn keys that are dpendent on this constraint.
*Action: Either disable the foreign key constraints or use disable cascade
So I tried to disable Table2 foreign key constraint and execute the following query:
alter table reference_emp disable constraint FK_EMPNO cascade;
but it gave the following error:-
alter table reference_emp disable constraint FK_EMPNO cascade
Error report:
SQL Error: ORA-14650: operation not supported for reference-partitioned tables
Please suggest me how can I disable-enable constraints in this condition.

Unable to sync Entities with database in Symfony

I am starting to explore Symfony and after create the schema by importing this sql script and create the entities following these commands:
Importing mapping information by generating the metadata files:
$ php app/console doctrine:mapping:import --force AcmeBlogBundle xml
Build related entity classes:
-> Generates entity classes with annotation mappings
$ php app/console doctrine:mapping:convert annotation ./src
$ php app/console doctrine:generate:entities AcmeBlogBundle
The command:
$ php app/console doctrine:schema:update --force
fails with the following error:
[Doctrine\DBAL\Exception\DriverException]
An exception occurred while executing 'ALTER TABLE writtings CHANGE id id INT AUTO_INCREMENT NOT NULL':
SQLSTATE[HY000]: General error: 1025 Error on rename of './amimusa/#sql-425_5e' to './amimusa/writtings' (errno: 150)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './amimusa/#sql-425_5e' to './amimusa/writtings' (errno: 150)
[PDOException]
SQLSTATE[HY000]: General error: 1025 Error on rename of './amimusa/#sql-425_5e' to './amimusa/writtings' (errno: 150)
The dump file generated is:
ALTER TABLE writtings CHANGE id id INT AUTO_INCREMENT NOT NULL;
ALTER TABLE writtings ADD CONSTRAINT FK_F9A6AFF48726D6E4 FOREIGN KEY (publication_type) REFERENCES publications_type (id);
ALTER TABLE musas CHANGE id id INT AUTO_INCREMENT NOT NULL;
ALTER TABLE publications DROP FOREIGN KEY contributor_FK;
ALTER TABLE publications DROP FOREIGN KEY state_FK;
ALTER TABLE publications DROP FOREIGN KEY writting_FK;
ALTER TABLE publications CHANGE id id INT AUTO_INCREMENT NOT NULL, CHANGE id_contributor id_contributor INT DEFAULT NULL, CHANGE id_state id_state INT DEFAULT NULL, CHANGE id_writting id_writting INT DEFAULT NULL;
ALTER TABLE publications ADD CONSTRAINT FK_32783AF440C1075C FOREIGN KEY (id_writting) REFERENCES writtings (id);
ALTER TABLE publications ADD CONSTRAINT FK_32783AF44D1693CB FOREIGN KEY (id_state) REFERENCES states (id);
ALTER TABLE publications ADD CONSTRAINT FK_32783AF4C27D5A64 FOREIGN KEY (id_contributor) REFERENCES contributors (id);
ALTER TABLE publications_musas DROP FOREIGN KEY musa_FK;
ALTER TABLE publications_musas DROP FOREIGN KEY publication_FK;
ALTER TABLE publications_musas DROP FOREIGN KEY musa_FK;
ALTER TABLE publications_musas CHANGE id_publication id_publication INT NOT NULL, CHANGE id_musa id_musa INT NOT NULL;
ALTER TABLE publications_musas ADD CONSTRAINT FK_7EF2161FB72EAA8E FOREIGN KEY (id_publication) REFERENCES publications (id);
ALTER TABLE publications_musas ADD CONSTRAINT FK_7EF2161FFB53D80 FOREIGN KEY (id_musa) REFERENCES musas (id);
DROP INDEX musa_fk ON publications_musas;
CREATE INDEX IDX_7EF2161FFB53D80 ON publications_musas (id_musa);
ALTER TABLE publications_musas ADD CONSTRAINT musa_FK FOREIGN KEY (id_musa) REFERENCES musas (id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE contributors CHANGE id id INT AUTO_INCREMENT NOT NULL;
ALTER TABLE states CHANGE id id INT AUTO_INCREMENT NOT NULL;
Anybody could help me to understand why the update fails?
Also I can't figure out why these operations are required since everything has been created from scratch.
As Cerad commented:
The reverse engineering stuff is not perfect. Doctrine likes to have
things defined it's own way. I'd suggest dropping the database then
use doctrine:schema:create to rebuild it the way doctrine wants it to
be.
$ php app/console doctrine:schema:drop --force
$ php app/console doctrine:schema:create
Has fixed the issue.

insert fail in a table that have composite key in Doctrine-symfony2

when i insert in a table that has composite key of two fields , it fail and say it is null insertion , once i delete the composite key it insert successfully.
An exception occurred while executing 'INSERT INTO course_to_category (courseId, categoryId, ordering) VALUES (?, ?, ?)' with params [null, null, 1]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'courseId' cannot be null
Seems like your composite key is primary key. Check this:
A PRIMARY KEY is a unique index where all key columns must be defined as NOT NULL. If they are not explicitly declared as NOT NULL, MySQL declares them so implicitly (and silently). A table can have only one PRIMARY KEY. The name of a PRIMARY KEY is always PRIMARY, which thus cannot be used as the name for any other kind of index.
http://dev.mysql.com/doc/refman/5.1/en/create-table.html

Resources