I have a table that contains NULL values. This table is meant only to store numerical values, except the second column which contains a time-stamp for each record. This table has been in use for some time and so has accumulated a lot of NULL values in varying columns. Here's the table's description:
+-----------------------------------------+-----------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------------------+-----------+------+-----+-------------------+----------------+
| results_id | int(11) | NO | PRI | NULL | auto_increment |
| time_stamp | timestamp | NO | | CURRENT_TIMESTAMP | |
| test_col | int(11) | YES | | NULL | |
| test_col-total | int(11) | YES | | NULL | |
| test_col_B | int(11) | YES | | NULL | |
| test_col_B-total | int(11) | YES | | NULL | |
+-----------------------------------------+-----------+------+-----+-------------------+----------------+
12 rows in set (0.01 sec)
I now want to UPDATE/ALTER the table so that:
from now on any NULL value being added to the table is handled and processed as a '0' value instead (really interested to know if this is indeed possible; if it is then I wont need to change a load of INSERT queries in a lot of my Python scripts elsewhere!)
all stored NULL values are updated/changed to '0'.
I am entirely stuck with this because on the one hand I want my SQL query to update a new rule to the table while on the other change current NULL values and as a novice this is a little more intermediate for my current understanding.
So far I have:
ALTER TABLE `results` MODIFY `<col_name>` INT(11) NOT NULL;
And I will do this for each column that currently allows NULL values. However, I do not know how to change stored NULL values to '0'.
Any input appreciated.
to change NULL values to 0
try
UPDATE results SET `col_name` = 0 WHERE `col_name` IS NULL;
to change columns to have NOT NULL and default to 0 try
ALTER TABLE results MODIFY `col_name` INT(11) NOT NULL DEFAULT 0;
you have to do it in the above order, i just tested this on http://sqlfiddle.com/
First change your values to 0 where they are null:
UPDATE results SET col1 = 0 WHERE col1 IS NULL;
...
Then you can add a DEFAULT of 0, that will be added whenever you supply no values to that table on an insert
ALTER TABLE `results` MODIFY `<col_name>` INT(11) NOT NULL DEFAULT 0;
Related
Is there a way to change te value of the Extra column that is shown with the SHOW COLUMNS/DESCRIBE sentences?
The documentation about this column states the following:
Extra
Any additional information that is available about a given column. The
value is nonempty in these cases:
auto_increment for columns that have the AUTO_INCREMENT attribute.
on update CURRENT_TIMESTAMP for TIMESTAMP or DATETIME columns that
have the ON UPDATE CURRENT_TIMESTAMP attribute.
VIRTUAL GENERATED or VIRTUAL STORED for generated columns.
DEFAULT_GENERATED for columns that have an expression default value.
I have the next table columns information but I wish to remove the Extra value of the start_date column.
Is there a way to do this?
+--------------------+--------------------+------+-----+-------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------------+------+-----+-------------------+-------------------+
| id_machine_product | "int(10) unsigned" | NO | PRI | NULL | auto_increment |
| ... | ... | ... | ... | ... | ... |
| start_date | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+--------------------+--------------------+------+-----+-------------------+-------------------+
EDIT:
I have implemented a fingerprint validation method in PHP that diffs the DESCRIBE tables values, I have database versions in production that doesn't have that Extra value even though those columns have an expression default value, so currently, I wish to alter that value so I don't get errors from my implemented fingerprint validation method in my development environment.
The production databases are in Mysql < 8.0 so, as per Bill Karwin's answer, I'm having trouble with my MySQL development environment version that is 8.0
It's not clear from your question why you want to eliminate the Extra information. It's just noting that the column's default is an expression.
To make the Extra field blank, you must make the column's default either a constant value or NULL.
mysql> create table foo ( id int unsigned primary key, start_date timestamp not null default current_timestamp);
mysql> show columns from foo;
+------------+------------------+------+-----+-------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+-------------------+-------------------+
| id | int(10) unsigned | NO | PRI | NULL | |
| start_date | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+------------+------------------+------+-----+-------------------+-------------------+
mysql> alter table foo modify start_date timestamp default null;
mysql> show columns from foo;
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | PRI | NULL | |
| start_date | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+-------+
Note that the Extra information "DEFAULT_GENERATED" is only present in MySQL 8.0. I suspect it's related to the new feature to support expressions in the DEFAULT clause. Any other expression also results in this Extra information.
mysql > alter table foo modify start_date timestamp default (now() + interval 1 hour);
mysql> show columns from foo;
+------------+------------------+------+-----+---------------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------------------------+-------------------+
| id | int(10) unsigned | NO | PRI | NULL | |
| start_date | timestamp | YES | | (now() + interval 1 hour) | DEFAULT_GENERATED |
+------------+------------------+------+-----+---------------------------+-------------------+
Topicstarters comment
I have implemented a fingerprint validation method in PHP that diffs
the DESCRIBE tables values, I have database versions in production
that doesn't have that Extra value even though those columns have an
expression default value, so currently, I wish to alter that value so
I don't get errors from my implemented fingerprint validation method
in my development environment.
The more standard SQL method would be which also works in MySQL 8
Query
SELECT
information_schema.COLUMNS.COLUMN_NAME AS 'Field'
, information_schema.COLUMNS.COLUMN_TYPE AS 'Type'
, information_schema.COLUMNS.IS_NULLABLE AS 'Null'
, information_schema.COLUMNS.COLUMN_KEY AS 'Key'
, information_schema.COLUMNS.COLUMN_DEFAULT AS 'Default'
, information_schema.COLUMNS.EXTRA AS 'Extra'
FROM
information_schema.TABLES
INNER JOIN
information_schema.COLUMNS ON information_schema.TABLES.TABLE_NAME = information_schema.COLUMNS.TABLE_NAME
WHERE
information_schema.TABLES.TABLE_NAME = '<table>'
This query should match the output of DESCRIBE
Then you could use REPLACE() on information_schema.COLUMNS.EXTRA output to remove or edit the way you want. For example removing extra features like DEFAULT_GENERATED or VIRTUAL GENERATED (generated columns)
you need an alter table statement. Something like
ALTER TABLE `document` MODIFY COLUMN `start_date ` INT AUTO_INCREMENT;
You can set a default value like
DEFAULT 1 NOT NULL
I have 2 tables called applications and filters. The structure of the tables are as follows:
mysql> DESCRIBE applications;
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| filter_id | int(3) | NO | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
mysql> DESCRIBE filters;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| label | varchar(255) | NO | | NULL | |
| link | varchar(255) | NO | | NULL | |
| anchor | varchar(100) | NO | | NULL | |
| group_id | tinyint(3) unsigned | NO | MUL | NULL | |
| comment | varchar(255) | NO | | NULL | |
+----------+----------------------+------+-----+---------+----------------+
7 rows in set (0.02 sec)
What I want to do is select all the records in applications and make a corresponding record in filters (so that filters.name is the same as applications.name). When the record is inserted in filters I want to get the primary key (filters.id) of the newly inserted record - which is an auto increment field - and update applications.filter_id with it. I should clarify that applications.filter_id is a field I've created for this purpose and contains no data at the moment.
I am a PHP developer and have written a script which can do this, but want to know if it's possible with a pure MySQL solution. In pseudo-code the way my script works is as follows:
Select all the records in applications
Do a foreach loop on (1)
Insert a record in filters (filters.name == applications.name)
Store the inserted ID (filters.id) to a variable and then update applications.filter_id with the variable's data.
I'm unaware of how to do the looping (2) and storing the auto increment ID (4) in MySQL.
I have read about Get the new record primary key ID from mysql insert query? so am aware of LAST_INSERT_ID() but not sure how to reference this in some kind of "loop" which goes through each of the applications records.
Please can someone advise if this is possible?
I don't think this is possible to do this with only one request to mysql.
But, i think this is a good use case for mysql triggers.
I think you should write it like this :
CREATE TRIGGER after_insert_create_application_filter AFTER INSERT
ON applications FOR EACH ROW
BEGIN
INSERT INTO filters (name) VALUES (NEW.name);
UPDATE applications SET filter_id = LAST_INSERT_ID() WHERE id = NEW.id;
END
This trigger is not tested but you should understand the way to write it.
If you don't know mysql triggers, you can read this part of the documentation.
This isn't an answer to your question, more a comment on your database design.
First of all, if the name field needs to contain the same information, they should be the same type and size (varchar(255))
Overall though, I think the schema you're using for your tables is wrong. Your description says that each record in applications can only hold one filter_id. If that is the case, there's no point in using two separate tables.
If there is a chance that there will be a many-to-one relationship, link the records via the relevant primary key. If multiple records in application can relate to a single filter, store filters.id in the applications table. If there are multiple filters for a single application, store applications.id in the filters table.
If there is a many-to-many relationship, create another table to store it:
CREATE TABLE `application_filters_mappings` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`application_id` int(10) unsigned NOT NULL,
`filters_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
);
I have recently started to work with MySQL for my study job and now face following problem:
My predecessor created a textmining table of the following structure:
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| TokenId | int(11) | NO | PRI | 0 | |
| Value | varchar(255) | YES | | NULL | |
| Frequency | int(11) | YES | | NULL | |
| PMID | int(11) | YES | MUL | NULL | |
+------------+--------------+------+-----+---------+-------+
In the context of restructuring, I added the following column:
+------------+--------------+------+-----+---------+-------+
| NewTokenId | int(11) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
If I now run the query:
insert into TitleToken(NewTokenId) select t.TokenId from Token as t, TitleToken as tt where t.Value = tt.Value
or even the query:
insert into TitleToken(NewTokenId) values(1);
I get following output:
ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
As I said, I am relatively new to (hands-on) *SQL and it feels like a stupid mistake, but since the column NewTokenId is no primary key, not unique and even Null is YES, I thought I'd be able to insert basically anything I want.
Any hint would be appreciated... thanks in advance :)
The problem here is that you have a default value for the primary key "TokenID", if you do not insert a value for the key in your insert statement the system will automatically insert 0. However, if there is another tuple with the same value for this attribute (which is probable because the default is 0) you will get that error.
You are attempting to perform an insert into a table without providing a unique value for TokenId. By default, according to the table description, TokenId defaults to 0, you cannot have multiple identical values in that column.
I have a table of this structure:
mysql> desc securities;
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| sym | varchar(19) | NO | PRI | | |
| bqn | int(11) | YES | | NULL | |
| sqn | int(11) | YES | | NULL | |
| tqn | int(11) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
I am trying to do a select and an update within the same query, so the reason I have chosen
insert into securities (sym, bqn, sqn , tqn) values('ANK', 50,0,1577798)
on duplicate key update bqn=bqn+50 , sqn=sqn+0 , tqn=tqn+1577798;
When I ran the above I observed it is in fact changing the values for all the other rows also.
Is this behaviour expected? I am using MySQL Database.
Your fiddle is missing the key, and the INSERT statement in the right panel (where it does not belong in the first place) is using different column names … *sigh*
Define the symbol column as PRIMARY KEY – and use the VALUES() syntax to get the values to add in the ON UPDATE part, so that you don’t have to repeat them every single time:
insert into securities
(symbol, buyerquan, sellerquan , totaltradedquan)
values('BANKBARODA', 73, 0, 4290270)
on duplicate key update
buyerquan=buyerquan+VALUES(buyerquan),
sellerquan=sellerquan+VALUES(sellerquan),
totaltradedquan=totaltradedquan+VALUES(totaltradedquan);
Works perfectly fine, result values are as to be expect from the input: http://sqlfiddle.com/#!2/21638f/1
I am trying to change an existing column in a table I have to allow for null values and then set the default value to null. I tried running the following but it does not seem to be updating the table:
mysql> ALTER TABLE answers_form MODIFY sub_id int unsigned NULL DEFAULT NULL;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc answers_form;
+--------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| answer_id | int(10) unsigned | NO | PRI | 0 | |
| sub_id | int(10) unsigned | NO | PRI | 0 | |
| form_id | int(10) unsigned | NO | PRI | NULL | |
| value | varchar(255) | NO | | NULL | |
| non_response | bit(1) | YES | | b'0' | |
+--------------+------------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
Can anyone see what I am doing wrong here?
its a primary key , mysql doesn't allow any part of the primary key to be null, which does make the fact that it allows a default value of null for the form_id odd, however the docs at
http://dev.mysql.com/doc/refman/5.5/en/optimizing-primary-keys.html
say "Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values".
Just out of curiosity, does it actually allow you to put in null values in the form_id field?
You have 2 non-nullable columns with the default value of null. This shouldn't be allowed by your database engine. If it is, it is rather far from a best practice.
sub_id is listed as a primary key
From the MySQL docs (5.7, but other versions say the same thing):
A PRIMARY KEY is a unique index where all key columns must be defined
as NOT NULL. If they are not explicitly declared as NOT NULL, MySQL
declares them so implicitly (and silently).
As to the discussion about the Non-null columns having a Default of NULL...
The NULL value in the Default column means that there is no default, not that the default is NULL.
Fiddle: http://sqlfiddle.com/#!2/c718d/1
If I create a simple table like so:
CREATE TABLE name_num(
Number INT PRIMARY KEY,
Name TEXT NOT NULL
);
And then do desc name_num, I get:
| FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA |
---------------------------------------------------
| Number | int(11) | NO | PRI | (null) | |
| Name | text | NO | | (null) | |
Again, from the MySQL docs:
If the column cannot take NULL as the value, MySQL defines the column with no explicit DEFAULT clause. Exception: If the column is defined as part of a PRIMARY KEY but not explicitly as NOT NULL, MySQL creates it as a NOT NULL column (because PRIMARY KEY columns must be NOT NULL), but also assigns it a DEFAULT clause using the implicit default value. To prevent this, include an explicit NOT NULL in the definition of any PRIMARY KEY column.