Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Tag Archives: mysql

SQLyog on Linux

0 Comments

Run SQLyog on Linux with WINE. You can download the sqlyog windows installer and just run it with WINE. SQLyog is my favourite GUI client. It is lightweight and powerful. I use the community edition which comes free. The paid versions of the app are awesome. I particularly miss the autocomplete feature. This feature autocompletes table names, field names, query commands etc. on tab press. I have used the MySQL query browser, but there's no comparison. The problem with SQLyog being that it runs only on windows :( . I tried alternatives - phpmyadmin, MySQL query browser - but none of them quite measured up against SQLyog. So I decided to try and get it working on my linux box ( Ubuntu Maverick - 10.10 ). I use Wine version wine-1.3.11 . Downloaded the latest installer from the community edition project page. Ran the installer with Wine and voila! That was it :) sqlyog running on wine in Ubuntu
Filed under code
Jan 16, 2011

How to update a field in a table with a field from another table

0 Comments

I needed a table which gave the map between the URLs and nodes in drupal. I ended up creating a table like this :

 

CREATE TABLE `nid_url_map` (
`nid` int(10) NOT NULL DEFAULT '0',
`url` varchar(255) DEFAULT NULL,
`type` varchar(32) DEFAULT NULL,
PRIMARY KEY (`nid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

To populate this table I used the url_alias table :

CREATE TABLE `url_alias` (
`pid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`src` varchar(128) NOT NULL DEFAULT '',
`dst` varchar(128) NOT NULL DEFAULT '',
`language` varchar(12) NOT NULL DEFAULT '',
`nid` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`pid`),
UNIQUE KEY `dst_language` (`dst`,`language`),
KEY `src_language` (`src`,`language`),
KEY `nid` (`nid`)
) ENGINE=MyISAM AUTO_INCREMENT=267714 DEFAULT CHARSET=utf8

This is the query I ran at first to populate the table :

INSERT IGNORE INTO nid_url_map (nid,url)
SELECT SUBSTR(src,6) AS nid,dst AS url FROM url_alias
WHERE src LIKE 'node/%';

The url_alias table maps the internal url ( in the form of node/ID ) to the pretty URL of the page ( eg. comedy/monty-python-spam ) . Now what remained was the type. The node type is available in the node table :

CREATE TABLE `node` (
`nid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`vid` int(10) unsigned NOT NULL DEFAULT '0',
`type` varchar(32) NOT NULL DEFAULT '',
`language` varchar(12) NOT NULL DEFAULT '',
`title` varchar(255) NOT NULL DEFAULT '',
`uid` int(11) NOT NULL DEFAULT '0',
`status` int(11) NOT NULL DEFAULT '1',
`created` int(11) NOT NULL DEFAULT '0',
`changed` int(11) NOT NULL DEFAULT '0',
`comment` int(11) NOT NULL DEFAULT '0',
`promote` int(11) NOT NULL DEFAULT '0',
`moderate` int(11) NOT NULL DEFAULT '0',
`sticky` int(11) NOT NULL DEFAULT '0',
`tnid` int(10) unsigned NOT NULL DEFAULT '0',
`translate` int(11) NOT NULL DEFAULT '0',
`titlehash` char(32) DEFAULT '',
PRIMARY KEY (`nid`),
UNIQUE KEY `vid` (`vid`),
KEY `node_changed` (`changed`),
KEY `node_created` (`created`),
KEY `node_moderate` (`moderate`),
KEY `node_promote_status` (`promote`,`status`),
KEY `node_status_type` (`status`,`type`,`nid`),
KEY `node_title_type` (`title`,`type`(4)),
KEY `node_type` (`type`(4)),
KEY `uid` (`uid`),
KEY `tnid` (`tnid`),
KEY `translate` (`translate`),
KEY `title` (`title`),
KEY `titlehash` (`titlehash`)
) ENGINE=MyISAM AUTO_INCREMENT=197121 DEFAULT CHARSET=utf8

I referred the MySQL man page on the UPDATE query. Found a useful query in the comments there. In case I need to use this one again, here's where I'm gonna refer :)

UPDATE nid_url_map,node
SET nid_url_map.type = node.type
WHERE nid_url_map.nid = node.nid;

 

There you go, easy peasy :)

Filed under code
Dec 7, 2010

How to dump or export all the table definitions in a MySQL database

0 Comments

So you have  a database with loads of tables. You want the table definitions of all of them. You don’t really need the data. mysqldump to the rescue!

mysqldump -u USERNAME --password=PASSWORD --no-data --opt DB1 > DUMPFILE

That’s it! Easy as a pie :) The key here is the –no-data option. It dumps all the table definitions, but not the table data.

Want to do this for multiple databases? No problem.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt --databases DB1 DB2 > DUMPFILE

The –databases option allows you to add multiple databases.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt --all-databases > DUMPFILE

The –all-databases option allows you to dump all the databases.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt DB1 --ignore-table DB1.TABLENAME1 --ignore-table DB1.TABLENAME2 > DUMPFILE

–ignore-table option allows you to skip dumping certain tables. Do not forget to specify the databasename when using this option.

 

Know any more tricks? Let us know in the comments below :)

Filed under code
Feb 5, 2010