Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 26 days ago.
Improve this question
I want to create a table.
I'm having long and hard time about naming columns.
If there are 3 columns having same characteristics.
ex) tag_1, tag_2, tag_3
->
CREATE TABLE BOARD (
BOARD_ID bigint not null
primary key,
TAG_1 varchar(10) null,
TAG_2 varchar(10) null,
TAG_3 varchar(10) null
)
Is it a best practice to naming like examples above?
It should be something like this:
CREATE TABLE BOARDS
(
BOARD_ID BIGINT
,name ...
,description ...
);
CREATE TABLE TAGS
(
TAG_ID INT
,TAG VARCHAR(10)
);
CREATE TABLE BOARDSTAGS
(
BOARD_ID BIGINT
,TAG_ID INT
);
You have one table for describing board properties and one of tag properties. Some of the tags can be shared across many boards. So, we need third table for telling which board, which tags has.
In this case, when you need additional tags, you do not need to add new column in the board table.
Nothing wrong with your notation. However, as your columns look really identical, you might wanna take a look into normalizing them. It might make sense in many cases and it will keep your schema cleaner.
Related
I am creating a sql database with a table holding questionnaire answers. The questions are full sentences (about 150 characters each) and I want to know what is the best method for maintaining that information as the fields. I am till new to SQL, but I see two options:
set each question as a number (1, 2, 3, 4...) and have a separate table holding the actual questions as the data that links to the number in the first table.
some method in CREATE TABLE that lets you set the field as a sentence. I though quotes would work, but they do not.
EDIT:
a quick example of what i am trying to do:
CREATE TABLE survey(
index_id INT PRIMARY KEY,
'between 1 and 10, how do you feel about the transparency of the scientific community?' VARCHAR(5)
);
Thanks!
You are mixing up the data in a table and creating the table.
When you create the table you define the structure of the table
Then you can add data to the table
Then you can query the table.
So for example create a table.
create table questionanswer (
questionnumber integer,
answer varchar(200)
)
add data to the table
insert into questionanswer (questionnumber, answer)
values (1, 'election day')
query the table for values
select answer
from questionanswer
where questionnumber = 1
Generally using VARCHAR(255) with encoding utf8mb4 is a good default. If you need long-form data, like essays, multiple paragraphs, etc. then use TEXT or LONGTEXT.
This is really a one-table problem:
CREATE TABLE questions (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
questionnaire_id INT NOT NULL,
num INT NOT NULL DEFAULT 0,
question VARCHAR(255) NOT NULL
);
Where if you want you can have multiple questionnaires by adding another questionnaire table, or just use that number as-is for partitioning the questions.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I made a table and I want to select certain data from the table using select statements.
I want to see all the book titles that were published in May.
How can I go about doing this?
Here is the table
CREATE TABLE titles
(
title_id CHAR(3) NOT NULL,
title_name VARCHAR(40) NOT NULL,
type VARCHAR(10) ,
pub_id CHAR(3) NOT NULL,
pages INTEGER ,
price DECIMAL(5,2) ,
sales INTEGER ,
pubdate DATE ,
contract SMALLINT NOT NULL,
CONSTRAINT pk_titles PRIMARY KEY (title_id)
)ENGINE = InnoDB;
I tried this:
SELECT * FROM titles
WHERE pubdate LIKE '%%%%-05-%%';
It displays the whole column, how can I get it to only display the books title?
Try this:
SELECT title_name FROM titles WHERE MONTH(pubdate) = '5'
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm working on a database for my database class.
I have been trying to change the INT from SUBJECT_ID to VARCHAR.
This is the code I have been using but it keeps giving me errors:
ALTER TABLE COURSE
ALTER COLUMN SUBJECT_ID VARCHAR(11);
I also tried this:
SELECT CONVERT(VARCHAR(11),SUBJECT_ID) FROM COURSE;
and this is the table I'm working with:
CREATE TABLE COURSE
(COURSE_ID INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
SUBJECT_ID INT(11) NOT NULL,
COURSE_GRADE_LEVEL CHAR(2) NOT NULL,
FAC_ID INT NOT NULL,
FOREIGN KEY(FAC_ID) REFERENCES FACULTY(FAC_ID));
Any suggestions will be appreciated. Thanks.
Try:
ALTER TABLE COURSE MODIFY SUBJECT_ID VARCHAR(11);
select cast(subject_id as varchar(11)) as subject_id_str from course
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am creating a job seek website with PHP and MySQL, one question is how to design the database better? The basic function of the website is that 'jobseekers' can login and search jobs, upload CVs, and 'employers' can login and post jobs, and browser CVs. Currently I just created 2 tables:
-- Table structure for table users
to analyze the table sturctures for a job seek website(using MySQL and PHP) [on hold]
-- user_type: 0 - jobseekers
-- 1 - employers
-- 2 - administrator
CREATE TABLE users (
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
email VARCHAR(80) NOT NULL,
pass CHAR(40) NOT NULL,
user_type TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
active CHAR(32),
last_login DATETIME NOT NULL,
registration_date DATETIME NOT NULL,
PRIMARY KEY (user_id),
UNIQUE KEY (email),
INDEX login (email, pass)
) ENGINE = INNODB;
-- Table structure for table jobs
CREATE TABLE jobs (
job_id INT(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(200) NOT NULL,
description text NOT NULL,
county VARCHAR(30) NOT NULL,
PRIMARY KEY (job_id)
) ENGINE = MYISAM ;
But I feel just two tables might not be enough, and the user table maybe need to be broke down. Any suggestion how to improve the design?
Perhaps you may want 4 tables;
2 to store the user details
jobseeker_details_table; ( name, address, phonenumber, CV etc)
employer_details_tabel; (name, address, companyname, phone, etc)
than 2 more tables, one for the jobs jobseekers have applied for, and one for the jobs employers have posted - as employers are likely to post more than one job position, and jobseekers and likely to apply for many different jobs, so;
jobs_applied_table; (id, jobid, status, etc)
employer_posted_jobs; (jobid, position_details, date_posted, status, etc)
hope that helps / gives you something to think about.
good luck
I'm working on a quiz project and I want create a mysql structure in such a way that:
questionID: A unique question identification number(primary key)
testID: A unique test identification number(question belongs to this test)(primary key)
questionOrder: The order of the question within the quiz questions, ie this question is n-th question in the quiz. I want this value to come from mysql, so that when I insert a new question to db, I don't have to calculate it
One question can be in multiple different tests.
I have couple of questions:
1) I have the following code but I get:
Incorrect table definition; there can be only one auto column and it must be defined as a key
How can I fix this?
2) This structure doesn't allow a question to belong to multiple quizzes. Any idea to avoid this?
3) Do you think this structure is good/optimum, can you suggest anything better?
CREATE TABLE `quiz_question` (
`questionID` int(11) NOT NULL auto_increment,
`quizID` int(11) NOT NULL default '0',
`questionOrder` int(11) NOT NULL AUTO_INCREMENT,
`question` varchar(256) NOT NULL default '',
`answer` varchar(256) NOT NULL default '',
PRIMARY KEY (`questionID`),
UNIQUE KEY (`quizID`, `questionOrder`),
KEY `par_ind` (`quizID`, `questionOrder`)
) ENGINE=MyISAM;
ALTER TABLE `quiz_question`
ADD CONSTRAINT `0_133` FOREIGN KEY (`quizID`) REFERENCES `quiz_quiz` (`quizID`);
CREATE TABLE `quiz_quiz` (
`quizID` int(11) NOT NULL auto_increment,
`topic` varchar(100) NOT NULL default '',
`information` varchar(100) NOT NULL default '',
PRIMARY KEY (`quizID`)
) ENGINE=MyISAM;
Thanks for reading this.
1) You can only have one AUTO_INCREMENT column per table. It should be a key. Generally, it's part of / is the PK.
2) A 'quiz' would be an entity composed of questions. You should have 3 tables:
1 - quiz_question: quest_id, question, answer
2 - quiz_quiz: quiz_id, topic, info
3 - quiz_fact: quiz_id, quest_id, quest_order
The quiz and question tables hold the per-item (quiz/question) information. The quiz_fact defines how a quiz is composed (this quiz has this question in this order).
3) My only suggestion would be to use Drizzle instead ; ) Seriously though, play with things - 'good enough' often is. If it suits your needs, why tinker? Otherwise you can ask more detailed questions once you have this up and runnning (ie my queries are too slow on such and such operations).
1) Do the order increment yourself. The DB will only do it if it's part of a PK. You might be able to hack it by making a composite key containing the order column but it's not worth it.
2) Rename quiz_question to question (and quiz_quiz to quiz). Make a new quiz-question join table called quiz_question. It should have a quiz ID and a question ID, linking a quiz to a question. As the same question will have different orders on different quizes, put the question order on the new quiz_question. You no longer need a quiz ID on the question table.
Remove AUTO_INCREMENT from the questionOrder field.
As far as having MySQL set the value in the questionOrder field, then do that in a subsequent UPDATE query. Usually, you'd want the administrator of the test, using your admin utility, to be able to adjust the ordering of questions. In that case, you just enter an initial value +1 higher than the highest previous ordering value (on that test). Then, you can let them adjust it something like the manner of adjusting a Netflix queue :)