花了点时间学完MySQL,打算写下关于MySQL基础博客,一方面巩固一下,另一方面就是希望能够帮助需要的人
mysql-5.5.36-win32.msi
mysql默认端口3306(建议修改,易被攻击)
mysql -uroot -p密码;
- select
- insert、delete、update(增删改针对表中数据)
- create、drop、alter(增删改针对表的结构)
- commit(提交)、rollback(回滚)
- grant、revoke
create database t1;#创建数据库
use t1;#指定使用t1数据库
source C:\Users\Administrator\Desktop\bwork.sql#将bwork这个sql文件放入t1数据库中
show tables;#查看当前数据库中所有表格目录
desc dept;#查看dept表的表结构(表结构不是表)
select database();#查看当前用的是哪个数据库
show create table dept;#查看dept表的创建语句
show tables from test1;#当前在t1库中,想查看test1库中的表目录
先看一下表目录
mysql> use t1
Database changed
mysql> show tables;
+————–+
| Tables_in_t1 |
+————–+
| dept |
| emp |
| salgrade |
+————–+
3 rows in set (0.00 sec)
dept部门表
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 | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
emp员工表
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 | |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.03 sec)
salgrade工资表
mysql> desc salgrade;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| GRADE | int(11) | YES | | NULL | |
| LOSAL | int(11) | YES | | NULL | |
| HISAL | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql> select ename from emp;
+--------+
| ename |
+--------+
| SMITH |
| ALLEN |
| WARD |
| JONES |
| MARTIN |
| BLAKE |
| CLARK |
| SCOTT |
| KING |
| TURNER |
| ADAMS |
| JAMES |
| FORD |
| MILLER |
+--------+
14 rows in set (0.00 sec)
mysql> select ename,empno from emp;
+--------+-------+
| ename | empno |
+--------+-------+
| SMITH | 7369 |
| ALLEN | 7499 |
| WARD | 7521 |
| JONES | 7566 |
| MARTIN | 7654 |
| BLAKE | 7698 |
| CLARK | 7782 |
| SCOTT | 7788 |
| KING | 7839 |
| TURNER | 7844 |
| ADAMS | 7876 |
| JAMES | 7900 |
| FORD | 7902 |
| MILLER | 7934 |
+--------+-------+
14 rows in set (0.00 sec)
mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)
mysql> select empno,ename,sal*12 as yearsal from emp;(sal*12被yearsal代替,但底层表没变,只是显示改变了)
+-------+--------+----------+
| empno | ename | yearsal |
+-------+--------+----------+
| 7369 | SMITH | 9600.00 |
| 7499 | ALLEN | 19200.00 |
| 7521 | WARD | 15000.00 |
| 7566 | JONES | 35700.00 |
| 7654 | MARTIN | 15000.00 |
| 7698 | BLAKE | 34200.00 |
| 7782 | CLARK | 29400.00 |
| 7788 | SCOTT | 36000.00 |
| 7839 | KING | 60000.00 |
| 7844 | TURNER | 18000.00 |
| 7876 | ADAMS | 13200.00 |
| 7900 | JAMES | 11400.00 |
| 7902 | FORD | 36000.00 |
| 7934 | MILLER | 15600.00 |
+-------+--------+----------+
1、查询工资为5000的员工
mysql> select empno,ename,sal from emp where sal=5000;
+-------+-------+---------+
| empno | ename | sal |
+-------+-------+---------+
| 7839 | KING | 5000.00 |
+-------+-------+---------+
1 row in set (0.00 sec)
2、找出工资大于1800,且要求部门编号是20或30的
mysql> select empno,ename,sal,deptno from emp where sal>1800 and(deptno=20 or deptno=30);
+-------+-------+---------+--------+
| empno | ename | sal | deptno |
+-------+-------+---------+--------+
| 7566 | JONES | 2975.00 | 20 |
| 7698 | BLAKE | 2850.00 | 30 |
| 7788 | SCOTT | 3000.00 | 20 |
| 7902 | FORD | 3000.00 | 20 |
+-------+-------+---------+--------+
3、找出名字中含义o的员工
mysql> select empno,ename from emp where ename like '%o%';
+-------+-------+
| empno | ename |
+-------+-------+
| 7566 | JONES |
| 7788 | SCOTT |
| 7902 | FORD |
+-------+-------+
3 rows in set (0.00 sec)
默认升序asc
降序——>最后加desc
mysql> select ename,sal from emp order by sal;
+--------+---------+
| ename | sal |
+--------+---------+
| SMITH | 800.00 |
| JAMES | 950.00 |
| ADAMS | 1100.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
| MILLER | 1300.00 |
| TURNER | 1500.00 |
| ALLEN | 1600.00 |
| CLARK | 2450.00 |
| BLAKE | 2850.00 |
| JONES | 2975.00 |
| FORD | 3000.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
+--------+---------+
按工资降序排,若工资相同按名字字段升序排
mysql> select ename,sal from emp order by sal desc,ename asc;
+--------+---------+
| ename | sal |
+--------+---------+
| KING | 5000.00 |
| FORD | 3000.00 |
| SCOTT | 3000.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| ALLEN | 1600.00 |
| TURNER | 1500.00 |
| MILLER | 1300.00 |
| MARTIN | 1250.00 |
| WARD | 1250.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| SMITH | 800.00 |
+--------+---------+
14 rows in set (0.00 sec)
数据库之间不通用
mysql> select lower(ename) from emp;
+--------------+
| lower(ename) |
+--------------+
| smith |
| allen |
| ward |
| jones |
| martin |
| blake |
| clark |
| scott |
| king |
| turner |
| adams |
| james |
| ford |
| miller |
+--------------+
14 rows in set (0.05 sec)
mysql> select upper(ename) from emp;
+--------------+
| upper(ename) |
+--------------+
| SMITH |
| ALLEN |
| WARD |
| JONES |
| MARTIN |
| BLAKE |
| CLARK |
| SCOTT |
| KING |
| TURNER |
| ADAMS |
| JAMES |
| FORD |
| MILLER |
+--------------+
14 rows in set (0.00 sec)
mysql> select substr(ename,1,2) from emp;
+-------------------+
| substr(ename,1,2) |
+-------------------+
| SM |
| AL |
| WA |
| JO |
| MA |
| BL |
| CL |
| SC |
| KI |
| TU |
| AD |
| JA |
| FO |
| MI |
+-------------------+
14 rows in set (0.00 sec)
mysql> select length(ename) from emp;
+---------------+
| length(ename) |
+---------------+
| 5 |
| 5 |
| 4 |
| 5 |
| 6 |
| 5 |
| 5 |
| 5 |
| 4 |
| 6 |
| 5 |
| 5 |
| 4 |
| 6 |
+---------------+
14 rows in set (0.00 sec)
mysql> select ename from emp where ename=trim(' smith ');
+-------+
| ename |
+-------+
| SMITH |
+-------+
1 row in set (0.01 sec)
mysql> select round(123.456);
+----------------+
| round(123.456) |
+----------------+
| 123 |
+----------------+
1 row in set (0.00 sec)
mysql> select round(rand()*100);
+-------------------+
| round(rand()*100) |
+-------------------+
| 81 |
+-------------------+
1 row in set (0.00 sec)
mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)
mysql> select ename,ifnull(comm,0) from emp;
+--------+----------------+
| ename | ifnull(comm,0) |
+--------+----------------+
| SMITH | 0.00 |
| ALLEN | 300.00 |
| WARD | 500.00 |
| JONES | 0.00 |
| MARTIN | 1400.00 |
| BLAKE | 0.00 |
| CLARK | 0.00 |
| SCOTT | 0.00 |
| KING | 0.00 |
| TURNER | 0.00 |
| ADAMS | 0.00 |
| JAMES | 0.00 |
| FORD | 0.00 |
| MILLER | 0.00 |
+--------+----------------+
14 rows in set (0.00 sec)
mysql> select sal+ifnull(comm,100) from emp;
+----------------------+
| sal+ifnull(comm,100) |
+----------------------+
| 900.00 |
| 1900.00 |
| 1750.00 |
| 3075.00 |
| 2650.00 |
| 2950.00 |
| 2550.00 |
| 3100.00 |
| 5100.00 |
| 1500.00 |
| 1200.00 |
| 1050.00 |
| 3100.00 |
| 1400.00 |
+----------------------+
14 rows in set (0.05 sec)
每一个数据库处理日期时,采用的机制都不同。实际开发中,表中的字段定义为DATE类型很少,因为一旦使用日期类型,那么JAVA程序将不通用。在实际开发中,一般使用“日期字符串”来表示日期
MySQL日期格式
%a 缩写星期名
%b 缩写月名
%c 月,数值
%D 带有英文前缀的月中的天
%d 月的天,数值(00-31)
%e 月的天,数值(0-31)
%f 微秒
%H 小时 (00-23)
%h 小时 (01-12)
%I 小时 (01-12)
%i 分钟,数值(00-59)
%j 年的天 (001-366)
%k 小时 (0-23)
%l 小时 (1-12)
%M 月名
%m 月,数值(00-12)
%p AM 或 PM
%r 时间,12-小时(hh:mm:ss AM 或 PM)
%S 秒(00-59)
%s 秒(00-59)
%T 时间, 24-小时 (hh:mm:ss)
%U 周 (00-53) 星期日是一周的第一天
%u 周 (00-53) 星期一是一周的第一天
%V 周 (01-53) 星期日是一周的第一天,与 %X 使用
%v 周 (01-53) 星期一是一周的第一天,与 %x 使用
%W 星期名
%w 周的天 (0=星期日, 6=星期六)
%X 年,其中的星期日是周的第一天,4 位,与 %V 使用
%x 年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y 年,4 位
%y 年,2 位
5.Java日期格式
yyyy 年
MM 月
dd 日
HH 时
mm 分
ss 秒
Java中将字符串——>日期类型
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=sdf.parse("1970-10-10");
1、查询1980-12-17入职的员工(hiredate只接收date类型)
mysql> select
-> ename,hiredate
-> from
-> emp
-> where
-> hiredate=str_to_date('1980-12-17','%Y-%m-%d');
+-------+------------+
| ename | hiredate |
+-------+------------+
| SMITH | 1980-12-17 |
+-------+------------+
1 row in set (0.00 sec)
查询员工的入职日期,以’10-12-1980’格式显示到窗口
mysql> select
-> ename,date_format(hiredate,'%m-%d-%Y')
-> from
-> emp;
+--------+----------------------------------+
| ename | date_format(hiredate,'%m-%d-%Y') |
+--------+----------------------------------+
| SMITH | 12-17-1980 |
| ALLEN | 02-20-1981 |
| WARD | 02-22-1981 |
| JONES | 04-02-1981 |
| MARTIN | 09-28-1981 |
| BLAKE | 05-01-1981 |
| CLARK | 06-09-1981 |
| SCOTT | 04-19-1987 |
| KING | 11-17-1981 |
| TURNER | 09-08-1981 |
| ADAMS | 05-23-1987 |
| JAMES | 12-03-1981 |
| FORD | 12-03-1981 |
| MILLER | 01-23-1982 |
+--------+----------------------------------+
14 rows in set (0.00 sec)