MySQL architecture - mysql

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

Related

MySQL ERROR 1215 (HY000): Cannot add foreign key constraint when I'm trying to create a table

I tried looking up the reasons why it's failing but I can't find the different data types issues mentioned in the solutions in my tables.
I'm getting the error when I'm trying to run the following:
comments (id int NOT NULL AUTO_INCREMENT,
user_id varchar(10) NOT NULL,
blog_id int NOT NULL,
comment varchar(150) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES user_details(id),
FOREIGN KEY(blog_id) REFERENCES blog(id));
existing data bases for primary keys:
user_details (id varchar(10) NOT NULL ,
name varchar(70) NOT NULL,
email varchar(40) NOT NULL,
salt varchar(40) NOT NULL,
masked_password varchar(40) NOT NULL,
is_active varchar(10) DEFAULT 'False');
blog (id int NOT NULL AUTO_INCREMENT,
category_id int NOT NULL,
title varchar(240) NOT NULL,
body longtext NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY(category_id) REFERENCES blog_catagories(id));
Let me know where I'm going wrong

Add a foregin key to a different table

I'm trying to add username from my st_accounts column into my table results using mySQL.
st_accounts table
`id` int(11) NOT NULL, //Primary Key
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
`img` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
results table
`score_ID` int(11) NOT NULL, //Primary key
`score` int(20) NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think you want:
create table accounts (
account_id int auto_increment primary key,
. . .
);
create table results (
result_id int auto_increment primary key,
account_id int not null
score int NOT NULL,
score_date date NOT NULL,
constraint fk_results_accounts foreig key (accounts_id) references accounts(account_id)
) ;
Notes:
Declare the primary key explicitly, not in a comment.
My convention is to name the primary key after the name of the table (in singular) with _id after it.
The foreign key has the same name as the primary key -- self documenting.
I made the integer primary keys auto_increment, so the database can assign unique values.

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

#1005 - Can't create table 'workswell_database.skoleni' Novice user with MySQL

sorry for my stupid question,but I do database in MySQL and was so happy,when i finished.Unfortunately,I had found a lot of mistakes in my database n when i will properly fix a bug,so i've another. Also,this is my Database and that guy what really boring right now,please write about my bugs and solution for them.I novice as a programmer and this database i do about 4 days !
CREATE TABLE`Skoleni` (
`sk_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Cena` MEDIUMINT NOT NULL,
`Obsazenost` text NOT NULL,
`Kapacita` datetime NOT NULL,
`Popis` text,
`Prerekvizity` text,
`Certifikat` MEDIUMINT NOT NULL,
PRIMARY KEY (`sk_id`),
FOREIGN KEY (`sk_id`) REFERENCES Skoleni_has_Termin(`Skoleni_sk_id`),
FOREIGN KEY (`sk_id`) REFERENCES Skoleni_has_Uzivatel(`Skoleni_sk_id`)
);
CREATE TABLE `Skoleni_has_Termin`(
`Skoleni_sk_id`INT NOT NULL UNIQUE,
`Termin_ter_id` INT NOT NULL UNIQUE,
PRIMARY KEY (`Skoleni_sk_id`)
);
CREATE TABLE `Termin` (
`ter_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Datum_cas` DATETIME NOT NULL,
`Misto_mo_id` INT NOT NULL,
PRIMARY KEY (`ter_id`),
FOREIGN KEY (`ter_id`) REFERENCES Skoleni_has_Termin(`Termin_ter_id`)
);
CREATE TABLE `Misto` (
`mo_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`ulice` MEDIUMINT NOT NULL,
`cislo_popisne` MEDIUMINT NOT NULL,
`lat` FLOAT (10,6) NOT NULL,
`lng` FLOAT (10,6) NOT NULL,
PRIMARY KEY (`mo_id`)
)ENGINE = MYISAM;
SELECT TABLE.Misto(
INSERT (`ulice`, `cislo_popisne`, `lat`, `lng`),
VALUES (`dr_Zikmunda_Wintra_376_5``16000 Praha 6 Bubenec``14.407438``50.101049`)
);
CREATE TABLE `Skoleni_has_Uzivatel` (
`Skoleni_sk_id` INT NOT NULL UNIQUE,
`Uzivatel_uziv_id` INT NOT NULL UNIQUE,
PRIMARY KEY (`Skoleni_sk_id`)
);
CREATE TABLE `Uzivatel` (
`uziv_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Jmeno` VARCHAR(30) NOT NULL,
`Typ` MEDIUMINT UNIQUE,
`Heslo` VARCHAR(32) NOT NULL,
`Potvrzeni` VARCHAR(1) NOT NULL,
PRIMARY KEY (`uziv_id`),
FOREIGN KEY (`uziv_id`) REFERENCES Skoleni_has_Uzivatel(`Uzivatel_uziv_id`)
);
CREATE TABLE `Skoleni_has_Lektor` (
`Skoleni_sk_id` INT NOT NULL UNIQUE,
`Lektor_lek_id` INT NOT NULL UNIQUE,
PRIMARY KEY (`Lektor_lek_id`)
);
CREATE TABLE `Lektor` (
`lek_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`Titul'pred'` VARCHAR(10) NOT NULL,
`Jmeno` VARCHAR(20) NOT NULL,
`Prijmeni` VARCHAR(20) NOT NULL,
`Titul'za'` VARCHAR(10),
`Firma` VARCHAR(30),
`Rodne cislo` CHAR(11) NOT NULL,
`Datum narozeni` DATE NOT NULL,
PRIMARY KEY (`lek_id`)
);
CREATE TABLE `Firma` (
`fir_id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`E-mail` VARCHAR(15) NOT NULL,
`Telefon` VARCHAR(15) NOT NULL,
`Web` VARCHAR(30),
`IC` CHAR(8) NOT NULL,
`DIC` VARCHAR(12) NOT NULL,
`Misto_mo_id` INT NOT NULL,
PRIMARY KEY (`fir_id`),
PRIMARY KEY (`Misto_mo_id`),
FOREIGN KEY (`Misto_mo_id`) REFERENCES Firma(`Misto_mo_id`)
);
The tables `skoleni_has_termin' and 'skoleni_has_lektor' are most likely not needed here. I would bring dates to trainings table and leave just lektor_id in skoleni table as relation between trainings and lecturers is 1:n not m:n (unless one training can have more than one lecturer)

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