Relative MySQL Database - php

I have a MySQL database table which is built as follows.
The numbers below the classes denote a "level" for the class, such as high, medium, or low, with 0 being does not attend that class.
I need to build another table to save homework assignments for each class, but I'm a bit lost as to how I'd build that table, specifically denoting what the exact columns of the rows have to be, since each class does not have something specific that denotes it.

A properly normalized structure would have a separate table for courses, giving each an id, and another table placing students into courses by including a student id, course id, and level.
CREATE TABLE students (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(64) NOT NULL,
password VARCHAR(64),
email VARCHAR(...)
UNIQUE KEY (username)
);
CREATE TABLE courses (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(64) NOT NULL
);
/* If a record doesn't exist for a course and student,
the student isn't enrolled in that course. Otherwise,
the level is defined here */
CREATE TABLE enrollments (
id INT NOT NULL PRIMARY KEY,
student_id INT NOT NULL,
course_id INT NOT NULL,
level INT NOT NULL,
/* Each student may be enrolled only once per course */
UNIQUE KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students (id),
FOREIGN KEY (course_id) REFERENCES courses (id)
);
Finally then, you can create a table for assignments assigned in each course:
CREATE TABLE assignments (
id INT NOT NULL PRIMARY KEY,
course_id INT NOT NULL,
description TEXT
/*... other columns related to the assignment as necessary*/
);
And for students to complete assignments if necessary:
CREATE TABLE student_assignments (
student_id INT NOT NULL,
assignment_id INT NOT NULL,
assignment_body TEXT, /* or whatever... */
/* Or to track when completed */
submitted_timestamp TIMESTAMP,
PRIMARY KEY (student_id, assignment_id),
FOREIGN KEY (assignment_id) REFERENCES assignments (id),
FOREIGN KEY (student_id) REFERENCES students (id)
);

Related

How to store list of strings in php/sql table

say I have object user that has a name and a list of emails of limitless lenght. Right now I have 3 tables in my database.
CREATE TABLE NAME (
name TINYTEXT primary key not null
)
CREATE TABLE EMAILS (
id int primary key not null,
email varchar(30)
)
CREATE TABLE USERS (
users_name int references NAME(ID),
users_email int references EMAILS(id),
constraint pk_USERS primary key (users_name, users_email)
)
I feel that there is some error in the making of my tables.
And here is how I am trying to insert the name and email into my database.
protected function setUser($name,$email){ $stmt = $this->estabConnect()->prepare("INSERT INTO USERS(users_email, users_name) VALUES (?,?)");
if(!$stmt->execute(array($email, $name))){ $stmt= null;
header('location: ../index.php?error=stmtfailed');
exit();
}
$stmt=null;
The SQL statement fails
I think the problem is with the sql statment
you must ask your self can users share an email address
yes your design is sorrect as you have a n:m relationship ehivh i represente dby the bridge table
no, your design is wrong and you need to show that a user can have any email adresses
The design would wlook like
CREATE TABLE USER(
id BIGint primary key not null,
name TINYTEXT
)
CREATE TABLE EMAILS (
user_idBIGint primary key not null,
email varchar(30)
)
But normally you want only one email adress per users or maybe a second for office, so that you have a 1:! relationship which is only a part of the user table
a more extended version has not an email table but instead a table that is linked to the user(id) and where you can have many adresses with phone and location in short a 1:n relationship
For your question
CREATE TABLE NAME (
ID int primary key not null,
name TINYTEXT not null
);
CREATE TABLE EMAILS (
id int primary key not null,
email varchar(30)
);
CREATE TABLE USERS (
users_name int references NAME(ID),
users_email int references EMAILS(id),
constraint pk_USERS primary key (users_name, users_email)
);
INSERT INTO USERS(users_name,users_email) VALUES (1,1)
SELECT * FROM USERS
users_name | users_email
---------: | ----------:
1 | 1
db<>fiddle here
To get that to work i changed your design, to corresond with your brisge table.
so as long as you don't enter the actual anme and email adress every should work

SQL: Getting values which all other values related to each must be in another table

I'm working in a web page with PHP and MySQL where I have this DB:
CREATE TABLE action (
idaction INT NOT NULL AUTO_INCREMENT,
--Other columns
PRIMARY KEY (`idaction`));
CREATE event (
idevent INT NOT NULL AUTO_INCREMENT,
--Other columns
PRIMARY KEY (`idevent`));
CREATE TABLE currentGame (
user_email VARCHAR(45) NOT NULL,
--Other columns
PRIMARY KEY (`user_email`));
CREATE TABLE IF NOT EXISTS currentEvents (
currentGame_user_email VARCHAR(45) NOT NULL,
event_idevent INT NOT NULL,
PRIMARY KEY (currentGame_user_email, event_idevent),
FOREIGN KEY (currentGame_user_email) REFERENCES currentGame (user_email),
FOREIGN KEY (event_idevent) REFERENCES event (idevent));
CREATE TABLE IF NOT EXISTS spawnConditions (
action_idaction INT NOT NULL,
event_idevent INT NOT NULL,
PRIMARY KEY (action_idaction, event_idevent),
FOREIGN KEY (action_idaction) REFERENCES action (idaction),
FOREIGN KEY (event_idevent) REFERENCES event (idevent));
So I need to do a query which has the actions which fulfill any of these conditions:
It is not in spawnConditions.
If it is in spawnConditions, all the events which it is related to in this table, must be in the subgroup of currentEvent with a certain known user_email.
In other words, for action A1, being in spawnConditions with events E1 and E2, to be able to be selected from table action, both E1 and E2 must be in currentEvents with the known currentGame_user_email.
Can it be written using only SQL or do I need to involve PHP?
I think that not exists can get the work done here. Basically, you want to filter out actions for which a corresponding record exists in currentEvents with a currentGame_user_email other than a fixed value:
select a.*
from action a
where not exists (
select 1
from spawnConditions sc
inner join currentEvents ce
on ce.event_idevent = sc.event_idevent
where
sc.action_idaction = a.idaction
and ce.currentGame_user_email <> ?
)
The question mark represents the "certain known user_email" you mentioned in the question.

I want to add multiple data in mysql table with php

I have 1 user and I want to insert more than one language(and degree) to him. How can I do this with php?
My languagetype table(all languages inserted)
CREATE TABLE TLANGUAGETYPE (
ID INT NOT NULL AUTO_INCREMENT,
language VARCHAR(100) NOT NULL,
PRIMARY KEY (ID)
)
My language table connected with user table.
CREATE TABLE TLANGUAGE (
languageID INT NOT NULL AUTO_INCREMENT,
userID INT NOT NULL,
ID INT NOT NULL,
degree FLOAT,
PRIMARY KEY (languageID),
FOREIGN KEY (userID) REFERENCES TUSER(userID),
FOREIGN KEY (ID) REFERENCES TLANGUAGETYPE(ID)
)
Instead of adding the languageId in user Table. You can create a mapping table which just contains the "USERID" and "LanguageID" columns.

Assign a unique ID to each name in a table where names are repeated

I have a table that contains millions of sales records and looks like this:
CREATE TABLE `sales` (
`dollar_amount` INT NULL,
`transaction_date` DATE NULL,
`company_name` VARCHAR(45) NULL,
`company_id` INT NULL);
The first three columns are populated with data. I would like to insert data into the company_id column that will identify each company with an auto_incremented integer. I plan to use the company_id field as a foreign key referencing another table that will contain each company's details. Many companies have multiple transactions, so the code needs to assign the same company_id to each row in the sales table with a matching company_name.
Is there a way to do this using only MySQL?
First, I'd recommend creating the company table:
CREATE TABLE company (
company_id INT NOT NULL AUTO_INCREMENT,
company_name VARCHAR(45),
PRIMARY KEY(company_id));
Then insert the companies from your sales data:
INSERT INTO company (company_name)
SELECT distinct company_name
FROM sales;
Finally, update your sales table with a join to get the company_id:
UPDATE sales s
JOIN company c ON s.company_name = c.company_name
SET s.company_id = c.company_id;
SQL Fiddle Demo
You should also remove the company_name field from the sales table since this is now stored in the company table.
To define an auto incremented integer, you just use the AUTO_INCREMENT keyword. However, if you define any columns as auto_increment, you must also make that column your primary key. Which, in this case, would make sense in order for it to be a foreign key elsewhere.
Try this:
CREATE TABLE `sales` (
`dollar_amount` INT NULL,
`transaction_date` DATE NULL,
`company_name` VARCHAR(45) NULL,
`company_id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(company_id);
SQL Fiddle

Mysql error with converting from myISAM to innodb

hey guys, i'm getting this error.
Error 1452 : Cannot add or update a child row: a foreign key constraint fails (`s2794971db/ProfileInterests`, CONSTRAINT `ProfileInterests_ibfk_2` FOREIGN KEY (`InterestID`) REFERENCES `Interests` (`ID`))
I change my tables from myISAM to innodb...found out I needed to so that delete was easier.
I had issues with it so I deleted the table which I needed to create the relationships with.
Then I made it again
I originally had
create table if not exists Users (
ID int not null auto_increment primary key,
FirstName varchar(40) not null,
LastName varchar(40) not null,
UserName varchar(40) not null,
UserEmail varchar(40) not null,
UserDOB timestamp not null,
UserJoin datetime not null
);
create table if not exists Interests(
ID int not null auto_increment primary key,
Interests varchar(40) not null
);
create table if not exists ProfileInterests (
userID int not null References Users(ID),
InterestID int not null References Interests(ID),
MiddleID int not null auto_increment primary key
);
but then I deleted the last table and made it
create table if not exists ProfileInterests (
userID int not null,
InterestID int not null,
MiddleID int not null auto_increment primary key
);
and then I made userID and InterestID into index's and then I added a relation User-> ID for userID and interests->ID for interestID
the error occurs when i'm trying to input data into via a php form.
Any ideas...really need some help!
Obviously, you're trying to insert a record into ProfileInterests for which there is no matching record in the Interests table. Look at the exact insert query, and check that you're supplying a valid value for every field in the table which is part of a foreign key relationship.

Categories