If I have click data on a server that expects more than one click per second, is it practical to normalize timestamps?
Basically, this:
CREATE TABLE clicks(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
timestamp TIMESTAMP NULL
)
VS
CREATE TABLE clicks(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
timestamp_id INT(6) UNSIGNED NULL
)
CREATE TABLE timestamps(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
timestamp TIMESTAMP NULL
)
No, not at all. Only time you split stuff out into another table is if there is another piece of data associated with it you want to save by itself. Not the case here, and the timestamp is not going to get reused.
Related
I'm creating a mySQL database for a small blog. This blog will have articles of different "types", like "public interest", "DIY", etc.
My question is about how to organize the database structure: should I create a table for the articles, a table for the types, and a third table that connect the two of them? Or should I just create the first two tables and add a field in the articles table that points out to the id number of the types table?
Option 1:
CREATE TABLE articles(
id int unsigned not null auto_increment primary key,
title varchar(300) NULL,
body TEXT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE articleType(
id int unsigned not null auto_increment primary key,
name char(200) NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `articleType` (`name`) VALUES
('public interest'),
('DIY')
CREATE TABLE articlesArticleType (
ID int unsigned not null auto_increment primary key,
typeID int not null,
articleID int not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Option 2:
CREATE TABLE articles(
id int unsigned not null auto_increment primary key,
title varchar(300) NULL,
body TEXT NULL,
articleType int NOT NULL DEFAULT 1
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE articleType(
id int unsigned not null auto_increment primary key,
name char(200) NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `articleType` (`nombre`) VALUES
('public interest'),
('DIY')
In the second case I just need two tables. Which way is more efficient and preserves data integrity?
First and foremost it is important to decide on the cardinality of relationship between the 2 tables - Articles and Types as it will influence the choice of the tables structure. Broadly there are 3 cardinalities possible:
One to One
One to Many
Many to Many
Option 1 will satisfy One to Many and Many to Many cardinalities while Option 2 will satisfy One to One cardinality.
I am looking on internet for 3 hours, but i dont find any solution.
I would like to create an SQL database via script. I storing user weight and height in a table, but i do not know which is the best type for it.
SQL code
CREATE TABLE details (
ID int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id int(11) NOT NULL REFERENCES user(ID),
weight decimal(5,2) UNSIGNED NULL,
height tinyint UNSIGNED NULL
);
I want store height in cm [100 - 220]
and weight in Kg [30.0 - 150.0] example. weight -> ##.#
Edit:
This is MySQL server.
If this it mySQL you can make them both decimals like you did with weight:
CREATE TABLE details (
ID int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id int(11) NOT NULL REFERENCES user(ID),
height FLOAT,
weight FLOAT
);
You store the number in the data base, the fact that it is kg or meters or whatever is something you have to remember, or deal with after you get the data from the database.
If you want a way to remember what unit you are storing into the data base you can do this:
CREATE TABLE details (
ID int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id int(11) NOT NULL REFERENCES user(ID),
height_cm FLOAT,
weight_kg FLOAT
);
I have got only one column for a table when i create two localized tables. Code as bellow.
-- Month
CREATE TABLE `month` (
`id` INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
);
-- Month Localized
CREATE TABLE `month_loc` (
`month_id' INT NOT NULL,
`name` VARCHAR(200) NOT NULL,
`description` VARCHAR(500) NOT NULL,
`lang_id` INT NOT NULL
);
month_loc.month_id is the foreign key.
month table holds only the primary key. Other all fields should be localized. Is this table structure correct ?
Thanks.
If correct implies a certain degree of normalization, and the content of your columns name and description vary per month_id, lang_id (which would be the combined primary key of month_loc), then yes, your design has reached the 3rd grade of normlization.
Suppose i want to create polls and quiz with a question and with several options as answers.
Do i have to create Poll model for this ?
How can i track the percentage result for every option in polls table ?
Should i make another table with polls answer options ?
What would be the table relationship for polls question and polls answer ?
Is this correct way?
create table polls (id integer not null auto_increment primary key, question varchar(300) not null, mark tinyint not null, created datetime, modified datetime);
create table pollsanswers (id integer not null integer auto_increment primary key, poll_id integer not null, answer varchar(500) not null, mark tityint not null, created datetime, modified datetime);
create table quizes (id integer not null auto_increment primary key, question varchar(300) not null, mark tinyint not null, created datetime, modified datetime);
create table quizesanswers (id integer not null integer auto_increment primary key, quiz_id integer not null, answer varchar(500) not null, mark tityint not null, created datetime, modified datetime);
If i make a mysql table polls , then can i access and use that table with posts or other controller or must i create polls_controller.php and poll.php model ?
Can i do this without making new model and controller ?
If it's me, I'd probably have the following tables:
create table polls (
id integer not null auto_increment primary key,
created datetime,
modified datetime
);
create table quizzes (
id integer not null auto_increment primary key,
created datetime,
modified datetime
);
create table questions (
id integer not null auto_increment primary key,
model varchar(255) not null, -- Poll or Quiz, in this scenario
foreign_key integer not null,
question varchar(255) not null,
created datetime,
modified datetime
);
create table answers (
id integer not null auto_increment primary key,
question_id integer not null,
answer varchar(255) not null,
created datetime,
modified datetime
);
My associations would probably be this:
Poll hasMany Question
Quiz hasMany Question
Question belongsTo Poll, Quiz
Question hasOne Answer
Answer belongsTo Question
Since both polls and quizzes have component questions, I'd try to consolidate that aspect. In order to account for both relationships, I'd make Question polymorphic.
I am looking to find the best practice in mapping one base table to multiple tables. For example if I had any one of the following base tables (comments,tags,favorites,ratings), it could map to one or more tables such as (blog post, picture, video). The example below can provide a better explanation.
More info:
I am looking to use these tables to create a Yii application which uses Active Record.
My proposed solution (Mysql):
My Base Table
create table comment (
id int(4) unsigned not null auto_increment primary key,
attach_id int(4) unsigned not null, #used to attach to a specific post/photo/video
attach_type_id tinyint(1) unsigned not null, #foreign key to attach_type(id)
comment text not null,
user_id int(4) unsigned null,
datetime_added datetime not null,
foreign key (attach_type_id) references attach_type(id)
);
My "Global Mapping" Table:
create table attach_type (
id tinyint(1) unsigned not null auto_increment primary key,
table_name varchar(20) not null #used for reference purposes only
);
Primitive Example of two of the "Multiple" Tables:
create table blog_post (
id int(4) unsigned not null auto_increment primary key,
title varchar(100) not null,
post text not null,
user_id int(4) unsigned null,
datetime_added datetime not null
);
create table photo (
id int(4) unsigned not null auto_increment primary key,
title varchar(100) not null,
description varchar(255) null,
file_name varchar(100) not null,
user_id int(4) unsigned null,
datetime_added datetime not null
);
To retrieve all comments for blog post id=54
blog_post table's id for its row in the attach_type table = 1
The post's row id for its row in the blog_post table = 54
select * from comments where attach_type_id=1 and attach_id=54;
So a (comment,tag,favorite,rating), comment seen above, can be attached to both a blog_post and/or a photo. Likewise multiple comments can be attached to a single blog_post / photo (allows for multiple users to comment). My question is what is the best way to go about this in mysql. Does the above look like a proper setup or would you suggest a better way and why. And also if using the above solution, does anyone forsee any glaring cons? Thank you for your response in advance, I'm simply trying to figure out the best way to go about doing this.
I believe this topic is related to what I am asking, but did not really answer my question:
Database tables, one table referencing multiple unrelated tables
Tom H. supplied an answer to another question which I believe answered my question. I do not know how to give him the proper credit but the link to his solution is here:
Database tables, one table referencing multiple unrelated tables
Thank you for your assistance Tom.
I'm still open to suggestions but taking into account the information posted in the link above I think I'm going to go the route of making multiple map tables.