Line 10: ERROR: ORA-00907 missing right parenthesis | Oracle 11g - oracle11g

I am trying to create table but it shows error, "ORA-00907: missing right parenthesis."
CREATE TABLE member
(member_id NUMBER(10)
CONSTRAINT member_member_id_pk PRIMARY KEY,
last_name VARCHAR2(25)
CONSTRAINT member_last_name_nn NOT NULL,
first_name VARCHAR2(25),
address VARCHAR2(100),
city VARCHAR2(30),
phone VARCHAR2(15),
join_date DATE DEFAULT SYSDATE
CONSTRAINT member_join_date_nn NOT NULL);
CREATE TABLE title
(title_id NUMBER(10)
CONSTRAINT title_title_id_pk PRIMARY KEY,
title VARCHAR2(60)
CONSTRAINT title_title_nn NOT NULL,
description VARCHAR2(400)
CONSTRAINT title_description_nn NOT NULL,
rating VARCHAR2(4)
CONSTRAINT title_rating_ck CHECK
(rating IN ('G', 'PG', 'R', 'NC17', 'NR')),
category VARCHAR2(20),
CONSTRAINT title_category_ck CHECK
(category IN ('DRAMA', 'COMEDY', 'ACTION',
'CHILD', 'SCIFI', 'DOCUMENTARY')),
release_date DATE);
CREATE TABLE title_copy
(copy_id NUMBER(10),
title_id NUMBER(10)
CONSTRAINT title_copy_title_if_fk REFERENCES title(title_id),
status VARCHAR2(15)
CONSTRAINT title_copy_status_nn NOT NULL
CONSTRAINT title_copy_status_ck CHECK (status IN
('AVAILABLE', 'DESTROYED','RENTED', 'RESERVED')),
CONSTRAINT title_copy_copy_id_title_id_ pk
PRIMARY KEY (copy_id, title_id));
First two tables were successfully created, the third one 'title_copy' shows error at line 10 which is PRIMARY KEY (copy_id, title_id));

Replace..:
[...]
CONSTRAINT title_copy_status_ck CHECK (status IN
('AVAILABLE', 'DESTROYED','RENTED', 'RESERVED')),
CONSTRAINT title_copy_copy_id_title_id_ pk
PRIMARY KEY (copy_id, title_id));
... by:
[...]
CONSTRAINT title_copy_status_ck CHECK (status IN
('AVAILABLE', 'DESTROYED','RENTED', 'RESERVED')),
CONSTRAINT title_copy_copy_id_title_id_pk
PRIMARY KEY (copy_id, title_id));
Note a removed space.

Related

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.

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.

Recursive foreign key 'on delete cascade'

CREATE TABLE IF NOT EXISTS type (
tid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (tid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_type_0 ON type (tid,uuid,name);
CREATE TABLE IF NOT EXISTS object (
oid VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
tid INTEGER NOT NULL,
CONSTRAINT FK_tid FOREIGN KEY (tid) REFERENCES type(tid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid)
);
CREATE INDEX IDX_object_0 ON object (oid,timestamp,tid);
CREATE TABLE IF NOT EXISTS object_user_owner (
uid INTEGER NOT NULL,
oid VARCHAR NOT NULL,
CONSTRAINT FK_uid FOREIGN KEY (uid) REFERENCES user(uid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT FK_oid FOREIGN KEY (oid) REFERENCES object(oid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid,uid)
);
CREATE INDEX IDX_object_user_owner_0 ON object_user_owner (uid,oid);
CREATE TABLE IF NOT EXISTS user (
uid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
password VARCHAR NOT NULL,
salt VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
lastaccess VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (uid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_user_0 ON user (uid,uuid,name);
The above three sqlite3 tables contain foreign keys. The problem is the deletion of a key in the upper table type. When I try do delete I get 'FOREIGN KEY constraint failed' Error. Deleting a from the lowest table object_user_owner before deleting a type works. I think sqlite does not check any recursive cascade constraints. Does anyone have experienced this too or is anything wrong with my design?

Oracle SQL classify Subtype

CREATE TABLE Customer
(
CustomerID NUMBER NOT NULL,
AccountID NUMBER(10) NOT NULL,
CustomerType VARCHAR(10) NOT NULL,
CustomerStatus VARCHAR(10) NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY (CustomerID),
CONSTRAINT check_customer_status CHECK(CustomerStatus IN ('Ineligible', 'Eligible')),
CONSTRAINT check_customer_type CHECK(CustomerType IN ('NonResident', 'Residential'))
);
Alter table CUSTOMER add CONSTRAINT res_customer_type UNIQUE (CustomerID, CustomerType)
CREATE TABLE nonresidential
(
CustomerID NUMBER NOT NULL,
CustomerType VARCHAR(10) NOT NULL,
BusinessName VARCHAR(10) NOT NULL,
ABNNumber NUMBER(10),
Email VARCHAR(35),
CONSTRAINT pk_nrtable PRIMARY KEY (CustomerID),
CONSTRAINT nonres_type CHECK (CustomerType = 'nonresident'),
CONSTRAINT res_customer_type UNIQUE (CustomerID, CustomerType) REFERENCES Customer(CustomerID, CustomerType)
);
The last line keeps coming up as missing parenthesis (around the references line along the unique constraint). I'd like to specify the unique constraint to create a subtype table under customer.
P.S I'm using oracle sql, thanks
You use the Unique keyword where you should use the Foreign key keyword.
CONSTRAINT res_customer_type UNIQUE (CustomerID, CustomerType) REFERENCES Customer(CustomerID, CustomerType)
should be
CONSTRAINT res_customer_type FOREIGN KEY (CustomerID, CustomerType) REFERENCES Customer(CustomerID, CustomerType)

Foreign Key Constraint in oracle

I have two tables Cal and EEL
I want to use the primary key of cal that is Cal_id as the foreign key for EEl
Here's what I tried.
Create table ELL
(course_code varcahr2(10) Constraints pk_course_code Primary Key,
Course_Title varchar2(30),
cal2_idnumber not null,
Constraint fk_cal2 Foreign Key (cal_id) References cal_id(cal2_id)
)
but it shows error at line 6 Ora-00904 "Cal_ID" invalid character
can someone tell me how to do this
ALTER TABLE table_name
add CONSTRAINT constraint_name
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n);
Not difficult, here below an example:
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
);
References cal_id(cal2_id) -- call_id is not your table name.
Instead of above code you can use as below.
References parent_table_name(cal2_id)
Constraint fk_cal_id2 Foreign Key (cal2_id) References cal(cal_id)
----------- constraint name (col in EEL) parent table name(parent table column name)

Resources