Can't INSERT values into tables - mysql

I have 3 tables in MySQL database:
CREATE TABLE bank(
idBank int(11) NOT NULL PRIMARY KEY auto_increment,
nameBank varchar(50)
);
CREATE TABLE region(
idRegion int(11) NOT NULL PRIMARY KEY auto_increment,
address varchar(50) NOT NULL,
district varchar(30) NOT NULL,
city varchar(50) NOT NULL,
tel varchar(15) NOT NULL
);
CREATE TABLE branch(
idBranch int(11) NOT NULL PRIMARY KEY auto_increment,
idBank int(11) NOT NULL,
idRegion int(11) NOT NULL,
quantity int(50) NULL,
president varchar(60) NULL,
FOREIGN KEY (idBank) REFERENCES bank (idBank),
FOREIGN KEY (idRegion) REFERENCES region (idRegion)
);
When I try to INSERT values into tables, it works for first two, but not recording into branch table. Why?

What do you try to insert, and what error do you get?
Since your tables have foreign key constraints, it means that you cannot insert a new line into these tables where the value for the foreign key does not exist in the referenced table.
In English: You cannot add a record in the branch table if there is no corresponding bank to which it belongs, same goes for region.

Related

MySQL architecture

I am new to relational database architecture creation. I am creating a very basic one where I have two data tables.
CREATLE TABLE Authors (
idAuthors INT NOT NULL auto_increment,
Name VARCHAR(50) NOT NULL,
Birthplace VARCHAR(50) NOT NULL,
PRIMARY KEY (idAuthors));
CREATLE TABLE Shops(
idShops NOT NULL auto_increment,
Name_shop VARCHAR(50) NOT NULL,
Name_authors VARCHAR(50) NOT NULL,
Location VARCHAR(50) NOT NULL
PRIMARY KEY (idShops));
My question is if you should have a foreign key in each table or an intermediate table that would be:
CREATLE TABLE intermediate(
PRIMARY KEY (idShops),
PRIMARY KEY (idAuthors));
Is this correct?
Thank you!
Something like
CREATE TABLE Authors (
idAuthors INT NOT NULL auto_increment,
NameAuthor VARCHAR(50) NOT NULL DEFAULT 'unknown',
BirthPlace VARCHAR(50) NOT NULL DEFAULT 'not defined',
PRIMARY KEY (idAuthors)
);
CREATE TABLE Shops(
idShops INT NOT NULL auto_increment,
NameShop VARCHAR(50) NOT NULL 'unspecified',
Location VARCHAR(50) NOT NULL 'not specified',
PRIMARY KEY (idShops)
);
CREATE TABLE Authors_Shops (
idAuthors INT NOT NULL,
idShops INT NOT NULL,
PRIMARY KEY (idAuthors, idShops),
CONSTRAINT fk_Authors FOREIGN KEY (idAuthors) REFERENCES Authors (idAuthors),
CONSTRAINT fk_Shops FOREIGN KEY (idShops) REFERENCES Shops (idShops)
);

Single attribute used as a foriegn key for multiple tables

I want to make Emp_id as a foreign key for Employee table and Record table. Can this be done?
This is my code
CREATE TABLE Empolyee(
EID varchar(8) NOT NULL,
E_name varchar(30) NOT NULL,
NID varchar(30) NOT NULL,
sex varchar(40) NOT NULL,
history varchar(30) NOT NULL,
salary varchar(10) NOT NULL,
cid int NOT NULL,
FOREIGN KEY (cid) REFERENCES Employee_Contact_No(CID),
FOREIGN KEY (cid) REFERENCES Employee_email(Email_ID),
PRIMARY KEY (EID)
);
Receptionist table
CREATE TABLE Receptionist(
r_id varchar(8) NOT NULL,
Emp_ID varchar(8) NOT NULL,
PRIMARY KEY (r_id),
FOREIGN KEY (Emp_ID) REFERENCES Employee(EID),
FOREIGN KEY (Emp_ID) REFERENCES Record(EID)
);
And the receptionist's records
CREATE TABLE Record(
record_no varchar(8) NOT NULL,
EID VARCHAR(8) Not Null,
patient_id varchar(15) NOT NULL,
discription varchar(30) NOT NULL,
appointment varchar(40) NOT NULL,
PRIMARY KEY (record_no)
);
You can make a single field reference multiple tables, but I cannot think of a scenario where it would ever be a good idea; it would require both tables to have the same id value, and (at least imply) that those id values be meaningfully coordinated.
From the looks of your examples, it is probably more likely that you need tables like Employee_Contact_No and Employee_email to reference the Employee table.

Cannot add or update a child row: foreign key constraint fails

Hi I am wondering why I getting an error
ERROR 1452 (23000): Cannot add or update a child row: a foreign key
constraint fails (exams.entries, CONSTRAINT entries_ibfk_2
FOREIGN KEY (student_id) REFERENCES students (student_id))
Any help would be appriatiated!
Here are the scripts that I think are relevant to the problem I'm having,
First I made this table entries and everything was fine
CREATE TABLE entries(
subject_id INT UNSIGNED NOT NULL,
subject_name VARCHAR(60) NOT NULL,
level_of_entry VARCHAR(60) NOT NULL,
exam_board VARCHAR(60) NOT NULL,
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
date_of_exam DATETIME NOT NULL,
PRIMARY KEY (date_of_exam),
FOREIGN KEY (subject_id)
REFERENCES subjects(subject_id),
FOREIGN KEY (student_id)
REFERENCES students(student_id)
Then I tried to insert data and I got the error.
INSERT INTO entries (subject_id,subject_name, level_of_entry, exam_board,date_of_exam)
VALUE ('1','chemistry','as','ocr','2017-05-05 12:00:00'),
('2','biology','gcse','aqa','2017-05-05 12:00:01'),
('3','music','gcse','edexcel','2017-05-05 12:00:02'),
('4','english','a','ocr','2017-05-05 12:00:03'),
('5','physics','a','aqa','2017-05-05 12:00:04'),
('6','maths','gcse','aqa','2017-05-05 12:00:05'),
('7','computing','gcse','aqa','2017-05-05 12:00:06'),
('8','physical_education','gcse','aqa','2017-05-05 12:00:07'),
('9','design_and_technology','gcse','aqa','2017-05-05 12:00:08'),
('10','french','gcse','aqa','2017-05-05 12:00:09');
It suggest on other questions similar to this to make sure your parent table has the same values but it does and its making know sense to me.
Here is the parent script.
CREATE TABLE IF NOT EXISTS students(
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
password CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (student_id) ,
UNIQUE (email));
Here is the other parent script but I'm having no problems with it.
CREATE TABLE IF NOT EXISTS subjects(
subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
subject_name VARCHAR(60) NOT NULL,
level_of_entry VARCHAR(60) NOT NULL,
exam_board VARCHAR(60) NOT NULL,
PRIMARY KEY (subject_id) ,
UNIQUE (subject_id));
If anyone could help I would be extremely grateful!
There are two issues with your logic related to student_id.
You should not have AUTO_INCREMENT in foreign key table.
A table which has a foreign key can't auto-populate values (logically and technically). It has to refer to some value in its Primary table. In your case, it is students table.
So, change your table structure as below:
CREATE TABLE entries(
subject_id INT UNSIGNED NOT NULL,
subject_name VARCHAR(60) NOT NULL,
level_of_entry VARCHAR(60) NOT NULL,
exam_board VARCHAR(60) NOT NULL,
student_id INT UNSIGNED NOT NULL,
date_of_exam DATETIME NOT NULL,
PRIMARY KEY (date_of_exam),
FOREIGN KEY (subject_id)
REFERENCES subjects(subject_id),
FOREIGN KEY (student_id)
REFERENCES students(student_id)
You should specify a value in your INSERT query for entries table
You need to ensure you insert a value manually in your entries table for student_id column.
A side not as specified in comments that you defined subject_id as INT but you are trying to insert character values.
Change your insert query as below:
INSERT INTO entries (subject_id,subject_name, level_of_entry, exam_board,date_of_exam, student_id)
VALUE (1,'chemistry','as','ocr','2017-05-05 12:00:00', 100),
(2,'biology','gcse','aqa','2017-05-05 12:00:01', 101);
Note: I assumed 100 and 101 as your existing student ids from students table. You need to replace them with the correct ids from your table.

Create mysql table, parenthesis error: You have an error in your SQL syntax; check the manual ... for the right syntax to use near ')' at line 12

As a recent SQL learner, I've tried this piece of code for creating six tables through dozens of times, but cannot figure out what did the shell mean by saying that something is wrong near ")".
If there are obvious syntax errors, I could have fixed it.
Thank you for helping.
-- Table APPOINTMENT
CREATE TABLE APPOINTMENT (
AppointmentID INTEGER NOT NULL AUTO_INCREMENT,
DateAndTime VARCHAR(20) NULL DEFAULT NULL,
Venue VARCHAR(20) NULL DEFAULT NULL,
PatientName VARCHAR(20) NULL DEFAULT NULL,
DoctorName VARCHAR(20) NULL DEFAULT NULL,
PatientID VARCHAR(20) NOT NULL,
DoctorID VARCHAR(20) NOT NULL,
IsEmergency VARCHAR(3) DEFAULT "YES",
IsVisited VARCHAR(3) DEFAULT "YES",
PRIMARY KEY (AppointmentID),
);
-- Table PATIENT
CREATE TABLE PATIENT (
PatientID VARCHAR(20) NOT NULL,
PatientName VARCHAR(20) NULL DEFAULT NULL,
Address VARCHAR(20) NULL DEFAULT NULL,
CurrentMedication VARCHAR(20) NULL DEFAULT NULL,
ChronicDisease VARCHAR(20) NULL DEFAULT NULL,
Allergies VARCHAR(20) NULL DEFAULT NULL,
MedicalHistory VARCHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (PatientID),
);
-- Table DOCTOR
CREATE TABLE DOCTOR (
DoctorID VARCHAR(20) NOT NULL,
DoctorName VARCHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (DoctorID),
);
-- Table BILL
CREATE TABLE BILL (
AppointmentID INTEGER NOT NULL AUTO_INCREMENT,
DoctorID VARCHAR(20) NOT NULL,
PatientID VARCHAR(20) NOT NULL,
DateAndTime VARCHAR(20) NULL DEFAULT NULL,
Diagnosis VARCHAR(20) NULL DEFAULT NULL,
Treatment VARCHAR(20) NULL DEFAULT NULL,
Fees INTEGER NULL DEFAULT NULL,
ServiceCharge INTEGER NULL DEFAULT NULL,
ClaimId INTEGER NOT NULL,
InsuredDeduction INTEGER NULL AUTO_INCREMENT DEFAULT NULL,
ReceiptNumber INTEGER NOT NULL,
AmountPaid INTEGER NULL DEFAULT NULL,
TotalAmountDue INTEGER NULL DEFAULT NULL,
PRIMARY KEY (AppointmentID),
);
-- Table PAYMENT
CREATE TABLE PAYMENT (
ReceiptNumber INTEGER NOT NULL AUTO_INCREMENT,
AppointmentID INTEGER NOT NULL,
AmountPaid INTEGER NULL DEFAULT NULL,
PatientID VARCHAR(20) NOT NULL,
PRIMARY KEY (ReceiptNumber)
);
-- Table CLAIM
CREATE TABLE CLAIM (
ClaimId INTEGER NOT NULL AUTO_INCREMENT,
InsuranceCompanyName VARCHAR(20) NULL DEFAULT NULL,
PatientID VARCHAR(20) NOT NULL,
PRIMARY KEY (ClaimId),
);
-- Foreign Keys
ALTER TABLE APPOINTMENT ADD FOREIGN KEY (PatientID) REFERENCES PATIENT (PatientID);
ALTER TABLE APPOINTMENT ADD FOREIGN KEY (DoctorID) REFERENCES DOCTOR (DoctorID);
ALTER TABLE PAYMENT ADD FOREIGN KEY (AppointmentID) REFERENCES BILL (AppointmentID);
ALTER TABLE PAYMENT ADD FOREIGN KEY (PatientID) REFERENCES PATIENT (PatientID);
ALTER TABLE BILL ADD FOREIGN KEY (DoctorID) REFERENCES DOCTOR (DoctorID);
ALTER TABLE BILL ADD FOREIGN KEY (PatientID) REFERENCES PATIENT (PatientID);
ALTER TABLE BILL ADD FOREIGN KEY (ClaimId) REFERENCES CLAIM (ClaimId);
ALTER TABLE BILL ADD FOREIGN KEY (ReceiptNumber) REFERENCES PAYMENT (ReceiptNumber);
ALTER TABLE CLAIM ADD FOREIGN KEY (PatientID) REFERENCES PATIENT (PatientID);
You have an extra comma in every create table statement. eg PRIMARY KEY (DoctorID) has a trailing comma. MySql expects another column declaration after that comma.
-- Table DOCTOR
CREATE TABLE DOCTOR (
DoctorID VARCHAR(20) NOT NULL,
DoctorName VARCHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (DoctorID)
);

How to create table in mysql database with foreign key which is primary key of another table?

CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id),
);
This is my first table(employee_login)and I want e_id as a foreign key in my next table (login) below
CREATE TABLE login(
login_id int auto_increment,
username varchar(20) not null,
password varchar(20) not null,
primary key (login_id),
e_id references employee_detail(e_id)
);
You can do it like follows :
CREATE TABLE person (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirt (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
PRIMARY KEY (id)
);
I hope you understand the create table code. owner in the shirt table is the foreign key referencing id from the person table
You have several mistakes. You were missing e_id column in your Login Table, you can't add a foreign key without adding employee_detail's primary key column which in this case is e_id. Also you can't use password as a column name since it is a preestablished query you'll need to use something else like "pass".
CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id));
CREATE TABLE login(
login_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
pass VARCHAR(20) NOT NULL,
e_id INT,
FOREIGN KEY (e_id) REFERENCES employee_detail(e_id)
);