Related
I'm creating a DB with 2 tables.
When I run the code in MySQL Workbench it works but when I run it in Oracle's liveSQL (I'm not posting a link because I don't think it is permitted) it doesn't.
The script is:
CREATE TABLE supervisores (
supid INT(6) NOT NULL,
suparea VARCHAR(20) NOT NULL,
supnombre VARCHAR(40) NOT NULL,
supemail VARCHAR(50) NOT NULL,
PRIMARY KEY (supid)
);
CREATE TABLE nomina (
empid INT(6) NOT NULL,
empnombre VARCHAR(30) NOT NULL,
empedad INT(2) NOT NULL,
emparea VARCHAR(20) NOT NULL,
supid INT(6),
PRIMARY KEY (empid),
FOREIGN KEY (supid) REFERENCES supervisores (stupid)
);
INSERT INTO supervisors
VALUES
(100001, 'Ventas', 'Jaime Rodriguez', 'jrodriguz#empresa.com'),
(100002, 'IT', 'Camila Jimenez', 'cjimenez#empresa.com'),
(100003, 'Operaciones', 'Diana Delgado', 'ddelgado#empresa.com'),
(100004, 'RRHH', 'Ana Silgado', 'asilgado#empresa.com'),
(100005, 'IT', 'Pepito perez', 'cjimenez#empresa.com');
INSERT INTO nomina
VALUES
(106218, 'Jairo Caldera', 32, 'Ventas', 100001),
(107773, 'Carlos Saldarriaga', 31, 'IT', 100005),
(102801, 'Javier Zúñiga', 25, 'Operaciones', 100003),
(101845, 'Julian Hurtado', 27, 'RRHH', 100004),
(105358, 'Viviana Solano', 28, 'RRHH', 100004),
(101351, 'Karen Castillo', 35, 'Operaciones', 100003),
(104278, 'Sebastián Paternina', 42, 'IT', 100002),
(105377, 'Marina Álvarez', 19, 'IT', 100002),
(105002, 'Ricardo Colorado', 28, 'Operaciones', 100003),
(100178, 'Andrés Domínguez', 35, 'Operaciones', 100003),
(103710, 'Daniel Olaya', 22, 'Operaciones', 100003),
(101499, 'Crisrina Navarrete', 24, 'Operaciones', 100003);
SELECT empnombre, empedad, empid, emparea, supnombre, supemail
FROM supervisores, nomina
WHERE supervisores.supid = nomina.supid
ORDER BY empnombre;
RESULT IN MySQL Workbench:
MySQL Workbench
RESULT IN Oracle's LiveSQL:
ORA-00907: missing right parenthesis
Any help will be much appreciated.
Thanks in advance!
I have created a table but when i try to insert values I get an error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (classicmodels.complaints, CONSTRAINT complaints_ibfk_1 FOREIGN KEY (employeeHandling) REFERENCES customers (customerNumber))
I'm not entirely sure what this means so an explanation would be very helpful! Thank you!
CREATE TABLE `complaints` (
`complaintNumber` INT UNIQUE AUTO_INCREMENT,
`customerNumber` INT NOT NULL,
`orderNumber` INT,
`employeeHandling` INT NOT NULL,
`startDate` DATE NOT NULL,
`closeDate` DATE,
`complaintType` VARCHAR(250) NOT NULL,
`details` TEXT,
`resolution` TEXT,
`status` VARCHAR(250) NOT NULL,
PRIMARY KEY (`complaintNumber`),
FOREIGN KEY (`employeeHandling`) REFERENCES customers (`customerNumber`),
FOREIGN KEY (`customerNumber`) REFERENCES employees (`employeeNumber`),
FOREIGN KEY (`orderNumber`) REFERENCES orders (`orderNumber`)
);
INSERT INTO complaints
VALUES
(NULL, 173, NULL, 1323, '2005-03-27', NULL, 'Late Shipment', 'Customer claims shipment came in 2 weeks late. Requests discount/partial refund.', NULL, 'New'),
(NULL, 260, 10283, 1323, '2004-09-18', '2004-09-22', 'Damaged Shipment', 'Motorcycle had broken axle. Requests replacement, partial refund.', 'Axle sent and installed. Customer took $1000 refund', 'Closed'),
(NULL, 385, 10108, 1621, '2003-04-22', '2003-04-23', 'Missing Item', 'Customer claims item not included in shipment. Requests shipment.', 'Customer miscounted shipment stock. No action required.', 'Closed'),
(NULL, 471, 10265, 1611, '2004-07-10', '2004-08-10', 'Unclear', 'Customer disatisfied with shipment quality(?), though inventory not damaged. Requests refund.', 'Customer failed to provide specific reasons. Complaint ticket closed after 1 month of idle time.', 'Closed');
I'm new to SQL, there's an error in my syntax that I don't understand. Any help/suggestions?
I created a table called ACCOUNT
CREATE TABLE `ACCOUNT` (
`acc_ID` INT NOT NULL,
`acc_name` VARCHAR(45) NOT NULL,
`acc_manager` VARCHAR(45) NOT NULL,
`acc_balance` VARCHAR(45) NOT NULL,
`acc_transactionLimit` INT NOT NULL,
`acc_creditLimit` INT NOT NULL,
`OUTLET_out_ID` INT NOT NULL,
PRIMARY KEY (`acc_ID`),
INDEX `fk_ACCOUNT_OUTLET1_idx` (`OUTLET_out_ID` ASC),
CONSTRAINT `fk_ACCOUNT_OUTLET1`
FOREIGN KEY (`OUTLET_out_ID`)
REFERENCES `OUTLET` (`out_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
No problems so far. But when i try to insert records into the table i get the following error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key
constraint fails (Database.ACCOUNT, CONSTRAINT fk_ACCOUNT_OUTLET1
FOREIGN KEY (OUTLET_out_ID) REFERENCES OUTLET (out_ID))
I checked back on my OUTLET reference table:
CREATE TABLE `OUTLET` (
`out_ID` INT NOT NULL,
`out_name` VARCHAR(45) NOT NULL,
`out_manager` VARCHAR(45) NOT NULL,
`out_address` VARCHAR(100) NOT NULL,
`out_phone` VARCHAR(15) NOT NULL,
`out_email` VARCHAR(45) NULL,
PRIMARY KEY (`out_ID`))
I didn't find any problems with it.
Yet when trying to insert:
INSERT INTO ACCOUNT(acc_ID, acc_name, acc_manager, acc_balance, acc_transactionLimit, acc_creditLimit) VALUES
( 01, 'White', 'Mr.Good', '352.00', 1000.00, 500.00),
( 02, 'Hopkins', 'Mr.Good', '403.00', 1000.00, 500.00),
( 03, 'Aumbry', 'Mr.Good', '240.00', 1000.00, 500.00),
( 04, 'Kaze', 'Mr.Good', '76.00', 1000.00, 500.00),
( 05, 'Ashley', 'Mr.Bad', '24.00', 1000.00, 500.00),
( 06, 'Zoara', 'Mr.Bad', '503.00', 1000.00, 500.00),
( 07, 'Shawn', 'Mr.Bad', '34.00', 1000.00, 500.00),
( 08, 'Obsie', 'Mr.Bad', '278.00', 1000.00, 500.00),
( 09, 'Ven', 'Mr.Mr.Bad', '345.00', 1000.00, 500.00),
( 10, 'Zhan', 'Mr.Mr.Bad', '491.00', 1000.00, 500.00);
I get the error stated above.
You need to insert data into the Outlet table that the OUTLET_out_ID references, as this is a NON-NULL column but is a foreign key but no key has yet been made, so you need to re-order your inserts accordingly.
You also can't skip this value because it's NON-NULL and your current inserts are failing as you're not giving it a valid Foreign Key reference.
Insert data into the source table of your foreign key constraints First and then the Account data second.
Additionally, the data inserted into the Outlet table needs to correspond to the values referenced in the Accounts table, as dictated by a foreign key.
Personally I think it's easier in these data insert situations to simply tinsert the data without FK constraints and add these conditions after the data has been inserted. Personally.
It would have been nice to see your OUTLET table script; but then there's something more obvious here: the field OUTLET_out_ID is set to NOT NULL and your INSERT script does not have value for that field.
So, either modify that field to OUTLET_out_ID INT NULL or include the values in your INSERT query.
Here is my table and I keep getting this error. Everything is good to go until I execute the code that I have put in bold at the bottom. Im really close to completing this table but ive tried everything to solve this error but it keeps coming up.
Please Help me
Thank You`
UPDATE
I took your guys suggestions and changed the inserted data now im getting an error 1136 which is column doesnt match value count for row 1 which is visit ID so its a datatype problem but ive tried everything and I cant figure it out IM SO CLOSE TO BEING DONE
PLEASE HELP!
Create Schema Visit;
create table roomtableS(
RoomID char (2) not null,
RoomNum char (2) not null,
Charge integer not null,
CONSTRAINT RoomTable_PK Primary Key(RoomID));
Insert into roomtableS values
('01','1A',125.00),
('02','1A',150.00),
('03','1A',100.00),
('04','1A',200.00),
('05','2B',150.00),
('06','2B',125.00),
('07','3C',200.00),
('08','3C',125.00),
('09','3C',100.00);
SELECT * FROM ROOMTABLES;
create table PATIENT(
PatientID char(5) not null,
PatientName Char(25) not null,
PatientEmail Char(30) null,
PatientPhoneNumber Char(10) null,
PatientAddress Char(100) null,
constraint PATIENT_PK Primary key(PatientID));
insert PATIENT values
('P1', 'Bruce Willis', 'bwillis#mail.org', '2022223333', '1111 Cosmic dr'),
('P2', 'Demi Moore', 'moore#email.net', '2021113333', '1112 Cosmic dr'),
('P3', 'Andre Agassi', 'agassi#mail.org', '2023333333', '1113 Cosmic dr'),
('P4', 'Jet Lee', 'jetlee#email.net', '2023334444', '1114 Chinatown ct'),
('P5', 'Jim Carey', 'carey#email.net', '2023335555', '1115 United dr'),
('P6', 'Bruce Lee', 'bruce#gmail.com', '2023336666', '1115 Chinatown ct');
select* From PATIENT;
Create table SERVICETable(
ServiceID Char (5) not null,
ServiceTreatment Char(25) not null,
ServiceCost numeric not null,
constraint SERVICE_PK Primary Key(ServiceID));
insert SERVICETable values
('S1','Sore throat', 10.00),
('S2', 'Fever', 15.00),
('S3', 'Headache', 10.00),
('S4', 'Blood pressusre', 20.00),
('S5', 'Yearly checkup', 30.00),
('S6', 'Common cold', 15.00);
select* from SERVICETable;
Create Table doctortable(
DocID char (5) NOT NULL,
DoctorFirstName char(15) Not NULL,
DoctorLastName char (15) Not Null,
DoctorPhone char (15) Not Null,
CONSTRAINT DoctorTable_PK Primary Key(DocID));
INSERT INTO doctortable values
('D1','Tim','Edward','555-123-4567'),
('D2','Andy','Smith','888-777-6666'),
('D3','John','Smith','222-321-7654');
Select * From doctortable;
Create Table visit(
VisitID char (2) not Null,
PatientID Char (5) not null,
DocID Char (5) not null,
ServiceID Char (5) not Null,
RoomID char (2) not Null,
Visit date not null,
CONSTRAINT VISIT_PK PRIMARY KEY (VisitID));
Alter table Visit
add foreign key (PatientID)
references Patient (PatientID);
Alter table Visit
add foreign key (DocID)
references doctortable (DocID);
Alter table Visit
add foreign key (ServiceID)
references ServiceTable (ServiceID);
Alter table Visit
add foreign key (RoomID)
references roomtableS (RoomID);
Insert into Visit (VisitID,RoomID,ServiceID,PatientID,Visit) values
**('1','P1','D1','S1','05','2014-01-03'),
('2','P2','D2','S2','01','2014-01-10'),
('3','P3','D1','S3','02','2014-01-10'),
('4','P4','D2','S4','07','2014-01-15'),
('5','P1','D3','S2','08','2014-01-10'),
('6','P5','D3','S5','03','2014-02-02'),
('7','P4','D1','S6','06','2014-01-10'),
('8','P3','D2','S5','03','2014-02-03'),
('9','P2','D3','S6','01','2014-02-04'),
('10','P3','D1','S2','06','2014-02-04'),
('11','P5','D2','S4','04','2014-02-05'),
('12','P4','D1','S5','09','2014-02-06');**
Select * from Visit;
Thank You!
Your insert query into Visit table is like this:
Insert Visit Values
('1','P1','D1','S1','2B','2014-01-03'),
Values are expected to be in the order of
(VisitID, RoomID, ServiceID, PatientID, DocID, Visit)
But they seem to be in a different order.
Change the insert statement to include column names.
Insert Visit
(VisitID, PatientID, DocID, ServiceID, RoomID, Visit)
Values
('1','P1','D1','S1','2B','2014-01-03'),
And the values, '2B' being inserted into 'RoomID' column seem to be 'RoomNum' values but not 'RoomID' values. Change it to '05', or '06'. Such change has to be done in all other inputs for the column.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Hi I'm almost done with this table but I ran into a problem I can't solve.
I keep getting error 1136 which is Column doesn't match value count for row 1 which is VisitID so it's a datatype problem but I cant figure it out.
The code I executed is the visit table code which is below:
Create Schema Visit;
create table roomtableS(
RoomID char (2) not null,
RoomNum char (2) not null,
Charge integer not null,
CONSTRAINT RoomTable_PK Primary Key(RoomID));
Insert into roomtableS values
('01','1A',125.00),
('02','1A',150.00),
('03','1A',100.00),
('04','1A',200.00),
('05','2B',150.00),
('06','2B',125.00),
('07','3C',200.00),
('08','3C',125.00),
('09','3C',100.00);
SELECT * FROM ROOMTABLES;
create table PATIENT(
PatientID char(5) not null,
PatientName Char(25) not null,
PatientEmail Char(30) null,
PatientPhoneNumber Char(10) null,
PatientAddress Char(100) null,
constraint PATIENT_PK Primary key(PatientID));
insert PATIENT values
('P1', 'Bruce Willis', 'bwillis#mail.org', '2022223333', '1111 Cosmic dr'),
('P2', 'Demi Moore', 'moore#email.net', '2021113333', '1112 Cosmic dr'),
('P3', 'Andre Agassi', 'agassi#mail.org', '2023333333', '1113 Cosmic dr'),
('P4', 'Jet Lee', 'jetlee#email.net', '2023334444', '1114 Chinatown ct'),
('P5', 'Jim Carey', 'carey#email.net', '2023335555', '1115 United dr'),
('P6', 'Bruce Lee', 'bruce#gmail.com', '2023336666', '1115 Chinatown ct');
select* From PATIENT;
Create table SERVICETable(
ServiceID Char (5) not null,
ServiceTreatment Char(25) not null,
ServiceCost numeric not null,
constraint SERVICE_PK Primary Key(ServiceID));
insert SERVICETable values
('S1','Sore throat', 10.00),
('S2', 'Fever', 15.00),
('S3', 'Headache', 10.00),
('S4', 'Blood pressusre', 20.00),
('S5', 'Yearly checkup', 30.00),
('S6', 'Common cold', 15.00);
select* from SERVICETable;
Create Table doctortable(
DocID char (5) NOT NULL,
DoctorFirstName char(15) Not NULL,
DoctorLastName char (15) Not Null,
DoctorPhone char (15) Not Null,
CONSTRAINT DoctorTable_PK Primary Key(DocID));
INSERT INTO doctortable values
('D1','Tim','Edward','555-123-4567'),
('D2','Andy','Smith','888-777-6666'),
('D3','John','Smith','222-321-7654');
Select * From doctortable;
Create Table visit(
VisitID char (2) not Null,
PatientID Char (5) not null,
DocID Char (5) not null,
ServiceID Char (5) not Null,
RoomID char (2) not Null,
Visit date not null,
CONSTRAINT VISIT_PK PRIMARY KEY (VisitID));
Alter table Visit
add foreign key (PatientID)
references Patient (PatientID);
Alter table Visit
add foreign key (DocID)
references doctortable (DocID);
Alter table Visit
add foreign key (ServiceID)
references ServiceTable (ServiceID);
Alter table Visit
add foreign key (RoomID)
references roomtableS (RoomID);
Insert into Visit (VisitID,RoomID,ServiceID,PatientID,Visit) values
**('1','P1','D1','S1','05','2014-01-03'),
('2','P2','D2','S2','01','2014-01-10'),
('3','P3','D1','S3','02','2014-01-10'),
('4','P4','D2','S4','07','2014-01-15'),
('5','P1','D3','S2','08','2014-01-10'),
('6','P5','D3','S5','03','2014-02-02'),
('7','P4','D1','S6','06','2014-01-10'),
('8','P3','D2','S5','03','2014-02-03'),
('9','P2','D3','S6','01','2014-02-04'),
('10','P3','D1','S2','06','2014-02-04'),
('11','P5','D2','S4','04','2014-02-05'),
('12','P4','D1','S5','09','2014-02-06');**
Select * from Visit;
There are two problems with the last insertion: the field DocId is missing but the values are there; the order between the fields and values don't match.
It should be like:
Insert into Visit (VisitID,PatientID, DocID, ServiceID, RoomID,Visit) values
('1','P1','D1','S1','05','2014-01-03'),
('2','P2','D2','S2','01','2014-01-10'),
('3','P3','D1','S3','02','2014-01-10'),
('4','P4','D2','S4','07','2014-01-15'),
('5','P1','D3','S2','08','2014-01-10'),
('6','P5','D3','S5','03','2014-02-02'),
('7','P4','D1','S6','06','2014-01-10'),
('8','P3','D2','S5','03','2014-02-03'),
('9','P2','D3','S6','01','2014-02-04'),
('10','P3','D1','S2','06','2014-02-04'),
('11','P5','D2','S4','04','2014-02-05'),
('12','P4','D1','S5','09','2014-02-06');
You could find the complete working code at sqlfiddle.