I have 5 tables which are
Users
-id
-influencer_id
Influencers
-id
Categories
-catogory_id
-influencer_id
platforms
-influencer_id
-platform_id
tasks
-influencer_id
-task_id
I want to delete an influencer and also delete all records at once. How to do that?
use ON DELETE CASCADE while creating tables. Like:
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
First delete records from other tables. You can use following query:
DELETE FROM Users u
USING Influencers i
WHERE u.id = i.id;
You can do the same for other tables and then at last DROP the Influencers table.
DROP TABLE Influencers;
Related
Database scheme:
If i create a new tag then book_table create a new column automatically.
Is it possible?
Create a relationship table instead.
CREATE TABLE book_tag_t
( book_id INT UNSIGNED NOT NULL
, tag_id INT UNSIGNED NOT NULL
, PRIMARY KEY (book_id,tag_id)
, CONSTRAINT book_tag_t_book FOREIGN KEY (book_id) REFERENCES book_t (id)
ON DELETE CASCADE ON UPDATE CASCADE
, CONSTRAINT book_tag_t_tag FOREIGN KEY (tag_id) REFERENCES tag_t (id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
To relate a `tag\ to a \book`
INSERT INTO book_tag_t (book_id, tag_id) VALUES ( 2, 14 );
To get all of the books related to a specific tag:
SELECT b.id
FROM book_t b
JOIN book_tag_t r
ON r.book_id = b.id
JOIN tag_t t
ON t.id = r.tag_id
WHERE t.tagName = 'Science'
ORDER BY b.id
To address the question you asked, "Is it possible?"
You could probably get something working. That will require a DDL ALTER TABLE statement to be executed on book_table, and that's going to be expensive in terms of concurrency (exclusive locks), and resources (creating a new copy of the table). And you can't do an ALTER TABLE in a trigger. If you actually need functionality like this, the required SQL would be better executed from the application, not from something "automatic" in the database.
What are you going to do when the number of rows in `tag_t` exceeds the number of columns allowed in a table? When a row is deleted? or updated?
It's a horrible idea.
The normal relational approach is to add a third table, a relationship table, to resolve the many-to-many relationship between `book` and `tag`. As illustrated in the first part of this answer.
I am creating my database with primary key i am passing this primary key to another table suppose, now i am deleting primary key my query is not fetching the value because i am using join to retrieve the data what is solution for that because this primary id is missing in the reference table in php mysql.
community table :
c.d_id communityname
1 BC
2 SC
3 ST
student Table
ID Community
1 1
2 3
3 1
SQL
SELECT c.communityname FROM student s left join community c on c.d_id=s.community WHERE s.stud_rollno = '".$rollno."' GROUP BY stud_rollno
Instead of using LEFT Join, you can use a simple Sub Query with a WHERE clause da!
SELECT `id`, (
SELECT `communityname` FROM `community` WHERE `Community`=`d_id`
) FROM `student`;
Else you need to cascade the primary keys and foreign keys.
If I got your problem right, you are using a primary key as an attribute in another table, it is a foreign key for that new table.
By specifying this, you can use cascade behaviors to remove "foreign rows" when the primary key is deleted in the first table.
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE
);
One way to go about this is to update the student table with another Community ID that exists in the Community table, delete the student with the ON DELETE CASCADE.
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()
How can I relate a table with multiple records from another table?
Basically, I have a table for an 'event', how do I keep track of which 'users' (in their own seperate table) are in a particular event? Right now I just have a column in the 'event' table with a comma separated list of the users IDs who have joined that 'event'.
There must be a better way to do this...right?
Typically you have a table called users_in_event which holds one row for each user in a many-to-many relationship with the events table. So for each event, you will have a number of table rows mapped individually to users.
CREATE TABLE users_in_event
(
user_id INT,
event_id INT,
FOREIGN KEY user_id REFERENCES users (user_id) ON UPDATE CASCADE,
FOREIGN KEY event_id REFERENCES events (event_id) ON UPDATE CASCADE
-- Optionally, use ON DELETE CASCADE for the events foreign key
-- FOREIGN KEY event_id REFERENCES events (event_id) ON UPDATE CASCADE ON DELETE CASCADE
)
To find out which users are in an event, do:
SELECT
users_in_event.user_id,
users.name
FROM users JOIN users_in_event ON users.user_id = users_in_event.user_id
WHERE event_id=1234;
If you have a many-to-many relationship, that is, users can attend many events, and events can be attended by many users, that would traditionally be represented with a mapping table with the primary key from each table, plus any attributes specific to the user-event relationship.
For example, if you have ID columns in your USER and EVENT tables that are both type INT:
CREATE TABLE USER_EVENT_MAPPING
(
USER_ID INT,
EVENT_ID INT
)