首页 > 代码库 > mysql 一些基础的命令

mysql 一些基础的命令

命令

访问监控器:mysql -u [username] -p;(会提示密码)

显示所有的数据库: show databases;

Access数据库:mysql -u [username] -p [database](会提示密码)

创建新的数据库: create database [database];

选择数据库: use [database];

确定是使用什么数据库: select database();

显示所有表: show tables;

显示表结构: describe [table];

列表中的某个表的所有索引: show index from [table];

创建列的新表: CREATE TABLE [table] ([column] VARCHAR(120), [another-column] DATETIME);

添加一列: ALTER TABLE [table] ADD COLUMN [column] VARCHAR(120);

添加列具有独特的自动递增ID: ALTER TABLE [table] ADD COLUMN [column] int NOT NULL AUTO_INCREMENT PRIMARY KEY;

插入一条记录: INSERT INTO [table] ([column], [column]) VALUES (‘[value]‘, [value]‘);

MySQL的功能输入的日期时间: NOW()

查看记录: SELECT * FROM [table];

说明记录: EXPLAIN SELECT * FROM [table];

选择记录部分: SELECT [column], [another-column] FROM [table];

计数记录: SELECT COUNT([column]) FROM [table];

计数并选择分组记录: SELECT *, (SELECT COUNT([column]) FROM [table]) AS count FROM [table] GROUP BY [column];

选择特定的记录:SELECT * FROM [table] WHERE [column] = [value];(选择器:<>!=,合并多个选择与ANDOR

含选择的记录[value]SELECT * FROM [table] WHERE [column] LIKE ‘%[value]%‘;

首先是选择的记录[value]SELECT * FROM [table] WHERE [column] LIKE ‘[value]%‘;

首先是选择记录val和结尾ueSELECT * FROM [table] WHERE [column] LIKE ‘[val_ue]‘;

选择一个范围: SELECT * FROM [table] WHERE [column] BETWEEN [value1] and [value2];

与客户订单和唯一的限制选择:SELECT * FROM [table] WHERE [column] ORDER BY [column] ASC LIMIT [value];(排序:DESCASC

更新记录: UPDATE [table] SET [column] = ‘[updated-value]‘ WHERE [column] = [value];

删除记录: DELETE FROM [table] WHERE [column] = [value];

删除所有记录从一个表(不删除表本身): DELETE FROM [table]; (这也将重置自动生成的,就像一个id列列递增计数器。)

删除表中的所有记录: truncate table [table];

删除表列: ALTER TABLE [table] DROP COLUMN [column];

删除表: DROP TABLE [table];

删除数据库: DROP DATABASE [database];

自定义列输出名称: SELECT [column] AS [custom-column] FROM [table];

导出数据库转储(更多信息点击这里mysqldump -u [username] -p [database] > db_backup.sql

使用--lock-tables=false的锁定表(更多信息选项这里)。

导入数据库转储(更多信息点击这里mysql -u [username] -p -h localhost [database] < db_backup.sql

登出: exit;

聚合函数

选择但不重复: SELECT distinct name, email, acception FROM owners WHERE acception = 1 AND date >= 2015-01-01 00:00:00

计算记录总数: SELECT SUM([column]) FROM [table];

算上总数[column]由和组[category-column]SELECT [category-column], SUM([column]) FROM [table] GROUP BY [category-column];

获得最大的价值[column]SELECT MAX([column]) FROM [table];

获取最小值: SELECT MIN([column]) FROM [table];

获取平均值: SELECT AVG([column]) FROM [table];

找圆润的平均值和组[category-column]SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];

多个表

从多个表中选择: SELECT [table1].[column], [table1].[another-column], [table2].[column] FROM [table1], [table2];

结合不同表行: SELECT * FROM [table1] INNER JOIN [table2] ON [table1].[column] = [table2].[column];

从不同的表合并行,但不需要连接条件:SELECT * FROM [table1] LEFT OUTER JOIN [table2] ON [table1].[column] = [table2].[column];(左表是出现在语句中的第一个表。)

使用重命名列或表的别名SELECT [table1].[column] AS ‘[value]‘, [table2].[column] AS ‘[value]‘ FROM [table1], [table2];

用户功能

列出所有用户: SELECT User,Host FROM mysql.user;

创建新用户: CREATE USER ‘username‘@‘localhost‘ IDENTIFIED BY ‘password‘;

授予ALL访问用户为*表:GRANT ALL ON database.* TO ‘user‘@‘localhost‘;

找出Mysql的主机的IP地址

SHOW VARIABLES WHERE Variable_name = ‘hostname‘;

来源于:https://gist.github.com/hofmannsven/9164408#commands

mysql 一些基础的命令