`

MySQL Commands

 
阅读更多

To login (from unix shell) use -h only if needed.

# [mysql dir]/bin/mysql -h hostname -u root -p

Create a database on the sql server.

mysql> create database [databasename];

List all databases on the sql server.

mysql> show databases;

Switch to a database.

mysql> use [db name];

To see all the tables in the db.

mysql> show tables;

To see database's field formats.

mysql> describe [table name];

To delete a db.

mysql> drop database [database name];

To delete a table.

mysql> drop table [table name];

Show all data in a table.

mysql> SELECT * FROM [table name];

Returns the columns and column information pertaining to the designated table.

mysql> show columns from [table name];

Show certain selected rows with the value "whatever".

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

Show all records containing the name "Bob" AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';

Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;

Show all records starting with the letters 'bob' AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';

Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;

Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

Sum column.

mysql> SELECT SUM(*) FROM [table name];

Join tables on common columns.

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;

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;

Change a users password from unix shell.

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'

Change a users password from MySQL prompt. Login as root. Set the password. Update privs.

# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;

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.

# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

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.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.

# mysql -u root -p
mysql> use mysql;
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');
mysql> flush privileges; 

or 

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;

To update info already in a table.

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';

Delete a row(s) from a table.

mysql> DELETE from [table name] where [field name] = 'whatever';

Update database permissions/privilages.

mysql> flush privileges;

Delete a column.

mysql> alter table [table name] drop column [column name];

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.

mysql> alter table [table name] drop index [colmn name];

Load a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db's.

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

Create Table Example 1.

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));

Create Table Example 2.

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');

分享到:
评论

相关推荐

    mysql commands

    Rational Rose是由美国的Rational公司开发的、面向对象的可视化建模工具。利用Rose工具,可以建立用UML描述的软件系统模型,它支持UML中的用例框图、活动框图、序列框图、协作框图、状态框图、组建框图和布局框图等...

    php_mysql_class.zip_MYSQL_class_php_php class_php+mysql

    PHP MYSQL CLASS, MySQL Class to allow easy and clean access to common mysql commands

    Common-MySQL-Commands.rar_MYSQL

    有很多朋友虽然安装好了mysql但却不知如何使用它。在这篇文章中我们就从连接MYSQL、修改密码、增加用户等方面来学习一些MYSQL的常用命令。

    MYSQL

    18.2.3 Host '...' is blocked错误 18.2.4 Out of memory错误 18.2.5 Packet too large错误 18.2.6 The table is full错误 18.2.7 Commands out of sync in client错误 18.2.8 Ignoring ...

    MySQL中文参考手册.chm

    MySQL中文参考手册.chm 449kb &lt;br/&gt;0 译者序 1 MySQL的一般的信息 1.1 什么是MySQL? 1.2 关于本手册 1.2.1 本手册中使用的约定 1.3 MySQL的历史 1.4 MySQL的主要特征 1.5...

    mysql-connector-net-8.0.21.msi

    MySqlCommandBuilder: Automatically generates single-table commands used to reconcile changes made to a DataSet with the associated MySQL database. MySqlDataAdapter: Represents a set of data commands ...

    nrpe监控mysql.docx

    Commands end with ; or \g. Your MySQL connection id is 51910 Server version: 5.5.3-m3-log Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql&gt;...

    MySQL中文参考手册

    * 1 MySQL的一般的信息 o 1.1 什么是MySQL? o 1.2 关于本手册 + 1.2.1 本手册中使用的约定 o 1.3 MySQL的历史 o 1.4 MySQL的主要特征 o 1.5 MySQL稳定性? o 1.6 顺应2000年 o 1.7 SQL一般信息和教程 o ...

    mysql存储过程之case语句用法实例详解

    本文实例讲述了mysql存储过程之case语句用法。分享给大家供大家参考,具体如下: 除了if语句,mysql提供了一个替代的条件语句CASE。 mysql的 CASE语句使代码更加可读和高效。废话不多说,我们先来看下简单case语句的...

    mysql-5.5.14.tar.gz

    Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.14-log Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. Oracle...

    MYSQL.rar_MYSQL_ROOT_mysql root_root-Cyclic-Music_sql

    Commands end with or \g. Your MySQL connection id is 4 to server version: 3.23.25-beta-log Type help or \h for help. Type \c to clear the buffer mysql&gt; 然后发布查询,直接键入题目中的语句: mysql...

    mysql-front解压使用

    MySQL-Front is a Windows front end program for the MySQL database server. The database structure and data can be handled via dialogs or SQL commands.

    Sams.MySQL.Phrasebook.Essential.Code.and.Commands.Mar.2006.chm

    Sams.MySQL.Phrasebook.Essential.Code.and.Commands.Mar.2006.chm

    mysql-8.0.20-macos10.15-x86_64.tar.gz

    Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.20 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of ...

    mysqlapp:监控Mysql db的应用程序

    * Create a read only user to access the following MySQL commands ** SHOW GLOBAL VARS ** SHOW GLOBAL STATUS * Give logscape read access to the /var/log/mysql log folder * Update the following vars ...

    mysql示例数据库 employee

    You need a MySQL database server (5.0+) and run the commands below through a user that has the following privileges: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, REFERENCES, INDEX, ALTER, ...

    开源的mysql管理工具

    The database structure and data can be handled via dialogs or SQL commands. Import and Export in standard file formats is supported. The database server can be connected directly or via ...

    管理MySQL数据库系统.doc

     Commands end with ; or \\g. 四、MySQL的几个重要目录 MySQL安装完成后不象SQL Server默认安装在一个目录,它的数据库文件、配置文 件和命令文件分别在不同的目录,了解这些目录非常重要,尤其对于Linux的...

    一键搭建个人博客系统脚本,基于完全编译lnmp(linux,nginx,mysql,php)搭建wordpress博客系统

    一键搭建个人博客系统脚本,基于完全编译lnmp(linux,nginx,mysql,php)搭建wordpress博客系统 ...chown -R nginx.nginx ./​重启nginx服务# systemctl restart nginx​登录... Commands end with ; or \g.Your MySQL c

    MySQL 连接

    MySQL 连接 使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库。 实例 ...以下是从命令行中连接mysql服务器的... Commands end with ; or g. Your MySQL connection id

Global site tag (gtag.js) - Google Analytics