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_command.zip_MYSQL”中,我们主要关注的是“mysql_command.pdf”,这个文档很可能包含了MySQL的常用命令及其详细解释。下面将详细介绍一些重要的MySQL命令。 1. **启动与关闭MySQL服务**: - `...
最近老是忘记Mysql Command Line Client异常关闭该怎样解决,因此我整理了一套解决方案,省的以后再花时间去百度上搜,给自己提供一个捷径,本人纯小白,解决方案仅供参考。
从给定的文件信息中,我们可以总结出一系列关于MySQL命令的重要知识点,这将对初学者以及需要复习MySQL基础操作的人员大有裨益。 ### 一、MySQL服务的启动与停止 - **启动MySQL服务**:通过Windows命令行工具,...
`mysqlcommand`是一个方便的工具,它为Laravel提供了更便捷的方式来使用MySQL命令行工具,使得开发者无需离开命令行环境即可执行常见的MySQL管理任务。这个压缩包文件`mysql-command-master`很可能包含了一个用于...
本压缩包文件"mysql-command.rar"聚焦于MySQL的操作,尤其是通过命令行界面进行的工作。下面将详细阐述相关知识点。 一、MySQL安装与启动 在开始使用MySQL命令行之前,你需要先在你的操作系统上安装MySQL。这通常...
前几天重装了mysql,装好之后navicat一直正常可以对mysql进行操作,但是今天要使用命令行进行操作时遇到-bash: mysql: command not found的问题。 总结一下解决办法: 这个问题的出现是两个原因 1 .bash_profile中...
这份名为"MySQL_command.zip_MYSQL"的压缩包文件显然包含了关于如何通过命令行界面操作MySQL数据库的重要信息。以下是根据提供的文件名“MySQL教程.doc”所推测的MySQL命令行操作的关键知识点: 1. **MySQL安装与...
根据给定的文件信息,以下是对“mysql command 整理”相关知识点的详细解析: ### MySQL 常用命令 MySQL是一种广泛使用的开源关系数据库管理系统(RDBMS),提供了丰富的命令集来管理数据库、表以及数据。以下是...
在Linux环境中,当你尝试在命令行中运行`mysql`命令时,如果遇到`bash: mysql: command not found`的错误提示,这通常意味着系统无法在默认的可执行文件搜索路径中找到`mysql`客户端。这个错误通常发生在你安装了...
MySQL command line client 使用命令 1.输入密码:****** 2.ues mysql;使用Mysql 3.show databases;显示数据库 4.use register;使用数据库名为register 5.show tables;显示register数据库中的表 6.describe user;...
MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql); return mySqlCommand; } ``` 例如,执行删除操作: ```csharp string sqlDel = "DELETE FROM student WHERE id = 12"; MySqlCommand mySqlCommand = ...
MySQL是世界上最流行的关系型数据库管理系统之一,用于存储和管理数据。本文主要介绍如何使用MySQL命令行客户端连接数据库、创建和管理用户以及操作数据库。 一、连接数据库 在MySQL中,连接到数据库通常通过命令行...
MySqlCommand command1 = new MySqlCommand("INSERT INTO mytable values (@id, @name)", connection, transaction); command1.Parameters.AddWithValue("@id", 1); command1.Parameters.AddWithValue("@name", ...
在MySQLDataAccess类中,利用ADO.NET的MySqlCommand对象编写SQL语句,如INSERT INTO、DELETE FROM、UPDATE和SELECT。使用ExecuteNonQuery()方法执行无返回值的操作(增、删、改),使用ExecuteReader()或...
using (MySqlCommand command = new MySqlCommand(query, connection)) { foreach (var kvp in data) { command.Parameters.AddWithValue($"@{kvp.Key}", kvp.Value); } OpenConnection(); command....
MySqlCommand command = new MySqlCommand(sql, connection); command.Parameters.AddWithValue("@imageData", imageData); command.ExecuteNonQuery(); ``` 在上面的代码中,`File.ReadAllBytes`用于读取图片文件...
MySqlCommand command = new MySqlCommand(sql, connection); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // 处理查询结果 } reader.Close(); ``` 6. **处理结果集**: `...
MySQL Workbench是一款强大的数据库设计和管理工具,专为MySQL数据库系统设计。它是SQL开发、数据库建模和数据库管理的集成解决方案。8.0版本引入了许多新功能和改进,旨在提高用户体验和工作效率。针对中文用户,...
MySqlCommand command = new MySqlCommand("SELECT * FROM mytable", connection); MySqlDataReader reader = command.ExecuteReader(); ``` - **插入、更新和删除**:对于DML操作(INSERT、UPDATE、DELETE),...