How to create a weak entity in mySql? - mysql

I want to know how can I create weak entities in mySql by Creating tables, I have the code like this:
CREATE TABLE users(
user_Id int AUTO_INCREMENT,
full_Name varchar(60),
email varchar(30),
password varchar(30),
reg_Date timestamp
);
CREATE TABLE personal_Infos(
...
);
These are the tables, all I want to Know is how can I Connect to each-other with foreign key and should I create another primary key at the second table? (Second table has some more informations for the first table)

should I create another primary key at the second table?
Yes you do need to create a primary key in your second table personal_Infos table and that primary key will become foreign key in your users table.
how can I Connect to each-other with foreign key ?
Suppose your primary key in personal_Infos table is P_id, then you can add it as foreign key in your users table like shown below
CREATE TABLE users(
user_Id int AUTO_INCREMENT,
full_Name varchar(60),
email varchar(30),
password varchar(30),
reg_Date timestamp,
p_id int not null,
FOREIGN KEY (P_Id) REFERENCES personal_Infos(P_Id)
);

Related

MySQL composite primary key and foreign keys at the same time

I'm trying to create a web application about cooking, and I get weird behaviours when creating my MySQL database. I have the following tables: friends and user, described as below
CREATE TABLE IF NOT EXISTS user (
user_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR (100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS friends (
user_id int NOT NULL,
friend_id int NOT NULL,
PRIMARY KEY (user_id, friend_id),
FOREIGN KEY (user_id) REFERENCES user(user_id),
FOREIGN KEY (friend_id) REFERENCES user(user_id)
);
For some reason, in my friends table, the composite primary key gets created, but only the second field friend_id is a foreign key. I have the exact same problem when I create a table named recipe_composition, when I create a composite key recipe_id, ingredient_id. The composite primary keys gets created but only the second field ingredient_id gets the foreign key reference.
Any help would be appreciated, thank you.

Simple attendance list SQL schema architecture advice

I have a sport club presence list on paper which I have to bring online.
Basically, I have everything in handle except the SQL schema architecture.
I have never designed a DataBase and I'm not sure if it's the right way. I appreciate any help!
If the person is present we make a x with a pen on paper :)
To do to look the same like on the paper but in an app I try to develop a Java Vaadin App with Spring Data JPA-Hibernate.
On paper and in the future APP look like this:
And this is the MySQL schema:
create table person(
id int PRIMARY KEY AUTO_INCREMENT,
first_name varchar(20)
);
create table isPresent(
id int PRIMARY KEY AUTO_INCREMENT,
isPresent boolean
);
create table persons_isPresent(
person_id int,
isPresent_id int,
FOREIGN KEY (person_id)
REFERENCES person(id),
FOREIGN KEY (isPresent_id)
REFERENCES isPresent(id)
);
create table training(
id int PRIMARY KEY AUTO_INCREMENT,
person_id int,
isPresent_id int,
training_time datetime,
FOREIGN KEY (person_id)
REFERENCES person(id),
FOREIGN KEY (isPresent_id)
REFERENCES isPresent(id)
);
I have never designed a DataBase and I'm not sure if it's the right way. I appreciate any help!
It seems unnecessary to have ispresent ID's.
I would keep your Person table as is.
Create a Training table with an ID and time, and an TrainingAttendance table with a Training ID and a Person ID. If you want to check a person's attendance at a training, check if a record exists in TrainingAttendance with the Person's ID and the Training ID.
CREATE TABLE PERSON (
PERSON_ID INTEGER NOT NULL PRIMARY KEY,
FIRST_NAME VARCHAR(20) NOT NULL);
CREATE TABLE TRAINING (
TRAINING_ID INTEGER NOT NULL PRIMARY KEY
TRAINING_TIME DATETIME NOT NULL);
CREATE TABLE TRAINING_ATTENDANCE (
TRAINING_ID INTEGER NOT NULL,
PERSON_ID INTEGER NOT NULL,
FOREIGN KEY(PERSON_ID) REFERENCES PERSON(PERSON_ID),
FOREIGN KEY(TRAINING_ID) REFERENCES TRAINING(TRAINING_ID),
PRIMARY KEY(TRAINING_ID, PERSON_ID));
I think it might be cleaner to think about training sessions independently. Then you could achieve that with three tables [training]<-[attendee]->[person], where the attendee is a binding table. So maybe something like this:
create table training(
id int PRIMARY KEY AUTO_INCREMENT,
training_time datetime
);
create table person(
id int PRIMARY KEY AUTO_INCREMENT,
first_name varchar(20)
);
create table attendee(
id int PRIMARY KEY AUTO_INCREMENT,
person_id int,
training_id int,
FOREIGN KEY (person_id) REFERENCES person(id),
FOREIGN KEY (training_id) REFERENCES training(id)
);
I tried it out with some simple inserts/select and it might do the trick for you?
insert INTO person (first_name) VALUES ('Pavel');
insert INTO training (training_time) VALUES (NOW());
insert INTO attendee (person_id, training_id) VALUES (1,1);
select person.first_name, training.training_time
from attendee, person, training
where person.id = attendee.person_id
AND training.id = attendee.training_id;

What is the wrong in the following MySql query?

create table rolemaster(roleno int(2) not null,
primary key(roleno),
rolename varchar(20));
create table roombed(roomnumber varchar(10),
bednumber varchar(10) not null primary key,
username varchar,
foreign key(username) references roleusers(username));
It is showing
ErrorNr: 1064.
How can I resolve this error?
You've forgot to define a size for username varchar.
CREATE TABLE roombed
(
roomnumber VARCHAR(10),
bednumber VARCHAR(10) NOT NULL PRIMARY KEY,
username VARCHAR(10),
-- ^--^----------- This
FOREIGN KEY(username) REFERENCES roleusers(username)
);
Give this a shot
create table roleusers(username varchar(10) not null primary key );
create table rolemaster(roleno int(2) not null primary key ,
rolename varchar(20));
create table roombed(roomnumber varchar(10),
bednumber varchar(10) not null primary key,
username varchar(10),
foreign key (username) references roleusers (username));
Ive added a roleusers table so I can give you a working script.
EDIT : What you were doing wrong for starters was the comma before the primary key.
The foreign key you added to a table we dont know about have to reference a key on the other table so in the example posted Ive added a roleusers table setting username as a primary key so it could be referenced later as a foreign key.
You had also forgotten to correctly define the varchar on username. as mentioned by another answer.
Hope this explanation helps

mysql One-To-Many Relationship

use one_to_many;
create table models(
model_id int primary key auto_increment unique,
name varchar(50),
manufacturer_id int
);
create table manufacturers(
manufacturer_id int primary key,
name varchar(50),
established_on timestamp,
constraint fk_manufacturers_models foreign key(manufacturer_id)
references models(manufacturer_id)
);
When i try to create one to many model like this example https://ibb.co/bPRGQ5 , it throws an error 1215 cannot add foreign key constraint. What am i doing wrong?
models.manufacturer_id must be indexed.
But normally you would put the foreign key constraint on the many table (models) not the one table.
If I understood correctly what you want to get try this
create table manufacturers(
manufacturer_id int primary key auto_increment unique,
name varchar(50),
established_on timestamp
);
create table models(
model_id int primary key auto_increment unique,
name varchar(50),
manufacturer_id int,
foreign key fk_manufacturer(manufacturer_id)
references manufacturers(manufacturer_id)
);

Can a table have two foreign keys?

I have the following tables (Primary key in bold. Foreign key in Italic)
Customer table
ID---Name---Balance---Account_Name---Account_Type
Account Category table
Account_Type----Balance
Customer Detail table
Account_Name---First_Name----Last_Name---Address
Can I have two foreign keys in the Customer table and how can I implement this in MySQL?
Updated
I am developing a web based accounting system for a final project.
Account Category
Account Type--------------Balance
Assets
Liabilities
Equity
Expenses
Income
Asset
Asset_ID-----Asset Name----Balance----Account Type
Receivable
Receivable_ID-----Receivable Name-------Address--------Tel-----Asset_ID----Account Type
Receivable Account
Transaction_ID----Description----Amount---
Balance----Receivable_ID----Asset_ID---Account Type
I drew the ER(Entity relationship) diagram using a software and when I specify the relationship it automatically added the multiple foreign keys as shown above. Is the design not sound enough?
create table Table1
(
id varchar(2),
name varchar(2),
PRIMARY KEY (id)
)
Create table Table1_Addr
(
addid varchar(2),
Address varchar(2),
PRIMARY KEY (addid)
)
Create table Table1_sal
(
salid varchar(2),`enter code here`
addid varchar(2),
id varchar(2),
PRIMARY KEY (salid),
index(addid),
index(id),
FOREIGN KEY (addid) REFERENCES Table1_Addr(addid),
FOREIGN KEY (id) REFERENCES Table1(id)
)
Yes, MySQL allows this. You can have multiple foreign keys on the same table.
Get more details here FOREIGN KEY Constraints
The foreign keys in your schema (on Account_Name and Account_Type) do not require any special treatment or syntax. Just declare two separate foreign keys on the Customer table. They certainly don't constitute a composite key in any meaningful sense of the word.
There are numerous other problems with this schema, but I'll just point out that it isn't generally a good idea to build a primary key out of multiple unique columns, or columns in which one is functionally dependent on another. It appears that at least one of these cases applies to the ID and Name columns in the Customer table. This allows you to create two rows with the same ID (different name), which I'm guessing you don't want to allow.
Yes, a table have one or many foreign keys and each foreign keys hava a different parent table.
CREATE TABLE User (
user_id INT NOT NULL AUTO_INCREMENT,
userName VARCHAR(100) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
userImage LONGBLOB NOT NULL,
Favorite VARCHAR(255) NOT NULL,
PRIMARY KEY (user_id)
);
and
CREATE TABLE Event (
EventID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (EventID),
EventName VARCHAR(100) NOT NULL,
EventLocation VARCHAR(100) NOT NULL,
EventPriceRange VARCHAR(100) NOT NULL,
EventDate Date NOT NULL,
EventTime Time NOT NULL,
EventDescription VARCHAR(255) NOT NULL,
EventCategory VARCHAR(255) NOT NULL,
EventImage LONGBLOB NOT NULL,
index(EventID),
FOREIGN KEY (EventID) REFERENCES User(user_id)
);