I am creating two tables. The first table creates with no errors, but when I try to create the SUBHEAD table, I get an error: Line 2, Missing right parenthesis. I am not sure what is wrong with this line. Below is my SQL statements:
CREATE TABLE HEAD
(Code NUMERIC(4,0) NOT NULL PRIMARY KEY,
HeadName VARCHAR(50) NOT NULL UNIQUE,
HType VARCHAR(1) NOT NULL,
HDate DATE NOT NULL,
OpBal DECIMAL(11,2) NOT NULL
);
CREATE TABLE SUBHEAD
(HCode NUMERIC(4,0) NOT NULL FOREIGN KEY REFERENCES HEAD(Code),
SubCode NUMERIC(4,0) NOT NULL,
SubName VARCHAR(50) NOT NULL,
SDate DATE NOT NULL,
OpBal DECIMAL (11,2) NOT NULL,
CONSTRAINT pk_subheadID PRIMARY KEY (HCode, SubCode)
);
syntax: http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#SQLRF52167
note that "in-line" constraint syntax is : "col col_type REFERENCES TAB(COL)" and not "FOREIGN KEY"
The data type for number is NUMBER not NUMERIC
So my memory did not fool me - you can have multiple in-line constraints (although the syntax graph doesn't show it):
SQL> create table tt1(a number primary key);
Table created.
SQL> create table tt2(a number references tt1(a) not null);
Table created.
CREATE TABLE SUBHEAD
(HCode NUMERIC(4,0) NOT NULL, FOREIGN KEY (Hcode) REFERENCES HEAD(Code),
SubCode NUMERIC(4,0) NOT NULL,
SubName VARCHAR(50) NOT NULL,
SDate DATE NOT NULL,
OpBal DECIMAL (11,2) NOT NULL,
CONSTRAINT pk_subheadID PRIMARY KEY (HCode, SubCode)
);
(note comma, and reference to the new table column)
Related
I have two tables:
CREATE TABLE Trasy_srodki_transportu(
ID_Trasy Integer NOT NULL,
ID_pojazdu Integer NOT NULL
)
/
CREATE TABLE Trasy(
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Now i want to add foreign key to Trasy_srodki_transportu referencing to Trasy table:
ALTER TABLE Trasy_srodki_transportu ADD CONSTRAINT Trasa_jest_wykorzystywana FOREIGN KEY (ID_Trasy) REFERENCES Trasy (ID_Trasy)
/
and this throws Oracle (ORA-02270) : no matching unique or primary key for this column-list error. Any suggestions how to fix this?Data modeler view
A foreign key needs to reference a key on the related table, but it's not the case in your example. Change the definition of the second table by adding a PRIMARY KEY constraint in it, as in:
CREATE TABLE Trasy (
ID_Trasy Integer PRIMARY KEY NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Alternatively, you can create a unique constraint on it, that can also serve as a key. For example:
CREATE TABLE Trasy (
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL,
CONSTRAINT uq_idtrasy UNIQUE (ID_Trasy)
)
CREATE TABLE medico
(num_cedula serial NOT NULL UNIQUE,
nome varchar(20) NOT NULL,
especialidade varchar(20) NOT NULL,
PRIMARY KEY(num_cedula));
CREATE TABLE consulta
(num_cedula serial NOT NULL,
num_doente serial NOT NULL,
data_consulta date NOT NULL,
nome varchar(20) NOT NULL,
CHECK(EXTRACT(DOW FROM data_consulta) NOT IN (6,0)),
UNIQUE(num_doente,data_consulta,nome),
FOREIGN KEY(nome) REFERENCES instituicao(nome) ON UPDATE CASCADE,
FOREIGN KEY(num_cedula) REFERENCES medico(num_cedula) ON UPDATE CASCADE,
PRIMARY KEY(num_cedula,num_doente,data_consulta));
CREATE TABLE analise
(num_analise serial NOT NULL UNIQUE,
especialidade varchar(20) NOT NULL,
num_cedula serial ,
num_doente serial ,
data_analise date ,
data_registo date NOT NULL,
nome varchar(20) NOT NULL,
quant integer NOT NULL,
inst varchar(20) NOT NULL,
CONSTRAINT fk_analise FOREIGN KEY(num_cedula,num_doente,data_analise) REFERENCES consulta(num_cedula,num_doente,data_consulta) ON UPDATE CASCADE,
FOREIGN KEY(inst) REFERENCES instituicao(nome) ON UPDATE CASCADE,
PRIMARY KEY(num_analise));
This is my code but when I try to insert another "analise" with num_cedula,num_doente,data_analise as NULL it gives me this error ("ERROR: null value in column "num_cedula" violates not-null constraint
DETAIL: Failing row contains (32, Ortopedia, null, null, null, 2019-12-02, glicemia, 176, Instituicao1).
SQL state: 23502") and I don't really know why.
Also, this is the insert code I used:
INSERT INTO analise VALUES(32,'Ortopedia',NULL,NULL,NULL,'2019-12-02','glicemia',176,'Instituicao1');
The serial datatype just does not allow null values. The documentation explains what happens under the hood when declaring a serial (emphasis is mine):
Thus, we have created an integer column and arranged for its default values to be assigned from a sequence generator. A NOT NULL constraint is applied to ensure that a null value cannot be inserted.
I don't think that you really want serial here. The use case for that datatype is to create an auto-incremented column. You could probably use int instead, that would avoid the problem you are getting here.
CREATE table Book
(
book_title varchar (100) not null ,
book_genre char(60) not null,
Date_of_publish date not null,
user_code char(7) not null ,
book_id char (7) primary key not null ,
constraint writer__id_fk foreign key (writer_id),
constraint publisher__id_fk foreign key (publisher_id)
);
I'm getting
[ORA-00905: missing keyword]
in publisher table
CREATE table publisher
(
publisher_id char (7) primary key not null,
publisher_name char(20) not null,
publisher_number char(10) not null,
publisher_email varchar2(60) not null,
publisher_address varchar2(60) not null,
);
I'm getting
[ORA-00904: : invalid identifier]
The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
Refer this link for more details
https://www.w3schools.com/sql/sql_foreignkey.asp
Hope this helps.
Welcome to the wonderful world of SQL! :-)
General remark:
Please tell us what kind of DBMS you're using. MySQL? SQL Server? Oracle? SQlite? Different systems use different kinds of syntaxes.
First statement:
The problem seems to be in the FOREIGN KEY-portion.
Usually, you'll state something like:
CONSTRAINT [constraint_name] FOREIGN KEY([column_in_this_table]) REFERENCES OTHER_TABLE([column_in_other_table])
edit (added):
The [column_in_this_table] has to exist in your DDL (CREATE TABLE-statement), like so:
CREATE TABLE Book ( book_title ... etc., publisher_id INT, CONSTRAINT FK_publ_id FOREIGN KEY(publisher_id) REFERENCES publisher(publisher_id));
Here, you'll have a 'original' column called 'publisher_id', in the 'publisher'-table. You refer to it from within the 'Book'-table, by first having a 'publisher_id' column in the 'Book'-table (which should have the same DDL as the original column by the way). Next, you'll add a FOREIGN KEY to the 'Book'-table, that is imposed on the Book(publisher_id) column. Note, that you could also name the column in your 'Book'-table differently -- like, say, 'Spongebob' or 'Patrick'. But for future use, you'd like naming conventions that tell what you might expect to find in a column. So you'd name columns for what they contain.
Second statement:
The problem is with the last portion of your statement, where there's a comma after the NOT NULL portion for column publisher_address.
(Part of) your statement:
publisher_address varchar2(60) not null, );
Try replacing that with:
publisher_address VARCHAR2(60) NOT NULL);
edit (note to self):
VARCHAR2 turns out to be a valid datatype in Oracle databases (see:
Oracle documentation)
For your first table, the Foreign Keys do not reference any table. For your second table, I would imagine that comma after your last column isn't helping anything.
So this is the answer.
CREATE table Book
(
book_title varchar (100) not null ,
book_genre char(60) not null,
Date_of_publish date not null,
user_code char(7) not null ,
publisher_id char (7) not null,
writer_id char(7) not null,
book_id char (7) primary key not null ,
CONSTRAINT book_writer_id_fk FOREIGN KEY(writer_id) REFERENCES writer(writer_id),
CONSTRAINT book_publisher_id_fk FOREIGN KEY(publisher_id) REFERENCES publisher(publisher_id)
);
CREATE table publisher
(
publisher_id char (7) primary key not null,
publisher_name char(20) not null,
publisher_number char(10) not null,
publisher_email varchar2(60) not null,
publisher_address varchar2(60) not null
);
I Got a table BILL which is associated to another table BANK_CARD as follows:
create table BILL (
id_bill BIGSERIAL not null,
id_bank_card BIGSERIAL null,
id_registred_user BIGSERIAL not null,
reference_number INT4 null,
purchase_date DATE null,
bill_status VARCHAR(50) null,
payment_method VARCHAR(50) null,
constraint PK_BILL primary key (id_bill)
);
create table BANK_CARD (
id_bank_card BIGSERIAL not null,
id_registred_user BIGSERIAL not null,
card_type VARCHAR(50) null,
card_number INT4 null,
expiring_date DATE null,
cipher INT4 null,
constraint PK_BANK_CARD primary key (id_bank_card)
);
The table BILL has a 0..1 association with the table BANK_CARD, and BANK_CARD has a 1..n association with table BILL.
But when i execute my sql script i get the following error:
conflicting NULL/NOT NULL declarations for column "id_bank_card" of table "bill"
Because the relationship BILL and BANK_CARD is 0..1 the foreign key id_bank_card can be null in the table bill, so i don't understand why i get this error.
Any help please?
Thanks.
You are confusing data type definitions for primary and foreign keys. A bigserial is a generator of values of the bigint type. The foreign keys should use that data type. See table definitions below. Also, use of the NULL clause is redundant as that is the default behaviour. Primary keys can not be NULL so NOT NULL there is also redundant.
create table bank_card (
id_bank_card bigserial,
id_registred_user bigint references <user table>,
card_type VARCHAR(50),
card_number INT4,
expiring_date DATE,
cipher INT4,
constraint PK_BANK_CARD primary key (id_bank_card)
);
create table bill (
id_bill BIGSERIAL,
id_bank_card bigint references bank_card,
id_registred_user bigint references <user table>,
reference_number INT4,
purchase_date DATE,
bill_status VARCHAR(50),
payment_method VARCHAR(50),
constraint pk_bill primary key (id_bill)
);
If you are using Java+PostgreSQL:
this can happen if you accidentially insert null strings into your statement.
String N = System.getProperty("line.seperator");
Returned null, screwing up my sql statement.
FIXES: (Choose One)
String N = System.lineSeparator();
String N = "\n";
Exact string as it appeared in my code:
String CT =(""
//:+"---------10--------20--------30-------39
//:+"0123456789012345678901234567890123456789
+"CREATE TABLE t_1( "+N//:0
+"id SERIAL PRIMARY KEY "+N//:30
+" ,c_1 INT "+N//:60
+" ,c_2 VARCHAR (255) "+N//:90
+" ,c_3 CHARACTER( 8 ) "+N//:120
+" ,c_4 BOOLEAN "+N//:150
+" ,c_5 BYTEA "+N//:180
+" ,c_6 DATE "+N//:210
+"); " //:240
);;
Exact Error Message when N==null instead of "\n":
org.postgresql.util.PSQLException:
ERROR: conflicting NULL/NOT NULL declarations
for column "nullid" of table "t_1"
Stack: Java+Heroku+Tomcat9+PostGreSQL 9.5.13
I am trying to create a table in oracle but I am getting this error: unknown command ")" - rest of line ignored. I can't figure out what is causing this error. Below is my SQL for the table:
CREATE TABLE PAYMENT
(PayNum INT NOT NULL PRIMARY KEY,
CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PDate DATE NOT NULL,
Amt DECIMAL(11,2) NOT NULL,
CONSTRAINT fk_BANKACC_PAYMENT FOREIGN KEY (BankName, AccNum)
REFERENCES BANKACC(BankName, AccNum),
CONSTRAINT fk_CRCARD_PAYMENT FOREIGN KEY (CType, CCNum)
REFERENCES CRCARD(CType, CCNum)
);
Your code is correct. Make sure you are referencing primary keys (all 4).
Check this: http://sqlfiddle.com/#!2/7be70/1/0
If you do not follow that, you may get this error: There are no primary or candidate keys in the referenced table 'BANKACC' that match the referencing column list in the foreign key 'fk_BANKACC_PAYMENT'.
Code in the fiddle above:
CREATE TABLE BANKACC
(BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PRIMARY KEY(BankName, AccNum));
CREATE TABLE CRCARD
(CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
PRIMARY KEY(CType, CCNum));
CREATE TABLE PAYMENT
(PayNum INT NOT NULL PRIMARY KEY,
CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PDate DATE NOT NULL,
Amt DECIMAL(11,2) NOT NULL,
CONSTRAINT fk_BANKACC_PAYMENT FOREIGN KEY (BankName, AccNum)
REFERENCES BANKACC(BankName, AccNum),
CONSTRAINT fk_CRCARD_PAYMENT FOREIGN KEY (CType, CCNum)
REFERENCES CRCARD(CType, CCNum)
);
Also you should read this for better understanding on how to implement foreign key constraint: http://docs.oracle.com/cd/E17952_01/refman-5.5-en/create-table-foreign-keys.html
If you're running this in SQL*Plus, get rid of the blank line between REFERENCES and ):
REFERENCES CRCARD(CType, CCNum)
);
Or set sqlblanklines on to change the behaviour.
By default it interprets a blank line as the end of the statement, but doesn't run it. So your entire CREATE TABLE command is essentially ignored, and the ); is treated as a stanalone command. Hence the error message you get, since it doesn't mean anything on its own.
Please use NUMBER for numeric columns.
http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT313
Also, in Oracle, use VARCHAR2 for strings.
but of it your syntax should be correct.