SQL: Missing Parenthesis - sql

I have tried editing the command and looking at other questions in order to answer this question, but I still get the dreaded
ORA-00907: missing right parenthesis.
Here is my code. Is there any key word that I may need to drop here? Thanks
CREATE TABLE Loan
(
LoanID INT IDENTITY ( 1, 1 ) UNIQUE,
BranchID INT NOT NULL REFERENCES Branch(BranchID) ON DELETE CASCADE,
LoanNumber CHAR(20) NOT NULL UNIQUE,
LoanType VARCHAR(30) NOT NULL,
Amount MONEY NOT NULL,
ModifiedDate DATETIME DEFAULT (getdate()),
PRIMARY KEY ( LoanID )
);

The following ddl is syntactically correct. Of course, you have to check whether that statement really produces what you want ( in particular the IDENTITY keyword in your original statement is not accounted for ):
CREATE TABLE Loan
(
LoanID INTEGER NOT NULL PRIMARY KEY
, BranchID INTEGER NOT NULL CONSTRAINT tl_fk_branchid REFERENCES Branch(BranchID) ON DELETE CASCADE
, LoanNumber CHAR(20) NOT NULL CONSTRAINT tl_u_loannumber UNIQUE
-- right padded to length of 20 with blanks
, LoanType VARCHAR2(30) NOT NULL
, Amount Number(*,4) NOT NULL
-- cf. http://stackoverflow.com/a/29014422, changed per #BobJarvis' comment
, ModifiedDate DATE DEFAULT SYSDATE
);
The syntax deviations are as follows:
IDENTITYkeyword
inline constraint specification
datatype to represent date and time
datatype for string content
stand-in for money datatype
current datetime

Related

How implement the function of `case` in DDL?

I have a table order. Its DDL is listed below
create table "order"
(
id serial primary key,
quantity integer not null,
estimated_delivery_date date not null,
lodgement_date date check ( lodgement_date > '2022-03-02' ),
product_model varchar(128) not null,
sales_id integer not null,
contract_number varchar(10) not null
);
I want to make the lodgement_date attribute to be null if it is later than 2022-03-02 when inserting a new row. How can I achieve this function in DDL.
To do that, you can't do it in the creation of the table. It is gonna be a constraint.
Example:
CONSTRAINT check_lodgement_date
CHECK (lodgement_date > '2022-03-02')

Creating a table syntax errors in sql

I'm trying to create a SQL table for a school assignment. I'm required to name the constraints, and I am receiving a syntax error when creating my table
SELECT * FROM demo;
CREATE TABLE PRODUCT
(
ProductID NUMBER,
ProductDescription VARCHAR(200),
CONSTRAINT ProductPK PRIMARY KEY (ProductID)
);
CREATE TABLE ITEM
(
ItemNum NUMBER
CONSTRAINT Inull NOT NULL
CONSTRAINT ItemPK PRIMARY KEY(ItemNum),
ItemDesc VARCHAR(200)
);
The syntax error is as follows:
near "(": syntax error
It is likely something simple, but this is my first time writing SQL.
You have a few errors .. One, you are missing commas after ItemNum NUMBER and CONSTRAINT Inull NOT NULL
Additionally there are other ways to define NOT NULL, you technically don't need to use CONSTRAINT in traditional SQL.
And depending on the flavor of SQL you are using .. I like to use INT or INTEGER instead of NUMBER -- It's basically the same as NUMBER() with a few exceptions, but if you are using whole numbers I would use INT or INTEGER
SELECT * FROM demo;
create TABLE PRODUCT
(
ProductID INT,
ProductDescription VARCHAR(200),
CONSTRAINT ProductPK PRIMARY KEY (ProductID)
);
CREATE TABLE ITEM
(
ItemNum INT NOT NULL,
CONSTRAINT ItemPK PRIMARY KEY(ItemNum),
ItemDesc VARCHAR(200)
);
UPDATE
And as #Schwern suggests .. Your query can be further simplified, as there really is no need for CONSTRAINTS here .. I would also recommend Auto Incrementing your IDs .. Thus removing the need for NOT NULL on the ID. This also negates the need to supply an ID with the INSERT query ..
SELECT * FROM demo;
create TABLE PRODUCT
(
ProductID INT PRIMARY KEY AUTO_INCREMENT,
ProductDescription VARCHAR(200)
);
CREATE TABLE ITEM
(
ItemNum INT PRIMARY KEY AUTO_INCREMENT,
ItemDesc VARCHAR(200)
);

ORA-00907: "missing right parenthesis"

First of all, I apologize for asking the same question again but the information on the old topics did not work in my oraclesql code and secondly there may be some syntax errors, I learned mysql in school last semester, but now we are learning oraclesql with old mysql information in the lesson, so i am a bit confused.
So my question is i want to create 3 related table (movie-casts-actor) but i am getting missing right parenthesis. trying to connect casts table to movie table with foreign key and actor table to casts table. what am i doing wrong? how spagetti is it?
CREATE TABLE Movie (
movie_id NUMBER(7) NOT NULL,
movie_name varchar2(50) NOT NULL ,
movie_director varchar2(50) NOT NULL ,
movie_year INT(4) NOT NULL ,
movie_duration INT(3) ,
movie_language varchar2(15) ,
movie_rating number(4,2) ,
PRIMARY KEY(movie_id),
CONSTRAINT is_unique UNIQUE(movie_id),
CONSTRAINT movie_id_checker CHECK(movie_id>0 and movie_id<=9999999)
);
CREATE TABLE Casts (
movie_id_fk INT(7) FOREIGN KEY REFERENCES Movie(movie_id) ,
cast_id INT(7) NOT NULL,
actor_fullname varchar2(50) NOT NULL ,
actor_role varchar2(50) ,
PRIMARY KEY(cast_id),
CONSTRAINT is_unique UNIQUE(cast_id),
CONSTRAINT cast_id_checker CHECK(cast_id>0 and cast_id<=9999999)
);
CREATE TABLE Actor (
cast_id_fk INT(7) FOREIGN KEY REFERENCES Casts(cast_id) ,
actor_id INT(7) NOT NULL,
actor_name CHAR(30) ,
actor_surname CHAR(25) ,
actor_gender CHAR(5) ,
actor_age CHAR(3) CONSTRAINT real_age_check CHECK(actor_age>0 AND actor_age<=150),
PRIMARY KEY(actor_id),
CONSTRAINT is_unique UNIQUE(actor_id),
CONSTRAINT actor_id_checker CHECK(actor_id>0 and actor_id<=9999999)
)
The errors are:
INT or NUMBER(4,0) and not INT(4)
You cannot have a unique key and a primary key on the same column.
An inline foreign key just need the REFERENCES keyword and does not need FOREIGN KEY.
Other issues:
You probably don't want actor name/surname as fixed-length CHAR strings and want to us variable-length VARCHAR2.
You probably want gender to be a code from a fixed list (which can be as long or as short as you find appropriate to describe the actors) rather than as a string.
You probably don't want to have an AGE column as that will be out of date as soon as the first birthday of an actor occurs; instead have a DATE_OF_BIRTH column that is a DATE data type and then you can calculate the age as and when necessary.
Using the table name as a prefix for every column is a waste of key strokes; you would be better to just name the columns for what they are without the prefix. Similar with fk as a suffix.
If you are using a NUMBER(7,0) for id values then you don't need to check that it is less than or equal to 9999999 as it is impossible to be a greater value; however, you can have zero or negative values so the check constraint for the lower bound may still be valid.
If you are using Oracle 12c or later then you should probably be using an IDENTITY column for the id values unless you are taking the id values from a 3rd party.
CREATE TABLE Movie (
id NUMBER(7,0) NOT NULL,
name varchar2(50) NOT NULL ,
director varchar2(50) NOT NULL ,
year NUMBER(4,0) NOT NULL ,
duration NUMBER(3,0),
language varchar2(15) ,
rating number(4,2) ,
PRIMARY KEY(id),
CONSTRAINT movie_id_checker CHECK(id>0)
);
CREATE TABLE Casts (
movie_id INT REFERENCES Movie(id) ,
id NUMBER(7,0) NOT NULL,
fullname varchar2(50) NOT NULL ,
role varchar2(50) ,
PRIMARY KEY(id),
CONSTRAINT cast_id_checker CHECK(id>0)
);
CREATE TABLE Actor (
cast_id NUMBER(7,0) REFERENCES Casts(id) ,
id NUMBER(7,0) NOT NULL,
name VARCHAR2(30) ,
surname VARCHAR2(25) ,
gender CHAR(1)
CHECK ( gender IN ( 'M', 'F', 'X', 'Y', 'Z' ) ),
date_of_birth DATE
CONSTRAINT real_age_check CHECK(date_of_birth >= DATE '1870-01-01' ),
PRIMARY KEY(id),
CONSTRAINT actor_id_checker CHECK(id>0)
);
db<>fiddle here

Create a table for following condition

condition for the problem to be solved:
The code i tried to do is
create table article (
ArCode CHAR(5),
ArName VARCHAR(30) NOT NULL ,
Rate Number(8,2) ,
Quantity NUMBER(4) CHECK (Quantity>0) DEFAULT 0 ,
Class CHAR(1)
);
I couldn't solve the first condition and so i am getting right parenthesis missing for final condition
I would translate your requirement as follows:
CREATE TABLE article (
ArCode CHAR(5) PRIMARY KEY CHECK(ArCode like 'A%'),
ArName VARCHAR(30) NOT NULL,
Rate NUMERIC(8,2),
Quantity NUMERIC(4) DEFAULT 0 CHECK (Quantity >= 0),
Class CHAR(1) CHECK(Class in ('A', 'B', 'C'))
);
Changes to your original code:
you want NUMERIC rather than NUMBER
ArCode must be declared as PRIMARY KEY, and needs a check constraint to enfore the "Must begin with A" requirement
the check constraint on Quantity should allow 0 value (that's the default!)
Class needs a check constraint on the list of allowed values

Get foreign key between two dates

It is necessary to analyze the fluctuations in exchange rates, depending on the sign of the zodiac.
I would like to insert foreign key (zodiak_id) in the follow table:
create table CURRENCY (
CUR_ID number NOT NULL,
CUR_DATE DATE not null,
CUR_NAME varchar2(3) not null,
VALUE NUMBER not null,
Zodiac_id number,
constraint PK_CUR primary key (CUR_ID),
CONSTRAINT FK_ZOD FOREIGN KEY (Zodiac_id) REFERENCES Zodiac(Zodiac_id)
);
of this table:
create table Zodiac (
Zodiac_id number not null,
Zodiac_name VARCHAR2(15) not null,
START_PERIOD date,
END_PERIOD date,
constraint PK_Zod primary key (Zodiac_id)
);
The sqls i wrote is below:
insert into CURRENCY cr(Zodiac_id)
select Zodiac_id from ZODIAC z
where cr.CUR_DATE >= z.START_PERIOD and cr.CUR_DATE <= z.END_PERIOD;
But get this error: SQL Error: ORA-00904: "CR"."CUR_DATE": invalid identifier
Thanks in advance for your help.
Try this;
insert into CURRENCY (Zodiac_id)
select Z.Zodiac_id from Zodiac Z
inner join CURRENCY CR ON CR.Zodiac_id=Z.Zodiac_id
WHERE CR.CUR_DATE BETWEEN Z.START_PERIOD AND Z.END_PERIOD
It's because you're inserting only the zodiac_id into the CURRENCY table, but cur_date is a required field. You've marked it with a "not null" constraint in the create table statement.