MariaDB / MySQL adds DEFAULT NULL to column definition, how to change that? - mysql

When I create table with this statement:
CREATE TABLE `change_log` (
`object_id` int(11)
)
MariaDB creates table change_log which has DEFAULT NULL in it's table definition for object_id column. That is, for the statement:
SHOW CREATE TABLE change_log
it returns:
CREATE TABLE `change_log` (
`object_id` INT(11) NULL DEFAULT NULL
)
How can I configure mariadb/mysql not to add DEFAULT NULL in this case?
I use MariaDB 10.2.14 on Windows 10

You only must add DEFAULT NULL like this:
CREATE TABLE `change_log` (
`object_id` int(11) DEFAULT NULL
) ENGINE=InnoDB;

SELECT ##sql_mode;
Do you see STRICT_ALL_TABLES or STRICT_TRANS_MODE?
What happens when you include (or exclude) those from ##sql_mode before the CREATE TABLE? Before the INSERT?

Related

problem with mysql update - its change all rows

I have mysql history table that set like this:
CREATE TABLE `history` (
`id` int(11) DEFAULT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_id` int(11) DEFAULT '0'
)
I ran this line:
ALTER TABLE history ADD COLUMN removed int(11) DEFAULT '0';
but when i run this:
update history set removed=1 where user_id=1599;
I get all rows changed with random values and and date reset to now!
I think i'v error in table but I dont now what. the set seem fine....
Since the user_id is Default to '0'
and here you're overwriting the values in user_id, it's messing up the table
So just do
Create table history ( user_id int(11));
remove " Default '0' ".

mysql drop statement with --

What is the difference between drop table and --drop table in mysql
For example: I'm getting error if I use -- but in all other places of Magento they are using -- before drop.
--DROP TABLE IF EXISTS {$this->getTable('faq/dinkchika')};
CREATE TABLE IF NOT EXISTS {$this->getTable('faq/dinkchika')} (
`faq_id` int(11) NOT NULL AUTO_INCREMENT,
`faq_question` varchar(255) DEFAULT NULL,
`faq_answer` varchar(255) DEFAULT NULL,
PRIMARY KEY (`faq_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
A line that starts with -- and has a space after that, is treated as a comment until the end of line. It won't be executed.
You can read more about Mysql comment syntax here: http://dev.mysql.com/doc/refman/5.1/en/comments.html
use a white space after -- ,if you are not using whitespace after -- then it will not
count as comment.after whitespace your query will look like this.
-- DROP TABLE IF EXISTS {$this->getTable('faq/dinkchika')};
CREATE TABLE IF NOT EXISTS {$this->getTable('faq/dinkchika')} (
`faq_id` int(11) NOT NULL AUTO_INCREMENT,
`faq_question` varchar(255) DEFAULT NULL,
`faq_answer` varchar(255) DEFAULT NULL,
PRIMARY KEY (`faq_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
Or you may use #(Hash) as well and
Try this: Drop table IF EXISTS table_name;
And then continue with creating the table, as it will be guaranteed to no longer exist.
I hope it will help for you..

Alter SQL table - allow NULL column value

Initially, the table "MyTable" has been defined in the following way:
CREATE TABLE IF NOT EXISTS `MyTable` (
`Col1` smallint(6) NOT NULL AUTO_INCREMENT,
`Col2` smallint(6) DEFAULT NULL,
`Col3` varchar(20) NOT NULL,
);
How to update it in such a way that the column "Col 3" would be allowed to be NULL?
The following MySQL statement should modify your column to accept NULLs.
ALTER TABLE `MyTable`
ALTER COLUMN `Col3` varchar(20) DEFAULT NULL
ALTER TABLE MyTable MODIFY Col3 varchar(20) NULL;
This works in PSQL, not sure if it also works in normal SQL.
ALTER TABLE tablename
ALTER COLUMN columnname DROP NOT NULL;
ALTER TABLE school MODIFY COLUMN school_van varchar(36) DEFAULT NULL;
"ALTER" keyword didn't work but "MODIFY" worked fine in MySQL 8.0.26

Creating tables in MySQL

I created a table license_serial in my "database_name". But when I tried creating a second table named license, it says that it already exists.
Why is that ? My database contains only a license_serial.frm and a db.opt.
mysql> SHOW TABLES;
+---------------------+
| Tables_in_mobilemp3 |
+---------------------+
| license_serial |
+---------------------+
1 row in set (0.00 sec)
mysql> select * from license;
ERROR 1146 (42S02): Table 'mobilemp3.license' doesn't exist
Creating the second table:
CREATE TABLE `license` (
`id` int(10) unsigned NOT NULL auto_increment,
`serial_id` int(10) unsigned NOT NULL,
`uid` bigint(20) unsigned NOT NULL,
`active` tinyint(1) NOT NULL,
`first_seen` timestamp NOT NULL default CURRENT_TIMESTAMP,
`last_seen` timestamp NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `serial_uid` (`serial_id`,`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
gives the following error message:
ERROR 1050 (42S01): Table 'mobilemp3.license' already exists
EDIT:
And the solution is this(in this order):
drop database databasename;
create database databasename;
use databasename;
The only thing I can think of, is that the table was created e.g. by root and the user you are using does not have access to that table. In that case you cannot select from it (not sure if it would be filtered out in the show tables command though)
Log in as root to that database and check if that table exists.

MySQL problem: autoID not starting with 1

I'm creating a new table from scratch through scripting - first, I'm dropping it (in case it already exists):
DROP TABLE IF EXISTS `myTable`
then I'm creating it:
CREATE TABLE IF NOT EXISTS `myTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
... and so on
The problem: for some strange reason, my autoID-field ALWAYS starts at 2028 instead of 1, although I'm generating it from scratch. What is wrong?
Look at the end of create block. You probably have something like AUTO_INCREMENT=2028. If this is the case just put AUTO_INCREMENT=1 at the end of create table block
such as
CREATE TABLE IF NOT EXISTS `myTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...
) ENGINE=xxx AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `myTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id));
Please add primary key
You can update it by altering the table
ALTER TABLE <tablename> AUTO_INCREMENT = 1;