I thought i was in the right track. I forgot that a user can do multiple activities. What I did was it's only one activity. What can i do with this? I came up with a solution that might work, come up with an sql statement that updates the activity column without overwriting the text. But that seems stupid. Pretty sure theres a better solution?
I made some adjustment but I think this will improve your database. See this in action via http://sqlfiddle.com/#!2/c6546/9/0
CREATE TABLE workers
(
id_workers int primary key,
username varchar(30),
type varchar(30)
);
CREATE TABLE activities
(
id_activities int auto_increment primary key,
activity varchar(30)
);
CREATE TABLE timesheet
(
id_timesheet int auto_increment primary key,
id_workers int,
id_activities int,
start_time datetime,
end_time datetime
);
INSERT INTO workers
(id_workers, username, type)
VALUES
('1','Danice', 'Administrator'),
('2','Micheal', 'Administrator');
INSERT INTO activities
(activity)
VALUES
('Edit Employee'),
('Edit Employee Information'),
('Edit Department');
INSERT INTO timesheet
(id_workers, id_activities,start_time, end_time)
VALUES
('1', '1', '2013-03-14 13:15:00','2013-03-14 13:20:00'),
('2', '2', '2013-03-14 13:00:00','2013-03-14 14:00:00'),
('1', '2', '2013-03-14 13:21:00','2013-03-14 13:23:00'),
('1', '3', '2013-03-14 13:24:00','2013-03-14 13:45:00');
SELECT a.id_activities, w.username, w.type, a.activity, t.start_time, t.end_time
FROM (timesheet AS t LEFT JOIN activities AS a ON t.id_activities = a.id_activities ) LEFT JOIN workers AS w ON t.id_workers = w.id_workers;
BTW you could use MYSQL workbench if you want to get a good overview from you database layout. Set up a schema inside workbench for reference when coding. Or just use pen and paper. ;-)
With regard to you audit trail, you could give workers the option to delete there activity from the timesheet by adding and extra column (e.g. audittrail) to timesheet. By default the column audittrail is set to 0 and if an worker deletes his entry you give this column the value 1. Then add to you SELECT query "WHERE audittrail = 0" to display all the timesheet entries. And in the case you require an audit trail you leave out the WHERE clause.
http://www.c-sharpcorner.com/uploadfile/nipuntomar/normalization-and-its-types/
What you're trying to do is called database normalization. You want meta data against one particular record. So just 'glancing' at your problem. Make a table for storage, X, and a have a foreign key constraint to the data record you want to describe. In this case, it looks like the users table, and then have fields in this X table which stores the 'multiple data' type which is related to users table.
Tables
users -- table
id
username
password
status
updation_date
creation_date
x -- table
id
users_id -- foreign key constraint
part1_of_multiple_data -- meta log data you are wanting
part2_of_multiple_data -- meta log data you are wanting
part3_of_multiple_data -- add more fields as you need in the table.
updation_date
creation_date
Related
I have a SQL query as follows-
"INSERT INTO users(id, rank) SELECT v.user, v.vote FROM votes v WHERE
v.assertion = '$ID' ON DUPLICATE KEY UPDATE
rank = ( CASE WHEN v.vote = '1' THEN rank+50 WHEN v.vote = '-1'
THEN rank-200 WHEN v.vote = '3' THEN rank+100 ELSE rank END)"
applied on a database with a table users with and id and rank field, and a votes table with a user and vote field. I have to update the rank of the users in the users table based on their vote.
I really like this kind of query, but I've noticed a problem: every time I execute this from my PHP script the query adds a row to the users table completely empty (with only an ID, which is A_I, and a rank of 1, when usually there would be other field as well). I can't really wrap my head around why this happens.
Any help/idea?
Your table does not have a primary key first provide a primary key to id
run this sql query
alter table user add primary key (id)
and than try it will work
There are two possible reasons :
The id column is not the primary key, and probably you table doesn't have a primary key at all.
Create a primary key like this :
alter table user add primary key (id)
If you insert an value of 0 in an auto increment column, a new id is generated. An auto incremented column must not contain the value 0.
There is also a more general problem with your approach : in fact you only insert the user id and the rank, other compulsory fields in the table (username) are missing. The insert part does not seem to be valid for this reason. If you use an insert on duplicate key update, you must make sure that the result is correct which ever of insert and update is executed.
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 have this SQL code which inserts into a database named daily. I regularly update the daily database like this:
INSERT INTO daily
(SELECT * FROM dailytemp
WHERE trim(`Point`) <>''
AND trim(`Point`) IN (Select trim(`Point`) FROM `data`));
I believe I am getting the error because both tables have id primary columns.
I want the data inserted, but a new id generated for any duplicate found.
I tried to list the columns names, but it's about 20 columns, which might make the process cumbersome.
What is my best option?
Edit
The table defination of daily and daily_temp are the same, all the folumns are varchar(100) with and id of (bigint)
id bigint
col1 varchar(100)
col2 varchar(100)
col3 varchar(100)
etc..
A possible solution might be to use a BEFORE INSERT trigger and a separate table for sequencing (if you don't mind).
Sequence table
CREATE TABLE daily_seq(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY);
Now the trigger
DELIMITER $$
CREATE TRIGGER tg_daily_insert
BEFORE INSERT ON daily
FOR EACH ROW
BEGIN
INSERT INTO daily_seq VALUES(NULL);
SET NEW.id = LAST_INSERT_ID();
END$$
DELIMITER ;
Now you can insert whatever you want to daily, id value will replaced with unique auto incremented one.
Here is SQLFiddle demo
Indeed, you are certainly trying to insert a row with an id that already exists in daily.
If this does not pose any referential integrity issue, you could regenerate the id's in dailytemp so that they do not overlap with any one in daily. This might do the trick:
SET #offset = (SELECT MAX(id) FROM daily) - (SELECT MIN(id) FROM dalytemp) +1;
UPDATE dailytemp SET id = id + #offset;
Then try your INSERT.
If rows can be inserted into daily by another process than the one you described, or if for any reason a id is skipped in this table (eg. if the column is AUTO_INCREMENT), then the problem will reoccur. You may want to include this preliminary UPDATE in your daily procedure.
i decided to use a stored proceedure to perform the action, listing all colums name instead of this headache
thanks to all those who helped. Another reason i did that was that i needed to insert a date for the daily table for each set of data inserted, so i created a date column for the dail table only
INSERT INTO daily (col1, col2, etc..., created)
SELECT col1, col2, etc..., 'mydate' from daily_temp WHERE
trim(`Point`) <>'' AND trim(`Point`) IN
(Select trim(`Point`) FROM `data`));
thanks all
I'm fairly new to PHP and MySQL.
I have two tables as follows:
1.`users`: `id`//primary key
: `name`//user's name
2.`events`: `u_id`//index key and foreign field to users' id
: `u_name`
A user will input an id in a form. That id will be searched in the users table and the relevant details will be taken and inserted in the events table.
I've created the foreign fields and and till now I made a function that took id as a variable and returned details from the users tables as variables which I then inserted in the events table. But then, it meant using "a lot" of variables and I thought what was the use of foreign field.
I'm still learning PHP and don't know how to find and insert using FOREIGN fields from one table to another. I just know how to create foreign fields. Please help.
Is this what you're talking about?
This is how foreign key is created.
CREATE TABLE parent (id INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE child (id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
);
Apologize if I didn't understand your question
UPDATED
INSERT table1 (col1, col2, col3)
SELECT col1, col2, col3
FROM table2
WHERE col1 = 'xyz'
Hope this helps
You don't need to store the user name in the events table. The point of the foreign key is that you only need to store the user ID in the events table, because that is a REFERENCE to the user.
To get the user name for an event, say event number 6, you would do
select name from users join events on users.id = events.u_id where events.id = 6
So, you should not be trying to insert user data into the events table. Just put the ID in there, and the user data will be available for you to retrieve using the foreign key.
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()