MySQL

From Schmid.wiki
Jump to: navigation, search

Contents

Cheat Sheet

Add column to existing table : ALTER TABLE contacts ADD email VARCHAR(60);
Update value                 : UPDATE example SET age='22' WHERE age='21'
Retrieve data                : select fields from tablename where field='value';
Regular expressions          : select * from messages
                                  where name regexp "[Bb]and?ana.*";
Create database              : create database newdatabase;
Create user                  : grant all on newdatabase.* to 'user'@'localhost'
                                  identified by 'somepassword';
Update password              : SET PASSWORD FOR 'user'@'localhost' = PASSWORD('newpassword');
Create table                 : create table events (
                                  id int not null auto_increment,
                                  time datetime,
                                  title varchar(255) default 'no title',
                                  description text,
                                  primary key(id)
                               );
Insert value into table      : insert into events values (0,
                                  '2006-03-25 19:59:59', 'example',
                                  'This is an example');

Renaming a Database

# cd /var/lib/mysql
# mv name newname

Command-line mysql

$ mysql database -uusername -p

Backup of schmid.wiki Database

Backup:

# mysqldump --opt -uwikiuser -ppassword wikidb | bzip2 -c >schmid.wiki.sql.bz2

Restore:

# bunzip2 schmid.wiki.sql.bz2
# mysql -uroot -p
mysql> create database wikidb;
# mysql -uroot -p  wikidb < schmid.wiki.sql

References

Personal tools