12、不看表中的数据,只看表的结构,有一个命令:
desc 表名;
mysql> desc dept; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | DEPTNO | int(2) | NO | PRI | NULL | |部门编号 | DNAME | varchar(14) | YES | | NULL | |部门名字 | LOC | varchar(13) | YES | | NULL | |地理位置 +--------+-------------+------+-----+---------+-------+ mysql> desc emp; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | EMPNO | int(4) | NO | PRI | NULL | |员工编号 | ENAME | varchar(10) | YES | | NULL | |员工姓名 | JOB | varchar(9) | YES | | NULL | |工作岗位 | MGR | int(4) | YES | | NULL | |上级编号 | HIREDATE | date | YES | | NULL | |入职日期 | SAL | double(7,2) | YES | | NULL | |工资 | COMM | double(7,2) | YES | | NULL | |补助 | DEPTNO | int(2) | YES | | NULL | |部门编号 +----------+-------------+------+-----+---------+-------+ mysql> desc salgrade; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | GRADE | int(11) | YES | | NULL | |工资等级 | LOSAL | int(11) | YES | | NULL | |最低工资 | HISAL | int(11) | YES | | NULL | |最高工资 +-------+---------+------+-----+---------+-------+
describe缩写为:desc
mysql> describe dept;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| DEPTNO | int(2) | NO | PRI | NULL | |
| DNAME | varchar(14) | YES | | NULL | |
| LOC | varchar(13) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
其中要注意:select和from都是关键字。字段名和表名都是标识符。
mysql> SELECT DNAME FROM DEPT;
13、简单查询
13.1、查询一个字段?
select 字段名 from 表名;
其中要注意:select和from都是关键字。字段名和表名都是标识符。
强调:
对于SQL语句来说,是通用的,所有的SQL语句以“;”结尾。另外SQL语句不区分大小写,都行。
查询部门名字?
mysql> select dname from dept;
+------------+
| dname |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)
mysql> SELECT DNAME FROM DEPT;
+------------+
| DNAME |
+------------+
| ACCOUNTING |
| RESEARCH |
| SALES |
| OPERATIONS |
+------------+
4 rows in set (0.00 sec)
13.2、查询两个字段,或者多个字段怎么办?
使用逗号隔开“,”
查询部门编号和部门名?
select deptno,dname from dept;
+--------+------------+
| deptno | dname |
+--------+------------+
| 10 | ACCOUNTING |
| 20 | RESEARCH |
| 30 | SALES |
| 40 | OPERATIONS |
+--------+------------+
13.3、查询所有字段怎么办?
第一种方式:可以把每个字段都写上
select a,b,c,d,e,f... from tablename;
第二种方式:可以使用*
select * from dept;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
这种方式的缺点:1、效率低 2、可读性差。
在实际开发中不建议,可以自己玩没问题。你可以在DOS命令窗口中想快速的看一看全表数据可以采用这种方式。
13.4、给查询的列起别名?
mysql> select deptno,dname as deptname from dept;
+--------+------------+
| deptno | deptname |
+--------+------------+
| 10 | ACCOUNTING |
| 20 | RESEARCH |
| 30 | SALES |
| 40 | OPERATIONS |
+--------+------------+
我的微信
微信号已复制
我的微信
这是我的微信扫一扫