Creating Table in Oracle, SQL - sql

ORA-00907: missing right parenthesis
I can't figure out what is going wrong with my dataypes and/or commas. Any help would be awesome. Thank you!
CREATE TABLE RIDERS
(RIDER_ID VARCHAR2(4) CONSTRAINT RIDERS_RIDER_ID_PK PRIMARY KEY,
FIRSTRIDER VARCHAR2(20) CONSTRAINT RIDERS_FIRSTRIDER_NN NOT NULL,
LASTRIDER VARCHAR2(20) CONSTRAINT RIDERS_LASTRIDER_NN NOT NULL,
ADDRESS VARCHAR2(25),
CITY VARCHAR2(15),
STATE CHAR(2),
ZIP VARCHAR2(15),
PHONE VARCHAR2(25)
DATEJOINED DATE,
AGENT_ID VARCHAR2(4) CONSTRAINT RIDERS_AGENT_ID_FK REFERENCES AGENTS(AGENT_ID),
TEAM_ID VARCHAR2(4) CONSTRAINT RIDERS_TEAM_ID_FK REFERENCES TEAMS(TEAM_ID));

PHONE VARCHAR2(25)
There's your missing comma.

You miss a comma after PHONE:
CREATE TABLE RIDERS
(RIDER_ID VARCHAR2(4) CONSTRAINT RIDERS_RIDER_ID_PK PRIMARY KEY,
FIRSTRIDER VARCHAR2(20) CONSTRAINT RIDERS_FIRSTRIDER_NN NOT NULL,
LASTRIDER VARCHAR2(20) CONSTRAINT RIDERS_LASTRIDER_NN NOT NULL,
ADDRESS VARCHAR2(25),
CITY VARCHAR2(15),
STATE CHAR(2),
ZIP VARCHAR2(15),
PHONE VARCHAR2(25),
DATEJOINED DATE,
AGENT_ID VARCHAR2(4) CONSTRAINT RIDERS_AGENT_ID_FK REFERENCES AGENTS(AGENT_ID),
TEAM_ID VARCHAR2(4) CONSTRAINT RIDERS_TEAM_ID_FK REFERENCES TEAMS(TEAM_ID));

Related

I'm getting this error message: `ORA-00933: SQL command not properly ended` and was hoping someone could point out what's causing it

Here's the code that's causing the issue:
DROP TABLE customers;
--1 create tables
CREATE TABLE customers (
customer_id NUMBER(10),
last_name VARCHAR2(25),
first_name VARCHAR2(25),
home_phone VARCHAR2(12),
address VARCHAR2(100),
city VARCHAR2(30),
state VARCHAR2(2),
email VARCHAR2(25),
cell_phone VARCHAR2(12),
CONSTRAINT pk_customer_customer_id PRIMARY KEY (customer_id),
CONSTRAINT not_null_customer_last_name NOT NULL (last_name),
CONSTRAINT not_null_customer_first_name NOT NULL (first_name),
CONSTRAINT not_null_customer_home_phone NOT NULL (home_phone),
CONSTRAINT not_null_customer_address NOT NULL (address),
CONSTRAINT not_null_customer_city NOT NULL (city),
CONSTRAINT not_null_customer_state NOT NULL (state)
);
I've tried formatting the constraints at the column level and that didn't seem to help. I'm an absolute beginner and am doing this for a class so I'm sure it's something simple and silly but I couldn't figure it out for the life of me. After a few hours of frustration I figured I'd see if there was someone out there who could point me in the right direction.
I am using Oracle APEX.
NOT NULL constraints are defined in-line; there is no option to define them out-of-line (apart from using a CHECK constraint).
From the CREATE TABLE documentation:
inline_constraint::=
out_of_line_constraint::=
Therefore, if you want to use a NOT NULL constraint then you need to declare it inline:
CREATE TABLE customers (
customer_id NUMBER(10),
last_name VARCHAR2(25)
CONSTRAINT not_null_customer_last_name NOT NULL,
first_name VARCHAR2(25)
CONSTRAINT not_null_customer_first_name NOT NULL,
home_phone VARCHAR2(12)
CONSTRAINT not_null_customer_home_phone NOT NULL,
address VARCHAR2(100)
CONSTRAINT not_null_customer_address NOT NULL,
city VARCHAR2(30)
CONSTRAINT not_null_customer_city NOT NULL,
state VARCHAR2(2)
CONSTRAINT not_null_customer_state NOT NULL,
email VARCHAR2(25),
cell_phone VARCHAR2(12),
CONSTRAINT pk_customer_customer_id PRIMARY KEY (customer_id)
);
I think the problem is the way you are defining the NOT NULL constraints, they can be CHECK constraints so you should use:
CREATE TABLE customers (
customer_id NUMBER(10),
last_name VARCHAR2(25),
first_name VARCHAR2(25),
home_phone VARCHAR2(12),
address VARCHAR2(100),
city VARCHAR2(30),
state VARCHAR2(2),
email VARCHAR2(25),
cell_phone VARCHAR2(12),
CONSTRAINT pk_customer_customer_id PRIMARY KEY (customer_id),
CONSTRAINT not_null_customer_last_name CHECK (last_name IS NOT NULL),
CONSTRAINT not_null_customer_first_name CHECK (first_name IS NOT NULL),
CONSTRAINT not_null_customer_home_phone CHECK (home_phone IS NOT NULL),
CONSTRAINT not_null_customer_address CHECK (address IS NOT NULL),
CONSTRAINT not_null_customer_city CHECK (city IS NOT NULL),
CONSTRAINT not_null_customer_state CHECK (state IS NOT NULL)
);

Trying to do an assignment but stuck at ORA-00906: missing left parenthesis

Im doing a course on DB, this is one of the first assignments, I'm about to be done, but there is an issue somewhere in my REL_CURSOS_ALUMNOS query which ends up with the ORA-00906 error.
REL_CURSOS_ALUMNOS is a relationship table for "one to many" 1 ' CURSOS(Id_Curso) to many ALUMNOS(NIF_Alumno).
This is my query till now:
CREATE TABLE PROFESORES(
NIF_Profesor VARCHAR2(15) CONSTRAINT Prof_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Titulacion VARCHAR2(500),
Salario FLOAT(10) CONSTRAINT Prof_sal_NN NOT NULL
);
CREATE TABLE ALUMNOS(
NIF_Alumno VARCHAR2(15) CONSTRAINT Alum_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Sexo CHAR(1),
Fecha_Nacimiento DATE
);
CREATE TABLE CURSOS (
Id_Curso VARCHAR2(15) CONSTRAINT Curs_Id_PK PRIMARY KEY,
Nombre VARCHAR2(400) UNIQUE,
NIF_Profesor VARCHAR2(15) REFERENCES PROFESORES (NIF_Profesor),
Max_Alumnos NUMBER(5),
Inicio_Fecha DATE,
Final_Fecha DATE,
Num_Horas NUMBER(10) NOT NULL,
CONSTRAINT Curs_FechasIncorrectas CHECK (Final_Fecha > Inicio_Fecha));
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Cursos),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE KEY (Id_Curso, NIF_Alumno));
Thank you.
You have a couple of small errors in the final table definition:
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Curso),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE (Id_Curso, NIF_Alumno)
);
Your specific problem is caused by KEY. That is not needed for a unique constraint definition. Further, the reference for CURSOS is id_curso, not id_cursos.
I left both unique definitions. But if NIF_Alumno is unique, then the pair (Id_Curso, NIF_Alumno) is also unique, so that constraint is redundant.

Oracle shows invalid character on constraint check

here is my code
Create table Member(
Member_Id Number(5) primary key,
Member_Name varchar2(30),
Member_address varchar2(50),
Acc_Open_Date Date,
Membership_type varchar2(20),
Fees_paid Number(4),
Max_Book_Allowed Number(2),
Penalty_Amount Number(7,2),
CONSTRAINT mt CHECK (Membership_type IN(‘Lifetime’,’Annual’,’Half Yearly’,’Quarterly’))
);
now i want to set the check constraint with some specific string and i use for comparing the IN but it shows again and again invalid character.
now by using alter table it works..
but my query is how can i use in create table statement.
Too long for a comment.
This works for me.
Create table Member(
Member_Id Number(5) primary key,
Member_Name varchar2(30),
Member_address varchar2(50),
Acc_Open_Date Date,
Membership_type varchar2(20),
Fees_paid Number(4),
Max_Book_Allowed Number(2),
Penalty_Amount Number(7,2),
CONSTRAINT mt CHECK (Membership_type IN('Lifetime','Annual','Half Yearly','Quarterly'))
)

I am trying to create a table but its not getting created. The issue is with constraints. Mostly NOT NULL and FOREIGN KEY

CREATE TABLE Customer(
Cust_Id Number(4),
Cust_First_Name VarChar2(20),
Cust_Mid_Name VarChar2(20),
Cust_Last_Name VarChar2(20),
Account_Type Varchar2(15),
Cust_Email VarChar2(30),
Cust_Mobile Number(10),
Bank_Id NUMBER(4),
Account_No Number(4),
CONSTRAINT Cust_Id_Pri_Key PRIMARY KEY (Cust_Id),
CONSTRAINT Cust_Last_Name_Not_Null NOT NULL (Cust_Last_Name),
CONSTRAINT Cust_First_Name_Not_Null NOT NULL (Cust_First_Name),
CONSTRAINT Cust_Email_Unq UNIQUE(Cust_Email),
CONSTRAINT Cust_Mobile_Unq UNIQUE(Cust_Mobile),
CONSTRAINT Bank_Id_For_Key FOREIGN KEY REFERENCES bank(bankId)
);
Working SQL Fiddle: http://sqlfiddle.com/#!4/05311
Here's how your SQL should look like.
CREATE TABLE Customer(
Cust_Id Number(4) NOT NULL,
Cust_First_Name VarChar2(20) NOT NULL,
Cust_Mid_Name VarChar2(20),
Cust_Last_Name VarChar2(20) NOT NULL,
Account_Type Varchar2(15),
Cust_Email VarChar2(30),
Cust_Mobile Number(10),
Bank_Id NUMBER(4),
Account_No Number(4),
CONSTRAINT Cust_Id_Pri_Key PRIMARY KEY (Cust_Id),
CONSTRAINT Cust_Email_Unq UNIQUE(Cust_Email),
CONSTRAINT Cust_Mobile_Unq UNIQUE(Cust_Mobile),
CONSTRAINT Bank_Id_For_Key FOREIGN KEY (Bank_Id) REFERENCES bank(bankId)
);
Almost all your errors are from the Syntax.
NOT NULL is specified in the Column name itself, not as a separate constraint.
FOREIGN KEY CONSTRAINT requires you to specify the source column name, and the FOREIGN KEYcolumn in the table that it is defined as aPRIMARY KEY`
You have two problems: you can't specify NOT NULL as an out-of-line constraint and you are missing the column name in the foreign key constraint. The following should work:
CREATE TABLE customer
(
cust_id NUMBER (4),
cust_first_name VARCHAR2 (20) CONSTRAINT cust_first_name_not_null NOT NULL,
cust_mid_name VARCHAR2 (20),
cust_last_name VARCHAR2 (20) CONSTRAINT cust_last_name_not_null NOT NULL,
account_type VARCHAR2 (15),
cust_email VARCHAR2 (30),
cust_mobile NUMBER (10),
bank_id NUMBER (4),
account_no NUMBER (4),
CONSTRAINT cust_id_pri_key PRIMARY KEY (cust_id),
CONSTRAINT cust_email_unq UNIQUE (cust_email),
CONSTRAINT cust_mobile_unq UNIQUE (cust_mobile),
CONSTRAINT bank_id_for_key FOREIGN KEY (bank_id) REFERENCES bank (bankid)
);
However, I wouldn't bother with naming the NOT NULL constraints. Naming them doesn't really add very much manageability, as they can be updated by altering the table without knowing the name.

ORA-02270 error while creating a table

This is my create table command for creating table employee but I can't figure out why I get this error even though I have declared ssn as primary key
CREATE TABLE EMPLOYEE(
F_NAME VARCHAR2(15) NOT NULL,
M_NAME CHAR(2),
L_NAME VARCHAR2(15) NOT NULL,
SSN CHAR(9) PRIMARY KEY,
BIRTHDAY DATE,
ADDRESS VARCHAR2(50),
SEX CHAR(1) CHECK(SEX IN('M','F','m','f')),
SALARY NUMBER(7) DEFAULT 800,
SSSN CHAR(9),
DEPARTMENT_NUMBER NUMBER(5),
CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSSN)
REFERENCES EMPLOYEE(SSSN) ON DELETE SET NULL,
CONSTRAINT EMP_DEPTNO_FK FOREIGN KEY(DEPARTMENT_NUMBER)
REFERENCES DEPARTMENT(DEPT_NO) ON DELETE CASCADE);
but I am getting error:
ORA-02270: no matching unique or primary key for this column-list
Change CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSSN) to CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSN) like this:
CREATE TABLE EMPLOYEE(
F_NAME VARCHAR2(15) NOT NULL,
M_NAME CHAR(2),
L_NAME VARCHAR2(15) NOT NULL,
SSN CHAR(9) PRIMARY KEY,
BIRTHDAY DATE,
ADDRESS VARCHAR2(50),
SEX CHAR(1) CHECK(SEX IN('M','F','m','f')),
SALARY NUMBER(7) DEFAULT 800,
SSSN CHAR(9),
DEPARTMENT_NUMBER NUMBER(5),
CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSSN) REFERENCES EMPLOYEE(SSN) ON DELETE SET NULL,
CONSTRAINT EMP_DEPTNO_FK FOREIGN KEY(DEPARTMENT_NUMBER) REFERENCES DEPARTMENT(DEPT_NO) ON DELETE CASCADE);
A foreign key can be declared in a table if and only if it is a primary key in another table.
What you need to do immediately is ensure that SSN and DEPARTMENT_NUMBER are Primary key in their respective tables.
Visit this link and you will easily find your error.
http://www.techonthenet.com/oracle/errors/ora02270.php
In case you do not follow, learn from
http://www.w3schools.com/sql/sql_foreignkey.asp
Hope it helps