Currently, I'm working for my assignment project called 'Competition Management' and I don't know much about SQL and PHP. Afaik, my questions should be workout in PHP to do this or that but I'm just curious with these SQL. You can ignore the empty varchar.
2 Questions
Player Table is related to Competition and Association Table. Do I have to do something with it or just let it be?
In Competition Table, you can see config_competition field... but, the field should contain data inside it... in term of sport competition, lets say we have football sport, gaming e-sport, athlete sport, and etc... In this case, should I create a new table? If yes, how do I make it related with the Competition Table?
This is Organisation Table (who creates a competition)
CREATE TABLE organisation (
id_organisation varchar() PRIMARY KEY,
name_organisation varchar(),
password varchar()
);
This is Competition Table (Created by Organiser)
CREATE TABLE competition (
id_competition varchar() PRIMARY KEY,
name_competition varchar(),
config_competition varchar()
);
This is Association Table (participate a competition)
CREATE TABLE association (
id_association varchar() PRIMARY KEY,
name_association varchar(),
password varchar()
);
This is Player Table (registered by Association to participate player in a competition)
CREATE TABLE player (
id_player varchar() PRIMARY KEY,
name_player varchar()
);
You need to use foreign keys to refer to another table's primary keys. In that way, you can build a relationship between tables. In other words, you can connect the tables that have relationships with each other.
I can suggest you use MySQL workbench and draw an ER diagram of your project in the first phase of your project. After that, you can create a database.
Also, before all these work, you may need to look at the relational data models, one-to-many, one-to-one, and many-to-many. Then you can select the appropriate relations between tables.
For your second question below is an answer:
CREATE TABLE category (
category_id int PRIMARY KEY,
category_name varchar(),
);
CREATE TABLE competition (
id_competition varchar() PRIMARY KEY,
name_competition varchar(),
category_id int FOREIGN KEY REFERENCES category(category_id)
);
Related
I have two tables: Employee (ID, Name, Address) and Store(ID,Address) and I would like to record information about people who work in each store.
I thought of making a new table called Employee_List table. My questions:
1- Employee_List and Employee has one-to-many relation, right?
2- Employee_list to store has one-to-one relation, right?
3- How to define foreign and primary keys for Employee_List table?
Employee_list should have:
employee_listid (INT PK)
employee_id (INT FK)
store_id (INT FK)
I would recommend changing the table name to represent the composite table, i.e.
EmployeeStores. This would allow your schema to be scalable, employees can work in multiple stores.
In SQL Server:
CREATE TABLE EmployeeStores
(
EMPLOYEEStoreID INT IDENTITY,
EMPLOYEEID INT FOREIGN KEY REFERENCES Employee(employee_id),
STOREID INT FOREIGN KEY REFERENCES Store(store_id)
)
I've been working on a wordpress plugin that is designed to create a character for a wordpress user using MYSQL. I'm new to php and sql. I'm currently struggling with two tables in my DB. A class table and a skill table.
The class table is comprised of the different classes a character can be. For example; a Warrior, Paladin or Wizard.
The skills table is comprised of different skills a character can use. For example; lightning bolt, slay, or heal.
I want these tables to have a relation with each other. Only certain skills from the skills tabled can be used by a class from the class table. So for example, a Paladin class can only choose the heal skill and a Warrior class can only use the slay skill. I'm just not sure what to use to make that work successfully.
I'm not sure if there's a condition that can be used on the skills table that only allows a certain class to reference certain skills. Could anyone point me in the right direction? Thanks for any help in advance!
A many-to-many relationship will do the trick here, this is what I would do.
I would create 3 tables, 1) Character 2) Skill 3) character_skill, here is a very simple example:
CREATE TABLE hero
(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255)
);
CREATE TABLE skill
(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255)
)
CREATE TABLE character_skill
(
id INT PRIMARY KEY AUTO_INCREMENT,
charachter_id INT,
skill_id INT,
CONSTRAINT character_skill_character_id_fk FOREIGN KEY (charachter_id) REFERENCES hero (id),
CONSTRAINT character_skill_skill_id_fk FOREIGN KEY (skill_id) REFERENCES skill (id)
);
Now you will store the characters on the hero table, and the skills on the skill table, now here is the trick, to assign a hero(Character) a skill you will add a record to the character_skill table with the id of the hero and the id of the skill. In the example I added a constraint that will only allow you to add characters and skills that where previously created in the other tables.
Then to query it, I will look for all the Skills on the character_skill table that have the same charachter id, liks so:
select
skill.name
from skill
INNER JOIN character_skill
on character_skill.skill_id = skill.id
where character_skill.charachter_id = 3
This is a very simple example.
This is the basic idea, only the skills connected in the character_skill table can be use by character with id 3
I used hero for the name of the character table because the 'character' name is reserved
I'm not sure how to store or insert this data. I am using PHP and MySQL.
Let's say we're trying to keep track of people who enter marathons (like jogging or whatever). So far, I have a Person table that has all my person information. Each person happens to be associated with a unique varchar(40) key. There is a table for the marathon information (Marathon). I receive the person data in an CSV that as about 130,000 rows and import that into the database.
So - now the question is... how do I deal with that association between Person and Marathon? For each Marathon, I get a huge list of participants (by that unique varchar key) that I need to import. So... If I go the foreign key route, it seems like the insert would be very heavy and cumbersome to look up the appropriate foreign key for the person. I'm not even sure how I would write that insert... I guess it would look like this:
insert into person_marathon
select p.person_id, m.marathon_id
from ( select 'person_a' as p_name, 'marathon_a' as m_name union
select 'person_b' as p_name, 'marathon_a' as m_name )
as imported_marathon_person_list
join person p
on p.person_name = imported_marathon_person_list.p_name
join marathon m
on m.marathon_name = imported_marathon_person_list.m_name
There are not a lot of marathons to deal with at one time. There a lot of people, though.
--> Should I even give the person an ID and require all the foreign keys? or just use the unique varchar(40) as the true table key? But then I would have to join tables on a varchar and that's bad. A marathon can have anywhere from 1k to 30k participants.
--> Or, I could select the person info and the marathon info from the database and join it with the marathon_person data in PHP before I send it over to MySQL.
--> Or, I guess, maybe make a temporary table, then join in the db, then insert (through PHP)? It's been already strongly suggested that I do not use temporary tables ever (this is a work thing and this isn't my database).
Edit: I am not sure on what schema to use because I'm not sure if I should be using foreign keys or not (purpose of this whole post is to answer that question) but the basic design would be something like...
create table person (
person_id int unisgned auto_incrememnt,
person_key varchar(40) not null,
primary key (person_id),
constraint uc_person_key unique (person_key)
)
create table marathon (
marathon_id int unisgned auto_incrememnt,
marathon_name varchar(60) not null,
primary key (marathon_id)
)
create table person_marathon (
person_marathon_id int unsigned auto_increment,
person_id int unsigned,
marathon_id int unsigned,
primary key (person_marathon_id),
constraint uc_person_marathon unique (person_id, marathon_id),
foreign key person_id references person (person_id),
foreign key marathon_id references marathon (marathon_id)
)
I'm going to repeat the actual question really quick.... If I choose to use a foreign key for person, how do I import all the person_marathon data with the person_id in an efficient way? The insert statement I included above is my best guess....
The person data comes in a CSV of about 130,000 rows so that is a straight import into the person table. The person data comes with a unique varchar(40) for each person.
The person_marathon data comes in a CSV for each marathon, as a list of 1,000 to 30,000 unique varchar(40)'s that represent each person who participated in that marathon.
Summary: I am using PHP. So what is the best way to write the insert/import of the person_marathon data if I am using foreign keys? Would I have to do it like the insert statement above or is there a better way?
This is a many-to-many relationship, one person can enter many marathons, one marathon can be entered by many persons. You need additional table in your data model to track this relation, for example:
CREATE TABLE persons_marathons(
personID int FOREIGN KEY REFERENCES Persons(P_Id),
marathonID int FOREIGN KEY REFERENCES Marathons(M_Id)
)
This table uses foreign key constraints. The foreign key constraint prevents from inserting bad data (for example you cannot insert a row with personID = 123 when there is no such id in Persons table), it prevents also from deletes that would destroy a link between tables (for example you cannot delete a person X when exists a record in person_marathon table witth such personID).
If this table contains the following rows:
personID | MarathonID
----------+-----------
2 | 3
3 | 3
2 | 8
3 | 8
it means that persons 2 and 3 both entered marathons 3 and 8
I need to store multiple id's in either a field in the table or add another table to store the id's in.
Each member will basically have favourite articles. Each article has an id which is stored when the user clicks on a Add to favourites button.
My question is:
Do I create a field and in this field add the multiple id's or do I create a table to add those id's?
What is the best way to do this?
This is a many-to-many relationship, you need an additional table storing pairs of user_id and article_id (primary keys of user and article tables, respectively).
You should create a new table instead of having comma seperated values in a single column.
Keep your database normalized.
You create a separate table, this is how things work in a relational database. The other solution (comma separated list of ids in one column) will lead to an unmaintainable database. For example, what if you want to know how many times an article was favorited? You cannot write queries on a column like this.
Your table will need to store the user's id and the article's id - these refer to the primary keys of the corresponding tables. For querying, you can either use JOINs or nested SELECT queries.
As lafor already pointed out this is a many-to-many relationship and you'll end up with three tables: user, article, and favorite:
CREATE TABLE user(
id INT NOT NULL,
...
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE article (
id INT NOT NULL,
...
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE favorite (
userID INT NOT NULL,
articleID INT NOT NULL,
FOREIGN KEY (userID) REFERENCES user(id) ON DELETE CASCADE,
FOREIGN KEY (articleID) REFERENCES article(id) ON DELETE CASCADE,
PRIMARY KEY (userID, articleID)
) ENGINE=INNODB;
If you then want to select all user's favorite articles you use a JOIN:
SELECT * FROM favorite f JOIN article a ON f.articleID = a.id WHERE f.userID = ?
If you want to know why you should use this schema, I recommend reading about database normilization. With multiple IDs in a single field you would even violate the first normal form and thus land in a world of pain...
I am new to php and mysql. I created a database named 'students' which contain two tables as 'student_details' which have fields like 'ID, Name, Age, Tel#, Address' and another table as 'fee_details' which have fields like 'ID(student_details table ID), Inst Id, Date, Receipt No'.
I want to set a foreign key and retrieve data from both tables when a student paid their fees and if a student passed out or discontinued I want a delete option to delete his all records from my tables. So please help me to solve this by PHP code and displays it in HTML while using a search form.
Enforcing referential integrity at the database level is the way to go. I believe when you said you wanted the delete "to delete his all records from my tables" you meant deleting the row and all its child records. You can do that by using foreign keys and ON DELETE CASCADE.
CREATE TABLE students
(
student_id INT NOT NULL,
name VARCHAR(30) NOT NULL,
PRIMARY KEY (student_id)
) ENGINE=INNODB;
CREATE TABLE fee_details
(
id INT,
date TIMESTAMP,
student_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id)
ON DELETE CASCADE
) ENGINE=INNODB;
With this, when a student is deleted from the students table, all its associated records will be deleted from fee_details.
you can try mysql_query() and mysql_assoc_array()