Error is missing right parenthesis [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
create table empl
(
empid number primary key,
empname varchar(25),
email varchar(25) not null unique,
doj date not null,
sal number not null check (sal > 0),
deptid number FOREIGN KEY REFERENCES dept(deptid)
);

Remove the FOREIGN KEY:
create table empl (
empid number primary key,
empname varchar(25),
email varchar(25) not null unique,
doj date not null,
sal number not null check (sal>0),
deptid number REFERENCES dept(deptid)
);
SQL Fiddle is here.
When defining a column, the foreign key relationship is established by the keyword references. You use foreign key when you want to introduce it as a constraint after the columns are defined. So you could also write:
create table empl (
empid number primary key,
empname varchar(25),
email varchar(25) not null unique,
doj date not null,
sal number not null check (sal>0),
deptid number,
foreign key (deptid) REFERENCES dept(deptid)
);
By the way, when using Oracle, one usually uses varchar2() instead of varchar().

Related

Error report - ORA-01401: inserted value too large for column. First time making a table in SQL thanks in advance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
CREATE TABLE DRIVER
(
Driver_Licence VARCHAR2(8) NOT NULL,
SSN VARCHAR2(7),
First_Name VARCHAR2(7) NOT NULL,
Last_Name VARCHAR2(7),
Birth_Date DATE DEFAULT '01/01/1900',
Hire_Date DATE,
State_Name CHAR(2),
CONSTRAINT Driver_PK PRIMARY KEY (Driver_Licence),
CONSTRAINT Driver_UC UNIQUE (SSN),
CONSTRAINT Driver_CHK CHECK (Hire_Date > Birth_Date)
);
/*
Hire Date corresponds to the date the employee was first hired
*/
Trying to create a table in SQL but keep getting the error in the title
ORA-01401: inserted value too large for column
I think you misplaced error
ORA-01401: inserted value too large for column for creating table, that error is for inserting statement.
For creating the table, you need to convert to date for default value of column "Birth_Date". I try to run this script on my env and running well.
CREATE TABLE DRIVER
(
Driver_Licence VARCHAR2(8) NOT NULL,
SSN VARCHAR2(7),
First_Name VARCHAR2(7) NOT NULL,
Last_Name VARCHAR2(7),
Birth_Date DATE DEFAULT TO_DATE('01/01/1900', 'DD/MM/YYYY'),
Hire_Date DATE,
State_Name CHAR(2),
CONSTRAINT Driver_PK PRIMARY KEY (Driver_Licence),
CONSTRAINT Driver_UC UNIQUE (SSN),
CONSTRAINT Driver_CHK CHECK (Hire_Date > Birth_Date)
);
Hope this can help you.

Add a foreign key when creating a new table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to create a new table (Customer) and I'm trying to add the foreign key ZipCode, but its definitely not working I tried looking for possible solutions but I couldn't make it work with what I got. Any help is appreciated, thanks!
CREATE TABLE Zip_Code (
ZipCode varchar(10) Not null,
City varchar(20),
StateCode varchar(2)
);
ALTER TABLE Zip_Code
ADD Primary Key (ZipCode);
CREATE TABLE Customer (
CustomerID INT Primary Key,
CustomerName varchar(30),
Address varchar(30),
FOREIGN KEY (ZipCode) REFERENCES Zip_Code(ZipCode),
CreditLimit money,
Balance money,
);
The problem is not the primary key definition. The problem is customer. In order to declare a column as a foreign key, you have to define the column first:
CREATE TABLE Customer (
CustomerID INT Primary Key,
CustomerName varchar(30),
Address varchar(30),
ZipCode varchar(10),
FOREIGN KEY (ZipCode) REFERENCES Zip_Code(ZipCode),
CreditLimit money,
Balance money,
);
Here is a db<>fiddle.
MY SQL:
CREATE TABLE Customer (
CustomerID int NOT NULL,
ZipCode int,
CustomerName varchar(30),
Address varchar(30),
PRIMARY KEY (CustomerID),
FOREIGN KEY (ZipCode) REFERENCES Zip_Code(ZipCode)
);
SQL Server / Oracle:
CREATE TABLE Customer (
CustomerID int NOT NULL PRIMARY KEY,
CustomerName varchar(30),
Address varchar(30),
ZipCode int FOREIGN KEY REFERENCES Zip_Code(ZipCode)
);

update field with special conditions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to update the password for username (of type Admin) with next condition: first 3 letters of his name, 2 last letters of the last name and 4 last digits of his phone
create table company
(
CODE_COMPANY char(30),
NAME_COMPANY varchar2(30) not null,
MAIL_COMPANY varchar2(30) null,
constraint PK_CODE_COMPANY primary key (CODE_COMPANY),
);
create table USERNAME
(
NAME_USERNAME varchar2(30),
USER_LOCATION number,
fNAME varchar2 (30) not null,
lNAME varchar2 (30) not null,
PHONE_USER char(13) null,
PASSWORD varchar2(30) not null,
USER_POSITION varchar2 (30),
check (USER_POSITION in('Admin', 'Superadmin', 'Technician', 'Student')),
constraint PK_NAME_USERNAME primary key (NAME_USERNAME),
constraint FK_USER_LOCATION foreign key (USER_LOCATION) references uLOCATION (LOCATION)
);
create table uLOCATION
(
LOCATION number,
CODE_COMPANY char(30),
NAME_LOCATION varchar2(30) not null,
FLOOR_LOCATION varchar2(10),
check (FLOOR_LOCATION in ('MAIN_FLOOR', '1ST FLOOR', '2ND FLOOR', '3RD FLOOR')),
constraint PK_LOCATION primary key (LOCATION),
constraint FK_CODE_COMPANY_L foreign key (CODE_COMPANY) references company (CODE_COMPANY),
);
Oracle concat function accepts only two parameters, so there must be two nested concats to join 3 strings together.
update username set password =
concat(
concat(
substr(fname,1,3),
substr(lname, length(lname) - 1)
),
substr(phone_user, length(phone_user) - 3)
)
where user_position = 'Admin';
These links explain how substr() works with length()
https://www.techonthenet.com/oracle/functions/substr.php
https://www.w3resource.com/oracle/character-functions/oracle-length-function.php

#1215 SQL Cannot add foreign key constraint error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here is my code. I can't figure out why I can't add the constraint.
Create table customer(
UserId Integer
)
CREATE TABLE Date(
DateID INTEGER,
User1ID INTEGER NOT NULL,
User2ID INTEGER NOT NULL,
Date CHAR(20) NOT NULL,
GeoLocation CHAR(20) NOT NULL,
BookingFee INTEGER NOT NULL,
CustomerRepresentative CHAR(20) NOT NULL,
Comments CHAR(200),
User1Ratings CHAR(20),
User2Ratings CHAR(20),
Primary Key (DateID),
Check ( User1Ratings IN (‘Excellent’, ‘VeryGood’, ‘Good’, ‘Fair’, ‘Poor’) ),
Check ( User2Ratings IN (‘Excellent’, ‘VeryGood’, ‘Good’, ‘Fair’, ‘Poor’) ),
FOREIGN KEY (User1ID) REFERENCES customer(UserID)
)
The most likely explanation for the behavior is that UserID column is not defined as the PRIMARY KEY or a UNIQUE KEY in the customer table
One fix for this would be to re-write the create for the customer table
For SQL Server:
CREATE TABLE customer
( UserId Integer NOT NULL
, CONSTRAINT PK_customer PRIMARY KEY CLUSTERED (UserId)
);
For MySQL:
CREATE TABLE customer
( UserId INTEGER NOT NULL PRIMARY KEY
);

Sql schema error on create table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
it generates an error,i cant understand whats wrong with the script..every suggestion would be helpful thnx
CREATE TABLE ar_abonent
(
ar_nr_klienti_primary_key int,
emri varchar(25),
mbiemri varchar(25)
);
create table ar_celular
(
NR_CEL INT (12),
ar_nr_klienti FOREIGN Key REFERENCES ar_abonent (ar_nr_klienti),int,
identifikues boolean,
data_aktivizimit date,
marka varchar(25)
constraint chk_celular check (NR_CEL IN ('35566%','35567%','35568%','35569%' AND IDENTIFIKUES='TRUE')
);
CREATE TABLE ar_abonent
(
ar_nr_klienti int NOT NULL primary key , --<-- This column needs to be a primary key
emri varchar(25),
mbiemri varchar(25)
);
create table ar_celular
(
NR_CEL INT ,
ar_nr_klienti int FOREIGN Key REFERENCES ar_abonent (ar_nr_klienti) ,
identifikues bit,
data_aktivizimit date,
marka varchar(25),
constraint chk_celular check (NR_CEL IN ('35566%','35567%','35568%','35569%') AND IDENTIFIKUES= 1)
);
I tried correcting it using SQL Fiddle (set to Oracle 11G R2) as I don't own any Oracle servers. This is how it should look to run without issues:
CREATE TABLE ar_abonent
(
ar_nr_klienti int NOT NULL PRIMARY KEY ,
emri VARCHAR2(25),
mbiemri VARCHAR2(25)
);
CREATE TABLE ar_celular
(
NR_CEL int,
ar_nr_klienti int,
identifikues number(1),
data_aktivizimit date,
marka VARCHAR2(25),
CONSTRAINT fk_column FOREIGN KEY (ar_nr_klienti) REFERENCES ar_abonent (ar_nr_klienti),
CONSTRAINT chk_celular CHECK (NR_CEL IN ('35566%','35567%','35568%','35569%') AND IDENTIFIKUES= 1)
);
Sample SQL Fiddle
Turns out Oracle doesn't have a BOOLEAN type (at table level, although it does exist in PL/SQL), and VARCHAR2 might be preferred to VARCHAR (unless nulls are a concern).