Advertisement
ttxnam

MySQL Commands

May 20th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. * To login (from unix shell) use -h only if needed.
  2. # [mysql dir]/bin/mysql -h hostname -u root -p
  3.  
  4. * Create a database on the sql server.
  5. mysql> create database [databasename];
  6.  
  7. * List all databases on the sql server.
  8. mysql> show databases;
  9.  
  10. * Switch to a database.
  11. mysql> use [db name];
  12.  
  13. * To see all the tables in the db.
  14. mysql> show tables;
  15.  
  16. * To see database's field formats.
  17. mysql> describe [table name];
  18.  
  19. * To delete a db.
  20. mysql> drop database [database name];
  21.  
  22. * To delete a table.
  23. mysql> drop table [table name];
  24.  
  25. * Show all data in a table.
  26. mysql> SELECT * FROM [table name];
  27.  
  28. * Returns the columns and column information pertaining to the designated table.
  29. mysql> show columns from [table name];
  30.  
  31. * Show certain selected rows with the value "whatever".
  32. mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";
  33.  
  34. * Show all records containing the name "Bob" AND the phone number '3444444'.
  35. mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';
  36.  
  37. * Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field.
  38. mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;
  39.  
  40. * Show all records starting with the letters 'bob' AND the phone number '3444444'.
  41. mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';
  42.  
  43. * Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5.
  44. mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;
  45.  
  46. * Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.
  47. mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";
  48.  
  49. * Show unique records.
  50. mysql> SELECT DISTINCT [column name] FROM [table name];
  51.  
  52. * Show selected records sorted in an ascending (asc) or descending (desc).
  53. mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
  54.  
  55. * Return number of rows.
  56. mysql> SELECT COUNT(*) FROM [table name];
  57.  
  58. * Sum column.
  59. mysql> SELECT SUM(*) FROM [table name];
  60.  
  61. * Join tables on common columns.
  62. mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;
  63.  
  64. * Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.
  65. # mysql -u root -p
  66. mysql> use mysql;
  67. mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
  68. mysql> flush privileges;
  69.  
  70. * Change a users password from unix shell.
  71. # [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
  72.  
  73. * Change a users password from MySQL prompt. Login as root. Set the password. Update privs.
  74. # mysql -u root -p
  75. mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
  76. mysql> flush privileges;
  77.  
  78. * Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.
  79. # /etc/init.d/mysql stop
  80. # mysqld_safe --skip-grant-tables &
  81. # mysql -u root
  82. mysql> use mysql;
  83. mysql> update user set password=PASSWORD("newrootpassword") where User='root';
  84. mysql> flush privileges;
  85. mysql> quit
  86. # /etc/init.d/mysql stop
  87. # /etc/init.d/mysql start
  88.  
  89. * Set a root password if there is on root password.
  90. # mysqladmin -u root password newpassword
  91.  
  92. * Update a root password.
  93. # mysqladmin -u root -p oldpassword newpassword
  94.  
  95. * Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.
  96. # mysql -u root -p
  97. mysql> use mysql;
  98. mysql> grant usage on *.* to bob@localhost identified by 'passwd';
  99. mysql> flush privileges;
  100.  
  101. * Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.
  102. # mysql -u root -p
  103. mysql> use mysql;
  104. mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
  105. mysql> flush privileges;
  106.  
  107. or
  108.  
  109. mysql> grant all privileges on databasename.* to username@localhost;
  110. mysql> flush privileges;
  111.  
  112. * To update info already in a table.
  113. mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
  114.  
  115. * Delete a row(s) from a table.
  116. mysql> DELETE from [table name] where [field name] = 'whatever';
  117.  
  118. * Update database permissions/privilages.
  119. mysql> flush privileges;
  120.  
  121. * Delete a column.
  122. mysql> alter table [table name] drop column [column name];
  123.  
  124. * Add a new column to db.
  125. mysql> alter table [table name] add column [new column name] varchar (20);
  126.  
  127. * Change column name.
  128. mysql> alter table [table name] change [old column name] [new column name] varchar (50);
  129.  
  130. * Make a unique column so you get no dupes.
  131. mysql> alter table [table name] add unique ([column name]);
  132.  
  133. * Make a column bigger.
  134. mysql> alter table [table name] modify [column name] VARCHAR(3);
  135.  
  136. * Delete unique from table.
  137. mysql> alter table [table name] drop index [colmn name];
  138.  
  139. * Load a CSV file into a table.
  140. mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
  141.  
  142. * Dump all databases for backup. Backup file is sql commands to recreate all db's.
  143. # [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
  144.  
  145. * Dump one database for backup.
  146. # [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
  147.  
  148. * Dump a table from a database.
  149. # [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
  150.  
  151. *** Restore database (or database table) from backup.
  152. # [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
  153.  
  154. * Create Table Example 1.
  155. mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));
  156.  
  157. * Create Table Example 2.
  158. mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');
  159.  
  160. (http://www.pantz.org/software/mysql/mysqlcommands.html)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement