I use following query to create table news:
CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`news_title` varchar(500) NOT NULL,
`news_detail` varchar(5000) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
mysql> desc news;
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| news_title | varchar(500) | NO | | | |
| news_detail | varchar(5000) | NO | | | |
+-------------+---------------+------+-----+---------+----------------+
mysql> insert into news (news_title, news_detail) values ('test','demod demo');
mysql> select * from news;
+----+--------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------+
| id | news_title | news_detail |
+----+--------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------+
| 3 | Advani wants to shift from Gujarat, BJP trying to convince him otherwise | testt |
| 5 | test | demod demo |
+----+--------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------+
as you see in the select query the id is increment like 1,3,5,7.... means it increment by 2. So what is the problem here?
actually in my local, it is increment by 1 and working perfectly. but in my server it creates the problem.
Thanks in advance.
Why ?
The auto_increment value can be change with the variable auto_increment_increment.Normally, it’s always 1, but for some weird reason it was set to 2 in my case. I think MySQL Workbench may be involed.
You can change it be doing one of those :
SET ##auto_increment_increment=1
SET GLOBAL auto_increment_increment=1;
More information
You can find some information here and here.
Check system variable ##set_auto_increment_increment.
it should be
SET ##auto_increment_increment=1;
Related
I am building a dynamic application which will act based on settings.
The settings are stored in a MySQL table which consists of both App level data and global level data (app_id = 0).
My use case is, I want to select the settings of an App. If it does not exist, fetch the corresponding setting from the global level.
I have achieved this using sub queries and COALESCE function.
Question: Can the data be fetched in a single query? If not, Can the schema be modified to handle this App level and Global level in a much simpler way?
Schema
CREATE TABLE `settings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`partner_id` int(11) NOT NULL,
`app_id` int(11) DEFAULT NULL,
`type` varchar(64) DEFAULT NULL,
`name` varchar(64) DEFAULT NULL,
`value` varchar(300) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `key_partner_id` (`partner_id`),
KEY `key_app_id` (`app_id`),
KEY `key_type` (`type`),
KEY `key_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Data
| id | partner_id | app_id | type | name | value |
|----|------------|--------|-------|---------|--------|
| 1 | 500 | 0 | color | primary | blue |
| 2 | 500 | 100 | color | primary | green |
| 3 | 500 | 101 | color | primary | red |
query
SELECT * FROM settings WHERE app_id in (
COALESCE ((SELECT app_id FROM settings WHERE app_id = 100), 0)
);
| id | partner_id | app_id | type | name | value |
|----|------------|--------|-------|---------|-------|
| 2 | 500 | 100 | color | primary | green |
SELECT * FROM settings WHERE app_id in (
COALESCE ((SELECT app_id FROM settings WHERE app_id = 102), 0)
);
| id | partner_id | app_id | type | name | value |
|----|------------|--------|-------|---------|-------|
| 1 | 500 | 0 | color | primary | blue |
Single query to get the settings for the app or fall back on global settings
SELECT * FROM settings
WHERE app_id IN(0,102)
ORDER BY app_id DESC
LIMIT 1;
Obviously, this assumes a single row for the app settings.
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 this table in mysql called ts1
+----------+-------------+---------------+
| position | email | date_of_birth |
+----------+-------------+---------------+
| 3 | NULL | 1987-09-03 |
| 1 | NULL | 1982-03-26 |
| 2 | Sam#gmail | 1976-10-03 |
| 2 | Sam#gmail | 1976-10-03 |
+----------+-------------+---------------+
I want to drop the equal rows using ALTER IGNORE.
I have tried
ALTER IGNORE TABLE ts1 ADD UNIQUE INDEX inx (position, email, date_of_birth);
and
ALTER IGNORE TABLE ts1 ADD UNIQUE(position, email, date_of_birth);
In both cases I get
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IGNORE TABLE ts1 ADD UNIQUE(position, email, date_of_birth)' at line 1
I'm using mySQL 5.7.9. Any suggestions?
To do it inline against the table, given just the columns you show consider the below. To do it in a new table as suggested by Strawberry, see my pastie link under comments.
create table thing
( position int not null,
email varchar(100) null,
dob date not null
);
insert thing(position,email,dob) values
(3,null,'1987-09-03'),(1,null,'1982-03-26'),
(2,'SamIAm#gmail.com','1976-10-03'),(2,'SamIAm#gmail.com','1976-10-03');
select * from thing;
+----------+------------------+------------+
| position | email | dob |
+----------+------------------+------------+
| 3 | NULL | 1987-09-03 |
| 1 | NULL | 1982-03-26 |
| 2 | SamIAm#gmail.com | 1976-10-03 |
| 2 | SamIAm#gmail.com | 1976-10-03 |
+----------+------------------+------------+
alter table thing add id int auto_increment primary key;
Delete with a join pattern, deleting subsequent dupes (that have a larger id number)
delete thing
from thing
join
( select position,email,dob,min(id) as theMin,count(*) as theCount
from thing
group by position,email,dob
having theCount>1
) xxx -- alias
on thing.position=xxx.position and thing.email=xxx.email and thing.dob=xxx.dob and thing.id>xxx.theMin
-- 1 row affected
select * from thing;
+----------+------------------+------------+----+
| position | email | dob | id |
+----------+------------------+------------+----+
| 3 | NULL | 1987-09-03 | 1 |
| 1 | NULL | 1982-03-26 | 2 |
| 2 | SamIAm#gmail.com | 1976-10-03 | 3 |
+----------+------------------+------------+----+
Add the unique index
CREATE UNIQUE INDEX `thing_my_composite` ON thing (position,email,dob); -- forbid dupes hereafter
View current table schema
show create table thing;
CREATE TABLE `thing` (
`position` int(11) NOT NULL,
`email` varchar(100) DEFAULT NULL,
`dob` date NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE KEY `thing_my_composite` (`position`,`email`,`dob`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
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 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;