Oracle shows invalid character on constraint check - sql

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'))
)

Related

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.

Creating Table in Oracle, 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));

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.

Missing Left Parenthesis - I see no error

I've been trying to figure this out for a while, but I haven't come to a solution, could you please help me? I have four tables like this, I think the problem may be the foreign keys?
I am using SQL+ (Oracle) and SQL Developer.
This is my code:
create table doctor(
doctor_id int constraint nn_name not null,
fname varchar2(30),
lname varchar2(30),
address varchar2(30),
phone_no number(30),
email varchar2(30),
experience_years number(10),
grade number(10),
specialities varchar(30),
availabilty_date default sysdate,
agency_id int,
foreign key references agency (agency_id);
As the error message says: you need one more ).
The create table ( does not get closed at the end. There were also some other bugs I fixed:
create table doctor
( doctor_id int constraint nn_name not null,
fname varchar2(30),
lname varchar2(30),
address varchar2(30),
phone_no number(30),
email varchar2(30),
experience_years number(10),
grade number(10),
specialities varchar(30),
availabilty_date date default sysdate, -- missing data type
agency_id int,
constraint agency_fk foreign key (agency_id) references agent (agency_id) -- invalid foreign constraint
) -- this one

Oracle table error: table creating with constraints

create table prmr(
emp_id number(10) primary key,
name varchar2(10),
mob number(10) unique key,
id varchar2(10),
email varchar2(20)
);
When I create this table, it gives me the error "Missing Parenthesis". Any help?
Your syntax is wrong. Remove key from unique key
CREATE TABLE prmr(
emp_id number(10) PRIMARY KEY,
name varchar2(10),
mob number(10) UNIQUE,
id varchar2(10),
email varchar2(20)
);
There is also a way to specify constraints separately:
CREATE TABLE prmr(
emp_id number(10) NOT NULL,
name varchar2(10),
mob number(10),
id varchar2(10),
email varchar2(20),
CONSTRAINT mob_unique UNIQUE (mob),
CONSTRAINT emp_id_pk PRIMARY KEY (emp_id)
);
I also added NOT NULL to the emp_id (Although you don't need to specify it explicitly as far as emp_id is PK, that could, probably, help avoid the confusion anyway).
Try this :
create table prmr(
emp_id number(10),
name varchar2(10),
mob number(10),
id varchar2(10),
email varchar2(20),
CONSTRAINT PK_table PRIMARY KEY (emp_id),
CONSTRAINT unique_mob UNIQUE(mob)
);