Changing int 10 to int 11 using DDL [duplicate] - mysql

This question already has answers here:
How can I modify the size of column in a MySQL table?
(3 answers)
Closed 3 years ago.
What would be the DDL syntax for changing a MySQL column name from int(10) to int(11)?
I've done a number of searches using DDL, int(10) and int(11) like this but I've not found any syntax examples.
The current migration is failing because an int(11) is being used as a foreign key referencing an int(10)
I realise I can do this in a GUI. I'm looking to push this change into a migration script for Django.
This was marked as a duplicate because another answer generally solved the issue.
I was searching specifically for changing int(10) to int(11). More to the point ... the question marked as a possible duplicate did not come up with the searches I was making.

A simple ALTER TABLE:
ALTER TABLE yourtable MODIFY COLUMN yourcolumn INT(11)
Demo on dbfiddle

Related

problems with the check in a sql table [duplicate]

This question already has answers here:
CHECK constraint in MySQL is not working
(8 answers)
Closed 2 years ago.
i've created this table in SQL
CREATE TABLE product (
code CHAR(7) NOT NULL,
name VARCHAR(30) NOT NULL,
Description VARCHAR(500) NOT NULL,
cost DOUBLE UNSIGNED NOT NULL,
PRIMARY KEY (code),
check(substring(code,1,3) like '%[a-z]%'
and substring(code,4,4) like '%[0-9]%'),
);
the value 'code' must consist of 3 characters and 4 numbers, but it doesn't work. what's wrong in the check?
the value 'code' must consist of 3 characters and 4 numbers, but it doesn't work. what's wrong in the check?
Use regular expressions:
check (code regexp '^[A-Z]{3}[0-9]{4}$')
MySQL does not extend the definition of LIKE to include character classes. It has real regular expression support.

InnoDB vs MyISAM in a table storing usernames and passwords [duplicate]

This question already has answers here:
MyISAM versus InnoDB [closed]
(25 answers)
Closed 2 years ago.
I had a table usernames that store user's username and password.
Structure of table usernames is given by:
CREATE TABLE `usernames`
(
`id` INT NOT NULL AUTO_INCREMENT ,
`user_id` INT NOT NULL ,
`username` VARCHAR(50) NOT NULL ,
`password` VARCHAR(100) NOT NULL ,
`time` TIMESTAMP NOT NULL ,
PRIMARY KEY (`id`),
UNIQUE `user_id_index` (`user_id`) /* it's primary key in users table(user infos)*/
UNIQUE `username_index` (`username`)
) ENGINE = InnoDB;
I'm expecting more than 100,000 rows in the table. And there are only two types of queries which will be made on this table, and example of these queries are:
SELECT * FROM usernames WHERE username = 'brad'
UPDATE usernames SET username = 'newbrad' WHERE user_id = '312'
Right now I'm using InnoDB engine, I want to know whether it's better than MyISAM in this particular case or not.
Thank you in advance!
I very much doubt you'd see any difference whatsoever with such a small database. Feel free to keep researching but probably don't worry about it, there's no wrong choice really. InnoDB is a more rigorous engine, and better for many (most?) applications, so if in doubt, pick that.
MyISAM has been worse even at read-only performance since at lest MySQL 5.1, and it has always been worse at write performance.
MyISAM has been completely deprecated in the latest version of MySQL, even the system tables are now InnoDB.

Why does this script fail because it doesn't like the column name? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 5 years ago.
It doesn't like the word release as a column name.
CREATE TABLE external_db (
external_db_id INT not null,
db_name VARCHAR(100) NOT NULL,
release VARCHAR(40) NOT NULL,
status ENUM ('KNOWNXREF','KNOWN','XREF','PRED','ORTH', 'PSEUDO') not null,
PRIMARY KEY( external_db_id )
);
I changed the field name to releaseX and the error went away.
This script came from https://github.com/Ensembl/ensembl/blob/release/91/sql/table.sql, which is supposed to be mySQL. Is this a versioning issue in MySQL or can I decorate the word release in the script so it can be used as a column name?
I am using MySQL 5.7 and MySQLWorkbench 6.3.
I know release is a bad name for a column but I didn't write the script.
Release is a reserved keyword
To use it as a column name, you can escape it with backticks like this:
`release`

MySQL table constraint with 2 values

In my MySQL DB I have a table that stores information about contractor's activities:
`task_id` int(11) DEFAULT '0',
`task_date` date DEFAULT NULL,
`contractor_id` int(11) DEFAULT '0'
Business logic demands that task_id's 5 and 6 are mutually exclusive, and for any given date any given contractor can have only 1 of them.
Is it possible to impose some constraint on that table that would enforce this logic?
I don't think adding constraint will solve the problem. You can try adding a trigger before insert on table and check if for that contractor_id you trying to insert, if its new.task_id is equal to 5 either 6 you should get this value and select on the table if there is any registry that goes against your business metrics and avoid it to be added before the insert finishes. I think that this simple explanation will help you go further this.

MySQL db structure help

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 :)