mysql get table structure with column collation - mysql

I need to get table full structure as one string.
I use SHOW CREATE TABLE my_table but lack column collation information
CREATE TABLE `my_table` (
`col_1` int(11) NOT NULL AUTO_INCREMENT,
`col_2` varchar(255) NOT NULL,
PRIMARY KEY (`col_1`),
KEY `col_2` (`col_2`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4
Espected:
Instead col_2 varchar(255) NOT NULL,
I want col_2 varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
One strange thing:
I don't understand why certains tables show also the column colation with SHOW CREATE TABLE but others no. However if I look the structure table in phpmyadmin all tables show column collation (when the column is varchar or text type)
One last note:
If I use SHOW FULL COLUMNS ... all columns have also their collation info (even if are showing or not in SHOW CREATE TABLE)
So if I have not other option I try to recreate the creation table string manually from SHOW FULL COLUMNS result. But i think that this is a hard and risky work ...
All that I need to use in a personal tool to compare when have structure change (between two servers)

Related

MySQL create table by SQL file made strange table structure

I played with MySQL DB 5.6 as some kind of praxis. As tool for creating table I used Navicat and made table with this
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for promet
-- ----------------------------
DROP TABLE IF EXISTS `traffic`;
CREATE TABLE `traffic` (
`p_id` bigint(20) NOT NULL AUTO_INCREMENT,
`p_cbr` bigint(20) DEFAULT NULL,
`p_invnum` bigint(20) DEFAULT NULL,
`p_issued` date DEFAULT NULL,
`p_returned` date DEFAULT NULL,
`p_deadtime` smallint(4) DEFAULT NULL COMMENT 'end date',
PRIMARY KEY (`p_id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4;
but when I uploaded it on hosting place by phpmyadmin I got table where each column (field) got small key on the right end? I expected that only p_id is indexed field (on the right side is grey key) but all fields also have yellow key! Under Action column for all fields is visible:
Change | Drop | Key pic |Primary| U Unique| More
Beside this, Navicat presented empty date type fields with Null contents, here, on hosting place, it's filled with 0000-00-00 and I'm not sure how to set up SQL in order to check that some particular isn't filled up, e.g. I used ISNULL on my local machine, like:
select * FROM traffic where p_invnum='$par1' AND ISNULL(p_returned)
Did I make some error or it's phpmyadmin feature? Thank you.
In phpmyadmin I changed theme to another, called "original", environment is now more classic, simple but structure options of mentioned table now looks different, just as I expected. Why, I don't know.
Moreover, empty date type fields, filled up with 0000-00-00 instead Null (as default values), doesn't force me to correct queries, probably MySQL accept 0000-00-00 as Null itself.

MYSQL copy fields from one table's structure and add them to another tables

I've added some additional fields for my table in db. Now I need to have this additional fields in other few tables. So the question is - can I somehow copy those fields from source table and add them to another tables? Both mysql console and phpmyadmin variants woulbe be nice. Thanks!
A phpmyadmin variant would be to export the table's structure only (Export->Custom->Choose "Structure"). After that, you will get something like this in the exported SQL file:
CREATE TABLE `table` (
`id` int(10) NOT NULL,
`name` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
At that point, you can go ahead and remove the last line of the query and the parenthesis after the CREATE TABLE. Then, go ahead and substitute the [other_table] name and change the query to match the following:
ALTER TABLE `[other_table]`
ADD `id` int(10) NOT NULL,
ADD `name` varchar(50) DEFAULT NULL;
Notice how all I did was change CREATE to ALTER and add ADD before each field.
NOTE: This is not very useful on such a trivial example, but when dealing with large amounts of columns, it could prove somewhat useful.

Unable to add Column to table in mysql

I am trying to add a column to a table.To do so I am trying
ALTER TABLE requirements Modify COLUMN parent_id int(11);
but when I try to execute this query mysql does not respond for long.So each time I have to kill the query.
I have created the table using
CREATE TABLE requirements (requirement_id smallint(6) NOT NULL AUTO_INCREMENT,
product_id smallint(6) NOT NULL,
name varchar(255) CHARACTERSET latin1 NOT NULL DEFAULT '',
PRIMARY KEY (requirement_id),
UNIQUE KEY requirement_product_id_name_idx (product_id,name),
UNIQUE KEY requirement_product_idx (requirement_id,product_id),
KEY requirement_name_idx_v2 (name) )
ENGINE=InnoDB
AUTO_INCREMENT=7365
DEFAULT CHARSET=utf8;
Please help me know why I am not able to execute the Alter table query.I am new to database is there something wrong with my alter table query.
According to your table defintion parent_id seems to be a new column which you want to add so your query should be to add the column not modify.
Try this:
alter table requirements add column parent_id int(11);
SQL FIDDLE DEMO
On a side note:
There needs to be a space between CHARACTERSET here
name varchar(255) CHARACTERSET latin1 NOT NULL DEFAULT '',
should be
name varchar(255) CHARACTER SET latin1 NOT NULL DEFAULT '',

Copy/duplicate SQL row with blob/text, How do that?

I would like to copy a SQL's row into the same table.
But in my table, I've a 'text' column.
With this SQL:
CREATE TEMPORARY TABLE produit2 ENGINE=MEMORY SELECT * FROM produit WHERE pdt_ID = 'IPSUMS';
UPDATE produit2 SET pdt_ID='ID_TEMP';
INSERT INTO produit SELECT * FROM produit2;
DROP TABLE produit2;
I get this error :
#1163 - The used table type doesn't support BLOB/TEXT columns
Here is my table :
pdt_ID varchar(6)
pdt_nom varchar(130)
pdt_stitre varchar(255)
pdt_accroche varchar(255)
pdt_desc text
pdt_img varchar(25)
pdt_pdf varchar(10)
pdt_garantie varchar(80)
edit_ID varchar(7)
scat_ID int(11)
pdt_asso1 char(3)
pdt_asso2 char(3)
pdt_online tinyint(4)
It's possible to help me to duplicate row ? How?
You can't store TEXT-columns (which really are blobs) in memory tables. See here
Depending on your ultimate goal, you may insert a md5-hash of the TEXT-column instead to preserve entity identity. Otherwise you need to put pdt_desc and such into another table and refer to it's primary key - that will save you some storage/memory too.

How to copy a field from a table paste into another table with same properties

I am searching this for hours.
I just want to copy some fields from one table into an another table. In the destination table the fields i want to copy doesn't exsist.
It's like, copy and paste.
Ex. I have to tables
Table A Like
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`active` int(11) DEFAULT '0'
Table B like
`date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`alias` varchar(255) DEFAULT NULL,
`voters` int(11) DEFAULT NULL,
`level` int(11) NOT NULL DEFAULT '0',
`content` text
Ex. I want to copy 'content , level, voters' from Table B to Table A with the same structure and data.
I have in my mind like this, But HOW?
Alter Table A Add content (same SCHEMA as TABLE B.content)
Alter Table A Add level (same SCHEMA as TABLE B.level)
Alter Table A Add level (voters SCHEMA as TABLE B.voters)
INSERT INTO A (col1 , col2 , col3)
SELECT colB1, colB2,colB3
FROM B
they must be that same datatype and size
UPDATE
if table A does not contain data you can create it like this
CREATE TABLE A LIKE B;
Use LIKE to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:
then make the insert statement above
if you want to keep the data in table A and add some columns from table B you can merge these two table inside new Table X then you delete table A and rename X
CREATE TABLE X
SELECT * from emp;
Note: if table A contains many record this will take too much time and is not a good solution
but your select statement will contains join , if there is a relationship between A and B tell me to write a more specific solution