How to generate the table schema from INFORMATION_SCHEMA in MySQL? - mysql

I trying to generate table structure using INFORMATION_SCHEMA in MySQL.
I need the same output as
SHOW CREATE TABLE Mytablename;
My intention is to generation create table script for list of tables in mysql.
please help. I need to take table scripts for 100 tables. like below
CREATE TABLE `customer_list` (
`ID` smallint(5) unsigned NOT NULL DEFAULT '0',
`name` varchar(91) DEFAULT NULL,
`address` varchar(50) NOT NULL,
`zip_code` varchar(10) DEFAULT NULL,
`phone` varchar(20) NOT NULL,
`city` varchar(50) NOT NULL,
`country` varchar(50) NOT NULL,
`notes` varchar(6) NOT NULL DEFAULT '',
`SID` tinyint(3) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

SHOW CREATE TABLE your_table_name;

The SQL query to get the table structure from the INFORMATION SCHEMA is as follows, where the database name is "dbname" and the table name is "Mytablename":
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'Mytablename';

Related

mysql insert into select join - copy values from one column to another table, passing through a connecting table

I can't get this to work
CREATE TABLE `oc_tax_class` (
`tax_class_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `oc_tax_rate`
--
CREATE TABLE `oc_tax_rate` (
`tax_rate_id` int(11) NOT NULL,
`geo_zone_id` int(11) NOT NULL DEFAULT 0,
`name` varchar(255) NOT NULL,
`rate` decimal(15,4) NOT NULL DEFAULT 0.0000,
`type` char(1) NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `oc_tax_rule`
--
CREATE TABLE `oc_tax_rule` (
`tax_rule_id` int(11) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`tax_rate_id` int(11) NOT NULL,
`based` varchar(10) NOT NULL,
`priority` int(5) NOT NULL DEFAULT 1
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
3 tables. I want oc_tax_class.title = oc_tax_rate.name
I believe, although I'm not sure, that I should
INSERT INTO oc_tax_class(title)
or
UPDATE oc_tax_class SET title = ...
SELECT oc_tax_rate.name, oc_tax_rule.tax_class_id
JOIN oc_tax_rule ON oc_tax_rate.tax_rate_id = oc_tax_rule.tax_rate_id
And then I don't know what to do next.
I need to copy values from one column to another table, passing through a connecting table.
MySQL supports a multi-table UPDATE syntax, but the documentation (https://dev.mysql.com/doc/refman/en/update.html) has pretty sparse examples of it.
In your case, this may work:
UPDATE oc_tax_class
JOIN oc_tax_rule USING (tax_class_id)
JOIN oc_tax_rate USING (tax_rate_id)
SET oc_tax_class.title = oc_tax_rate.name;
I did not test this. I suggest you test it first on a sample of your data, to make sure it works the way you want it to.

How to store translates in MySQL to use join?

I have a table that contains all translations of words:
CREATE TABLE `localtexts` (
`Id` int(11) NOT NULL,
`Lang` char(2) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'pe',
`Text` varchar(300) DEFAULT NULL,
`ShortText` varchar(100) NOT NULL,
`DbVersion` timestamp NOT NULL DEFAULT current_timestamp(),
`Status` int(11) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
As example there is a table that refers to localtexts:
CREATE TABLE `composes` (
`Status` int(11) NOT NULL DEFAULT 1,
`Id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The table above has foreign key Id to localtexts.Id. And when I need to get word on English I do:
SELECT localtexts.text,
composes.status
FROM composes
LEFT JOIN localtexts ON composes.Id = localtexts.Id
WHERE localtexts.Lang = 'en'.
I'm concerned in performance this decision when there are a lot of tables for join with localtexts.
You might find that adding the following index to the localtexts table would speed up the query:
CREATE INDEX idx ON localtexts (Lang, id, text);
This index covers the WHERE clause, join, and SELECT.

SQL select for serial column

Assume I have a data with columns :
aa1, aa2, aa3, aa4, aa5, aa6
and so on.
I 'm looking for a select query where I can just mention something like :
select aa[1 to n] from table...
Is there any way to do it directly ?
Thanks
In MariaDB there is a system settings in database information_schema where are defined all schemas, tables, columns, etc.
Using this you can list all details about the table and use it to compose the "dynamic" query as you need.
Example of table definition:
CREATE TABLE `tablename` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`aa1` INT(11) NULL DEFAULT NULL,
`aa2` INT(11) NULL DEFAULT NULL,
`aa3` INT(11) NULL DEFAULT NULL,
`aa4` INT(11) NULL DEFAULT NULL,
`aa5` INT(11) NULL DEFAULT NULL,
`aa6` INT(11) NULL DEFAULT NULL,
`aa7` INT(11) NULL DEFAULT NULL,
`aa8` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_bin'
ENGINE=InnoDB
AUTO_INCREMENT=3
;
The query to information_schema to list the tables:
SELECT GROUP_CONCAT(COLUMN_NAME) FROM information_schema.`COLUMNS`
WHERE TABLE_SCHEMA = 'stackoverflow'
AND TABLE_NAME = 'tablename'
AND COLUMN_NAME LIKE 'aa%'
And the result:
aa1,aa2,aa3,aa4,aa5,aa6,aa7,aa8
So you can then built your query as needed.
Edit:
In SQL documentation is said: "Do not use INFORMATION_SCHEMA views to determine the schema of an object. The only reliable way to find the schema of a object is to query the sys.objects catalog view."

How to update / insert data from table belong to database "A" to table belong to a database "B"?

How to update / insert data from table belong to database "A" to table belong to a database "B" ?
For example, I have a table in the name of ips as below belong to database "A":
CREATE TABLE `ips` (
`id` int(10) unsigned NOT NULL DEFAULT '0',
`begin_ip_num` int(11) unsigned DEFAULT NULL,
`end_ip_num` int(11) unsigned DEFAULT NULL,
`iso` varchar(3) DEFAULT NULL,
`country` varchar(150) DEFAULT NULL
) ENGINE=InnoDB
Let's assume I have a second table country belongs to database "B":
CREATE TABLE `country` (
`countryid` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`ordering` smallint(5) unsigned NOT NULL DEFAULT '0',
`iso` char(2) NOT NULL,
PRIMARY KEY (`countryid`)
) ENGINE=InnoDB
note :the two database are in the same server
You have to prefix the table names by the DB/schema name. Something like that:
INSERT INTO `database B`.`country` (columns)
SELECT columns FROM `database A`.`ips`;
Of course, you have to replace columns by the required column names and/or expression corresponding to your needs.
In SQLServer it goes like;
insert into x select * from otherdatabase.owner.table
Which can be expanded to select columns etc..
In Oracle you might need a database link bvetween them. THat was a long time ago for me ;-)

Equivalent of MSSQL IDENTITY Column in MySQL

What is the equivalent of MSSQL IDENTITY Columns in MySQL? How would I create this table in MySQL?
CREATE TABLE Lookups.Gender
(
GenderID INT IDENTITY(1,1) NOT NULL,
GenderName VARCHAR(32) NOT NULL
);
CREATE TABLE Lookups.Gender
(
GenderID INT NOT NULL AUTO_INCREMENT,
GenderName VARCHAR(32) NOT NULL
);
CREATE TABLE `Persons` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`LastName` varchar(255) NOT NULL,
`FirstName` varchar(255) DEFAULT NULL,
`Address` varchar(255) DEFAULT NULL,
`City` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=latin1;
This above example uses the AUTO_INCREMENT syntax. You can specify a starting offset specific to the table.
The Increment, however, has to be set globally.
SET ##auto_increment_increment=10;
You can also set a global default for the offset like follows:
SET ##auto_increment_offset=5;
To view your current values, type SHOW VARIABLES LIKE 'auto_inc%';