MYSQL数据库的增删改以及查
什么是数据库???
——存储数据的仓库
1、建库
drop database if exists DATABASE_NAME; //删除数据库(若存在)
create database DATABASE_NAME; //创建数据库
use DATABASE_NAME; //进入数据库
2、数据库中可以以表的方式存储数据,便于我们对其进行增删改查的操作
以下面两张表为例,进行增删改查的操作
DROP DATABASE IF EXISTS test1; CREATE DATABASE test1; USE test1; ##部门表 #DROP IF EXISTS TABLE DEPT; CREATE TABLE DEPT( DEPTNO int PRIMARY KEY,##部门编号 DNAME VARCHAR(14) , ##部门名称 LOC VARCHAR(13) ##部门地址 ) ; INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK'); INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS'); INSERT INTO DEPT VALUES (30,'SALES','CHICAGO'); INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON'); ##员工表 #DROP IF EXISTS TABLE EMP; CREATE TABLE EMP( EMPNO int PRIMARY KEY, #员工编号 ENAME VARCHAR(10), #员工姓名 JOB VARCHAR(9), #员工工作 MGR int, #员工直属领导编号 HIREDATE DATE, #入职时间 SAL double, #工资 COMM double, #奖金 DEPTNO int #对应dept表的外键 ); ## 添加 部门 和 员工 之间的主外键关系 ALTER TABLE EMP ADD CONSTRAINT FOREIGN KEY EMP(DEPTNO) REFERENCES DEPT (DEPTNO); INSERT INTO EMP VALUES(7369,'SMITH','CLERK',7902,"1980-12-17",800,NULL,20); INSERT INTO EMP VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30); INSERT INTO EMP VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30); INSERT INTO EMP VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20); INSERT INTO EMP VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30); INSERT INTO EMP VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30); INSERT INTO EMP VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10); INSERT INTO EMP VALUES(7788,'SCOTT','ANALYST',7566,'1987-07-03',3000,NULL,20); INSERT INTO EMP VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10); INSERT INTO EMP VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30); INSERT INTO EMP VALUES(7876,'ADAMS','CLERK',7788,'1987-07-13',1100,NULL,20); INSERT INTO EMP VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30); INSERT INTO EMP VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20); INSERT INTO EMP VALUES(7934,'MILLER','CLERK',7782,'1981-01-23',1300,NULL,10);
增:包括对表的增加和对表内数据的增加
1)create table TEST2(FIELD...);
2) a.单条数据 insert into EMP(ENAME) values('ZHANG');
b.多条数据复制于已知表
create table TEST(TEST VARCHAR(10));
insert into TEST(TEST) select ENAME from EMP;
c.一边创表一边代入数据 create table if not exists TEST2(select ENAME from EMP);
删:包括对表的删除和表内数据的删除
1)drop table TEST;
2)删除数据 delete from TEST2 where ENAME='ZHANG';
注:有一种不可恢复的删除方法,可以删除表内所有内容 truncate table TEST2;
改:可以对表的内容进行修改 update EMP set ENAME='SMITHHH' where ENAME='SMITH';
查:包括简单查询和复杂查询
1)简单查询一般为单表查询
a.例如查询部门30中的员工的详细信息
select * from EMP where DEPTNO=30;
b.例如查询从事clerk工作的员工编号,姓名,部门号
select EMPNO,ENAME,DEPTNO from EMP where JOB='clerk';
2)复杂查询
a.例如查询拥有员工的部门名和部门号
select D.DNAME,D.DEPTNO from DEPT D RIGHT join EMP E on E.DEPTNO=D.DEPTNO where E.ENAME is not null;
b.查询员工和所属经理的名称
select (A.ENAME) MANAGER,B.ENAME from ( select E.ENAME,E.DEPTNO from EMP E where E.JOB='MANAGER' )A inner join ( select E.ENAME,E.DEPTNO from EMP E where E.JOB<>'MANAGER' and E.JOB<>'PRESIDENT' )B on A.DEPTNO=B.DEPTNO;