一、MySQL 常规命令
1)显示当前数据库的版本号和日期。
MariaDB [(none)]> select version(),current_date();
+----------------+----------------+
| version() | current_date() |
+----------------+----------------+
| 5.5.60-MariaDB | 2018-08-29 |
+----------------+----------------+
1 row in set (0.00 sec)
2)通过 AS 关键字设置字段名。
SELECT VERSION() AS name; //可设置中文,通过单引号
select 字段 as 字段别名;
例如:select version() as \'版本号\';
MariaDB [(none)]> select version() as \'版本号\';
+----------------+
| 版本号 |
+----------------+
| 5.5.60-MariaDB |
+----------------+
1 row in set (0.00 sec)
3)通过 SELECT 执行返回计算结果
MariaDB [(none)]> select (20+5)*4;
+----------+
| (20+5)*4 |
+----------+
| 100 |
+----------+
1 row in set (0.00 sec)
4)通过多行实现数据库的使用者和日期
MariaDB [(none)]> select
-> user()
-> ,
-> now()
-> ;
+----------------+---------------------+
| user() | now() |
+----------------+---------------------+
| root@localhost | 2018-08-29 21:59:39 |
+----------------+---------------------+
1 row in set (0.00 sec)
5)通过一行显示数据库使用者和日期
MariaDB [(none)]> select user();select now();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
+---------------------+
| now() |
+---------------------+
| 2018-08-29 22:01:49 |
+---------------------+
1 row in set (0.00 sec)
6)命令的取消
MariaDB [(none)]> show database
-> \\c
7)MySQL 窗口的退出
MariaDB [(none)]> exit;
Bye
[root@centos ~]#
MySQL 常用数据类型
- 整数型:TINYINT,SMALLINT,INT,BIGINT
- 浮点型:FLOAT,DOUBLE,DECIMAL(M,D)
- 字符型:CHAR,VARCHAR
- 日期型:DATETIME,DATE,TIMESTAMP
- 备注型:TINYTEXT,TEXT,LONGTEXT
二、MySQL 数据库操作
1)显示当前存在的数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
2)选择你所需要的数据库
MariaDB [(none)]> use test;
Database changed
3)查看当前所选择的数据库
MariaDB [test]> select database();
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
4)根据数据库设置中文编码
MariaDB [test]> SET NAMES gbk; --set names utf8;
Query OK, 0 rows affected (0.00 sec)
5)查看有多少张表
MariaDB [test]> show tables;
Empty set (0.00 sec)
6)查看一张表的所有内容
MariaDB [test]> select now() from dual;
+---------------------+
| now() |
+---------------------+
| 2018-08-29 22:16:05 |
+---------------------+
1 row in set (0.00 sec)
7)创建一个数据库
MariaDB [test]> create database book;
Query OK, 1 row affected (0.00 sec)
8)在数据库里创建一张表
MariaDB [book]> create table users(
-> username varchar(20) not null,
-> sex char(1),
-> birth datetime
-> );
Query OK, 0 rows affected (0.01 sec)
9)显示表的结构
MariaDB [book]> desc users; --describe users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(20) | NO | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
10)给表插入一条数据
MariaDB [book]> INSERT INTO users (username,sex,birth) VALUES (\'Lee\',\'x\',NOW());
Query OK, 1 row affected (0.00 sec)
11)筛选指定的数据
MariaDB [book]> SELECT * FROM users WHERE username = \'Lee\';
+----------+------+---------------------+
| username | sex | birth |
+----------+------+---------------------+
| Lee | x | 2018-08-29 22:20:38 |
+----------+------+---------------------+
1 row in set (0.00 sec)
12)修改指定的数据
MariaDB [book]> UPDATE users SET sex = \'男\' WHERE username=\'Lee\';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
13)删除指定的数据
MariaDB [book]> DELETE FROM users WHERE username=\'Lee\';
Query OK, 1 row affected (0.00 sec)
14)按指定的数据排序
MariaDB [book]> SELECT * FROM users ORDER BY birth DESC; --正序
Empty set (0.00 sec)
15)删除指定的表
MariaDB [book]> DROP TABLE users;
Query OK, 0 rows affected (0.00 sec)
MariaDB [book]> show tables;
Empty set (0.00 sec)
16)删除指定的数据库
MariaDB [book]> drop database book;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
三、MySQL 常用函数
文本函数
数字函数
日期和时间函数
其它:MySQL 数据库常用命令小结
转:MySQL数据库常用操作