basic mysql browsingSOFTWARE: mysqlmysql -uname -p optionaldbname Here are some basic commands to connect to and browse mysql via command line: Connect to mysql with your NAME:
Enter password at prompt. Then check out your databases:
Then to select a db to browse:
Optional: add database name at the end of mysql connection to go straight to a database of your choice:
Now that we're in a database:
And to get a specific schema from a table:
From here you can proceed to have fun with your full array of select, update, insert craziness. MORE INFO: http://www.pantz.org/database/mysql/mysqlcommands.shtmlcountSOFTWARE: mysqlselect count(distinct subject) from ncurs; count something in a table (with a given criteria) MORE INFO:simple joinsSOFTWARE: mysqlselect t2.name from tabletwo as t2, tableone as t1 where t1.id = t2.id; A quick syntax for a basic inner join from multiple tables. You can also do aliases to make more concise by add 'as t1, as t2' to table names (see example) and then referencing t1 or t2 instead of the full table names. Note that this syntax performs an "inner" join by default, meaning that only results that match between both tables are returned! This is because the comma between the two table names is equivalent to basically saying "join". When using a comma, you use "where" in the second part of the statement. To include null results, use the full syntax (calling "left outer join" for example in place of the comma between the tables, and "on" in place of "where"). More info available on the mysql manual site (link below). MORE INFO: http://dev.mysql.com/doc/refman/5.0/en/join.htmladd column to tableSOFTWARE: mysqlalter table applications add column nickname varchar(255); Use to add a column to a table. Also you can specify where the new column goes by adding "after some_column_name" at the end of the command:
MORE INFO: update field valueSOFTWARE: mysqlupdate pets set doggiename = 'rex' where doggiename = 'oscar'; Use to change a field value in mysql. Safest way to use this, since you'll be changing values in the db, is to run a select statement first to make sure you're returning the correct results. Then, when you're sure you're selecting the right things, modify it to use 'update' and 'set' to make the actual changes to the values. The example here would update a pets table to change all doggie names of oscar to rex.
Or if you wanted to set a value to null for a unique record id you could do something like:
MORE INFO: left outer joinSOFTWARE: mysqlUse of left outer join to get foreign key values returned in place of _id column values in main table. And then also filtering those results with a where clause at the end to limit the result set based on given criteria. In this case, we have three tables: people, dogs, and cats. The people table contains the _id columns for dogs and cats. We first get the actual dog and cat names to replace the _id values (including null dog and cat names since we're using a left outer), then we filter to only see results if the cat's name is 'lumpah'.
MORE INFO: where field in (value range)SOFTWARE: mysqlselect fieldname from tablename where fieldtoscan in (value1, value2, value3); If you have a field to scan/select by like "id" and you want to get values back for multiple records you can search them all by using "in":
For example, this will return the email addresses for records 1 through 3 in the people table:
MORE INFO: rename existing column in tableSOFTWARE: mysqlalter table pets change dogs cats varchar(255); Use the alter command to change an existing column name to a new name. (Note that you need to declare the type, even if it is not changing.) So to rename a column that's a varchar(255) in the pets table from dogs to cats you do:
MORE INFO: mysql dump database backupSOFTWARE: mysqlmysqldump --opt -u username -p databasename > backupfilename.sql Make the DumpBackup a mysql database to a file:
Dump All Option
Help
Import into MysqlIf you want to turn around and import the dump into another mysql you can run this command on the other mysql:
Import All Option
Note: grants might not show up correctly after this for your mysql users, so you can run this in mysql to straighten them out:
MORE INFO: set main passwordSOFTWARE: mysql(as root) mysqladmin password 'password_here' To create the root password for mysql using mysqladmin when logged in as root:
Or, if you need to designate it specifically for root:
Altho, I'm not sure when you'd really need the second case. Guess if you have priveleges to run mysqladmin but your not logged in as root... MORE INFO:grants in mysqlSOFTWARE: mysqlshow grants for username@host Showing GrantsMysql allows multiple user@host combinations to have different privileges. Find out what privileges (grants) are assigned for users at various hosts with:
Example:
Create GrantsCreate grants with this type of syntax: First, let the user connect to mysql fro m their computer (host) with a certain password:
Give a user@host all privileges for a certain db:
If you didn't successfully set up the user/host with a password first, you set the user's password with:
Test Connecting from Remote HostTest connecting via the commandline (from remote host) with :
MORE INFO: http://www.pantz.org/software/mysql/mysqlcommands.html |