MySQL: Strange AUTO_INCREMENT - mysql

Table looks like:
mysql> DESC text;
+-----------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| text | varchar(255) | YES | | NULL | |
+-----------------+--------------+------+-----+-------------------+----------------+
2 rows in set (0.00 sec)
and AUTO_INCREMENT is 1:
mysql> ALTER TABLE text AUTO_INCREMENT = 1;
Query OK, 1 row affected (0.36 sec)
Records: 1 Duplicates: 0 Warnings: 0
but I get strange id like:
mysql> SELECT id FROM text;
+------------+
| id |
+------------+
| 2147483647 |
+------------+
1 row in set (0.00 sec)
What is the problem?

When you change the auto increment it is set to greatest(your_value,max(column)+ 1)
though I cant find the part in the docs which mention it, it is in the comments
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
ALTER TABLE text AUTO_INCREMENT = 1;
then check the result of
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'text';
to confirm that its not actually 1

Related

How do I alter table VARCHAR mysql Ruby on Rails?

I used the code below to change VARCHAR from (20) to (40) but no change happened in my table:
mysql> ALTER TABLE create_user modify email VARCHAR(40);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
But i use your code can accomplish.
mysql> desc create_user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| email | varchar(40) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> Alter table create_user modify email varchar(20);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc create_user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| email | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)
As per my comment you should run commit but that also means your auto commit settings are different from the default. you may want to look into that further to prevent future headaches
https://dev.mysql.com/doc/refman/5.7/en/commit.html

Is it possible to have an empty MySQL numeric field?

I want to have an empty field on a MySQL Numeric field. If I define the field to allow a NULL value it defaults to 0.00. Sometimes for this row item I prefer no value. I could probably create a different table to track these few items, but at this point I prefer a one table solution.
Because you did this with DEFAULT. Don't do that:
create table t1
( id int auto_increment primary key,
thing varchar(100) not null,
anInt NUMERIC(5,2) NULL DEFAULT 0
);
insert t1(thing) values ('fish');
select * from t1;
+----+-------+-------+
| id | thing | anInt |
+----+-------+-------+
| 1 | fish | 0.00 |
+----+-------+-------+
Works for me on mysql 5.7.12:
mysql> create table X (a double null);
Query OK, 0 rows affected (0.01 sec)
mysql> desc X;
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| a | double | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> alter table X add column b int;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc X;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| a | double | YES | | NULL | |
| b | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into X (a) values (1.3);
Query OK, 1 row affected (0.00 sec)
mysql> insert into X (b) values (1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from X;
+------+------+
| a | b |
+------+------+
| 1.3 | NULL |
| NULL | 1 |
+------+------+
2 rows in set (0.00 sec)

How do I avoid same id being used in a many-2-many relationship

foo_bars is a many-2-many table with both columns pointing to foo.id
I want foo_bars.[id1, id] to for a unique key
How do I avoid same id being used in a foo_bars entry.
i.e. insert into foo_bars (2,2) - How do I avoid this?
mysql> create table foo (id int(11), name varchar(255));
Query OK, 0 rows affected (0.49 sec)
mysql> desc foo;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> create table foo_bars (id1 int(11), id int(11));
Query OK, 0 rows affected (0.34 sec)
mysql> desc foo_bars;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id1 | int(11) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
you can add a unique contatraint
CHECK (id1<>id)
create table foo_bars (id1 int(11), id int(11),CHECK (id1<>id));
You can create a trigger, as follows:
DELIMITER $$
CREATE TRIGGER `test_id_uniqueness` BEFORE INSERT ON `foo_bars`
FOR EACH ROW
BEGIN
IF NEW.id = NEW.id1 THEN
SIGNAL SQLSTATE '12345';
SET MESSAGE_TEXT := 'foo_bars.ID and foo_bars.ID1 cannot be the same';
END IF;
END$$
DELIMETER ;

MySQL Insert from 2 source tables to one destination table

I am having issues inserting Id fields from two tables into a single record in a third table.
mysql> describe ing_titles;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| ID_Title | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(64) | NO | UNI | NULL | |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.22 sec)
mysql> describe ing_categories;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| ID_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| category | varchar(64) | NO | UNI | NULL | |
+-------------+------------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
mysql> describe ing_title_categories;
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| ID_Title_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ID_Title | int(10) unsigned | NO | MUL | NULL | |
| ID_Category | int(10) unsigned | NO | MUL | NULL | |
+-------------------+------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)
Let's say the data from the tables is:
mysql> select * from ing_titles;
+----------+-------------------+
| ID_Title | title |
+----------+-------------------+
| 3 | Chicken |
| 2 | corn |
| 1 | Fettucini Alfredo |
+----------+-------------------+
3 rows in set (0.00 sec)
mysql> select * from ing_categories;
+-------------+----------+
| ID_Category | category |
+-------------+----------+
| 1 | Dinner |
| 3 | Meat |
| 2 | Veggie |
+-------------+----------+
3 rows in set (0.00 sec)
I want to insert into ing_title_categories the record "corn, Veggie" or where ID_Title = 2 and ID_Category = 2.
Here's what I tried:
INSERT INTO ing_title_categories (ID_Title, ID_Category)
SELECT ing_titles.ID_Title, ing_categories.ID_Category
FROM ing_title_categories
LEFT JOIN ing_titles ON ing_title_categories.ID_Title=ing_titles.ID_Title
LEFT JOIN ing_categories ON ing_title_categories.ID_Category=ing_categories.ID_Category
WHERE (ing_titles.ID_Title=2) AND (ing_categories.ID_Category = 2);
There is no data inserted into the table ing_title_categories, and here is the reply from MySQL:
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
What is the correct syntax for inserting the ing_titles.ID_Title and ing_categories.ID_Category into the table ing_titles_categories?
Please, no PHP or Python examples. Use SQL that I can copy and paste into the MySQL prompt. I will be adding this to a C++ program, not PHP, JavaScript or Python.
Edit 1:
The ing_title_categories.ID_Title and ing_title_categories.ID_Category are foreign keys into the other tables.
INSERT INTO
ing_title_categories (ID_Title, ID_Category)
SELECT
ing_titles.ID_Title, ing_categories.ID_Category
FROM
ing_titles, ing_categories
WHERE
ing_titles.ID_Title = ing_categories.ID_Category AND
ing_titles.ID_Title = 2 AND ing_categories.ID_Category = 2;
SQL Fiddle demo
After taking advice from #DrewPierce and #KaiserM11, here is the MySQL sequence:
mysql> INSERT INTO ing_title_categories (ID_Title, ID_Category)
-> SELECT
-> ing_titles.ID_Title,
-> ing_categories.ID_Category
-> FROM ing_titles, ing_categories
-> where (ing_titles.ID_Title = 2) AND (ing_categories.ID_Category = 2)
-> ;
Query OK, 1 row affected (0.07 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from ing_title_categories;
+-------------------+----------+-------------+
| ID_Title_Category | ID_Title | ID_Category |
+-------------------+----------+-------------+
| 17 | 2 | 2 |
+-------------------+----------+-------------+
1 row in set (0.00 sec)
In this case, only possible way I see is using a UNION query like
INSERT INTO ing_title_categories (ID_Title, ID_Category)
SELECT Title, NULL
FROM ing_title WHERE ID_Title = 2
UNION
SELECT NULL, category
FROM ing_categories
WHERE ID_Category = 2
(OR)
You can change your table design and use an AFTER INSERT trigger to perform the same in one go.
EDIT:
If you can change your table design to something like below (No need of that extra chaining table)
ing_titles(ID_Title int not null auto_increment PK, title varchar(64) not null);
ing_categories( ID_Category int not null auto_increment PK,
category varchar(64) not null,
ing_titles_ID_Title int not null,
FOREIGN KEY (ing_titles_ID_Title)
REFERENCES ing_titles(ID_Title));
Then you can use a AFTER INSERT trigger and do the insertion like
DELIMITER //
CREATE TRIGGER ing_titles_after_insert
AFTER INSERT
ON ing_titles FOR EACH ROW
BEGIN
-- Insert record into ing_categories table
INSERT INTO ing_categories
( category,
ing_titles_ID_Title)
VALUES
('Meat' NEW.ID_Title);
END; //
DELIMITER ;

Prepend text to MySQL columns' names

Supposing a query such as:
SELECT * FROM tableA;
How can I prepend a_ to each columns' name? For example if there is a column "username" it would be accessed in the results as "a_username".
EDIT: The SELECT username AS a_username format will not help as I need to continue using the * field selection. There is a JOIN and a potential conflict with a returned column from another table in the JOIN. I will be iterating over the returned columns (foreach) and only want to output the columns that came from a particular table (whose schema may change) to HTML input fields where a site admin could edit the fields' content directly. The SQL query in question looks like SELECT firstTable.*, anotherTable.someField, anotherTable.someOtherField and their exists the possibility that someField or someOtherField exists in firstTable.
Thanks.
You can use the INFORMATION_SCHEMA.COLUMNS table to formulate the query and then use dynamic SQL to execute it.
First let's make a sample database called dotancohen and a table called mytable
mysql> drop database if exists dotancohen;
Query OK, 1 row affected (0.03 sec)
mysql> create database dotancohen;
Query OK, 1 row affected (0.00 sec)
mysql> use dotancohen
Database changed
mysql> create table mytable
-> (
-> id int not null auto_increment,
-> username varchar(30),
-> realname varchar(30),
-> primary key (id)
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> insert into mytable (realname,username) values
-> ('rolando','odnalor'),('pamela','alemap'),
-> ('dominique','euqinimod'),('diamond','dnomaid');
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from mytable;
+----+-----------+-----------+
| id | username | realname |
+----+-----------+-----------+
| 1 | odnalor | rolando |
| 2 | alemap | pamela |
| 3 | euqinimod | dominique |
| 4 | dnomaid | diamond |
+----+-----------+-----------+
4 rows in set (0.00 sec)
mysql>
Here is the metadata table called INFORMATION_SCHEMA.COLUMNS:
mysql> desc INFORMATION_SCHEMA.COLUMNS;
+--------------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| COLUMN_NAME | varchar(64) | NO | | | |
| ORDINAL_POSITION | bigint(21) unsigned | NO | | 0 | |
| COLUMN_DEFAULT | longtext | YES | | NULL | |
| IS_NULLABLE | varchar(3) | NO | | | |
| DATA_TYPE | varchar(64) | NO | | | |
| CHARACTER_MAXIMUM_LENGTH | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_OCTET_LENGTH | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_PRECISION | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_SCALE | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_SET_NAME | varchar(32) | YES | | NULL | |
| COLLATION_NAME | varchar(32) | YES | | NULL | |
| COLUMN_TYPE | longtext | NO | | NULL | |
| COLUMN_KEY | varchar(3) | NO | | | |
| EXTRA | varchar(27) | NO | | | |
| PRIVILEGES | varchar(80) | NO | | | |
| COLUMN_COMMENT | varchar(1024) | NO | | | |
+--------------------------+---------------------+------+-----+---------+-------+
19 rows in set (0.02 sec)
mysql>
What you need from this table are the following columns:
table_schema
table_name
column_name
ordinal_position
What you are asking for is to have the column_name and the column_name prepended with a_
Here is the query and how to execute it:
select concat('select ',column_list,' from ',dbtb) into #newsql
from (select group_concat(concat(column_name,' a_',column_name)) column_list,
concat(table_schema,'.',table_name) dbtb from information_schema.columns
where table_schema = 'dotancohen' and table_name = 'mytable'
order by ordinal_position) A;
select #newsql;
prepare stmt from #newsql;
execute stmt;
deallocate prepare stmt;
Let's execute it
mysql> select concat('select ',column_list,' from ',dbtb) into #newsql
-> from (select group_concat(concat(column_name,' a_',column_name)) column_list,
-> concat(table_schema,'.',table_name) dbtb from information_schema.columns
-> where table_schema = 'dotancohen' and table_name = 'mytable'
-> order by ordinal_position) A;
Query OK, 1 row affected (0.01 sec)
mysql> select #newsql;
+--------------------------------------------------------------------------------+
| #newsql |
+--------------------------------------------------------------------------------+
| select id a_id,username a_username,realname a_realname from dotancohen.mytable |
+--------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> prepare stmt from #newsql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> execute stmt;
+------+------------+------------+
| a_id | a_username | a_realname |
+------+------------+------------+
| 1 | odnalor | rolando |
| 2 | alemap | pamela |
| 3 | euqinimod | dominique |
| 4 | dnomaid | diamond |
+------+------------+------------+
4 rows in set (0.01 sec)
mysql> deallocate prepare stmt;
Query OK, 0 rows affected (0.00 sec)
mysql>
Give it a Try !!!
You mentioned in your question : The SELECT username AS a_username format will not help as I need to continue using the * field selection.
All you have to do to implement my suggestion is run the query using tableA as follows:
select concat('select ',column_list,' from ',dbtb) into #newsql
from (select group_concat(concat(column_name,' a_',column_name)) column_list,
concat(table_schema,'.',table_name) dbtb from information_schema.columns
where table_schema = DATABASE() and table_name = 'tableA'
order by ordinal_position) A;
When you retrieve that query result, just use it as the query to submit to mysql_query.
You'll need to list the columns, e.g
SELECT username AS a_username FROM tableA;
alternatively, post-process in back-end, e.g. change the array keys in your code
Create a view with renamed columns, e.g. -
CREATE VIEW a_view AS SELECT username AS a_username FROM table;
Then refer to this view.
As already mentioned, there is no standard way to mass-prefix column names in a regular query.
But if you really wanted to achieve it, you could write a stored procedure which would query information_schema to get a list of columns in a table, and then prefix them one by one. After that it's possible to concatenate a query as string, PREPARE and EXECUTE it.
A downside to this approach is the fact that you cannot join on a stored procedure's result. But of course, you could as well create a stored procedure for each type of query you issue. Prefixing the fields for any table could then be made a separate generic FUNCTION.
All of this stuff sounds to me like an overkill, though. I would recommend either renaming the actual columns, so that they are always prefixed, or just listing all the result fields with AS aliases, as Scibuff and Alister suggested.
I don't believe it can be done for all columns automatically, but you can list as many columns as you like with AS
SELECT id AS a_id,
name AS a_name,
email AS a_email /*, etc....*/
FROM tableA;
I've only inserted newlines for some extra clarity.