本文是本人总结的数据库常用的一些操作命令;
1.新建数据库:
命令:
create database 数据库名;
示例:
mysql> create database frost;
Query OK, 1 row affected (0.00 sec)
2.查看所有的数据库:
命令:
SHOW DATABASES;
示例:
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| flask |
| frost |
| hive |
| mysql |
| performance_schema |
| test |
+--------------------+
7 rows in set (0.00 sec)
3.进入某个数据库:
命令:
use 数据库名;
示例:
mysql> use frost;
Database changed
4.建表语句:
命令:
create table 表名( 字段名1 数据类型 非空 ,字段名2 数据类型 非空 ,字段名3 数据类型 非空 );
示例:
mysql> create table number_rate (name varchar(20), number_all int(10) ,rate float);
Query OK, 0 rows affected (0.04 sec)
5.查看库中的所有表:
命令:
show tables;
示例:
mysql> show tables;
+-----------------+
| Tables_in_frost |
+-----------------+
| number_rate |
+-----------------+
1 row in set (0.00 sec)
6.查看表结构:
命令:
desc 表名
示例:
mysql> desc number_rate;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| number_all | int(10) | YES | | NULL | |
| rate | float | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
7.往表里插入数据:
<1>插入单个字段的情况:
Insert into table_nam(列名) values(值);
示例:
mysql> insert into number_rate (number_all) values(20);
Query OK, 1 row affected (0.00 sec)
<2>插入多个字段的情况
插入两个字段以上:
Insert into table_name(列名1,列名2,列名3)values(值1,值2,值3);
示例:
mysql> insert into number_rate (name,rate) values(2,0.67);
Query OK, 1 row affected (0.01 sec)
插入全部字段:
Insert into table_name values(值1,值2,值3);值要和表中的字段顺序一样;
示例:
mysql> insert into number_rate values(2,29,0.67);
Query OK, 1 row affected (0.02 sec)
<3>使用子查询想标志那个插入数据;
insert into table_name 子查询select语句;//需要注意的是不能违反表table_name的约束条件,以及需要和table_name的字段一致,或者少于它,但是不能多于它。命令:
# 插入子查询结果
INSERT
INTO <表名> [(<属性列1>[,<属性列2>...])
子查询;
示例:
mysql> insert into number_rate (name,number_all,rate) select id,num,rate from user where user_name = '1';
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
8.查看表所有数据:
命令:
select * from 表名
示例:
mysql> select * from number_rate;
+------+------------+------+
| name | number_all | rate |
+------+------------+------+
| NULL | 20 | NULL |
| 1 | NULL | NULL |
| NULL | NULL | 0.67 |
| 2 | NULL | 0.67 |
| 2 | 29 | 0.67 |
| 245 | 13 | 0.21 |
+------+------------+------+
6 rows in set (0.00 sec)
9,查询对应字段
命令:
select 字段名 from 表名;
示例:
mysql> select rate from number_rate;
+------+
| rate |
+------+
| NULL |
| NULL |
| 0.67 |
| 0.67 |
| 0.67 |
| 0.21 |
+------+
6 rows in set (0.00 sec)
10.where特殊条件判断
命令:
Select * from 表名 where 过滤条件;
示例:
mysql> select * from number_rate where name=245;
+------+------------+------+
| name | number_all | rate |
+------+------------+------+
| 245 | 13 | 0.21 |
+------+------------+------+
1 row in set (0.00 sec)
11.between and,在..之间的值
命令:
Select * from 表名 where字段名 between ... and ...;
示例:
mysql> select * from number_rate where name between 1 and 2;
+------+------------+------+
| name | number_all | rate |
+------+------------+------+
| 1 | NULL | NULL |
| 2 | NULL | 0.67 |
| 2 | 29 | 0.67 |
+------+------------+------+
3 rows in set (0.00 sec)
12. In
命令:
Select * from emp where sale in (...,...,...);
示例:
mysql> select * from number_rate where name in (1,2,245) ;
+------+------------+------+
| name | number_all | rate |
+------+------------+------+
| 1 | NULL | NULL |
| 2 | NULL | 0.67 |
| 2 | 29 | 0.67 |
| 245 | 13 | 0.21 |
+------+------------+------+
4 rows in set (0.00 sec)
13.like模糊查询
命令:
select * from emp where ename lilke ‘M%’
查询emp表中ename列中有M的值,M为要查询内容中的模糊信息
%表示多个值,_代表一个字符;M%正则表达式,表示模糊查询信息为M开头的。
%M%:表示查询包含M在内的所有内容
%M_:表示查询M在倒数第二位的所有内容;
示例:
mysql> select * from number_rate where rate like '0.%';
+------+------------+------+
| name | number_all | rate |
+------+------------+------+
| NULL | NULL | 0.67 |
| 2 | NULL | 0.67 |
| 2 | 29 | 0.67 |
| 245 | 13 | 0.21 |
+------+------------+------+
4 rows in set (0.00 sec)
14.distinct 去重:
命令:
select distinct 字段名 from 表名;
示例:
mysql> select distinct rate from number_rate;
+------+
| rate |
+------+
| NULL |
| 0.67 |
| 0.21 |
+------+
3 rows in set (0.00 sec)
15.SQL统计某个字段的数量
select count(字段名) from 表名;
示例:
mysql> select count(rate) from number_rate;
+-------------+
| count(rate) |
+-------------+
| 4 |
+-------------+
1 row in set (0.01 sec)
16.SQL统计某个字段去重后的数量
命令:
select count(distinct 字段名) from 表名;
示例:
mysql> select count( distinct rate) from number_rate;
+-----------------------+
| count( distinct rate) |
+-----------------------+
| 2 |
+-----------------------+
1 row in set (0.00 sec)