《SQL Cookbook》学习笔记 —— 数据准备

1. 创建EMP表

CREATE TABLE `EMP` (
  `EMPNO` int NOT NULL,
  `ENAME` varchar(45) DEFAULT NULL,
  `JOB` varchar(45) DEFAULT NULL,
  `MGR` int DEFAULT NULL,
  `HIREDATE` date DEFAULT NULL,
  `SAL` int DEFAULT NULL,
  `COMM` int DEFAULT NULL,
  `DEPTNO` int DEFAULT NULL,
  PRIMARY KEY (`EMPNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

字段含义说明:

  1. MGR:该员工上级领导的EMPNO
  2. SAL:工资
  3. COMM:奖金

2. 创建DEPT表

CREATE TABLE `DEPT` (
  `DEPTNO` int NOT NULL,
  `DNAME` varchar(45) DEFAULT NULL,
  `LOC` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`DEPTNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

3. 向EMP表中插入数据

INSERT INTO EMP VALUES
        (7369, 'SMITH',  'CLERK',     7902,   str_to_date('12/17/1980', '%m/%d/%Y'),  800,   NULL,  20);
INSERT INTO EMP VALUES
        (7499, 'ALLEN',  'SALESMAN',  7698,   str_to_date('2/20/1981', '%m/%d/%Y'),   1600,  300,   30);
INSERT INTO EMP VALUES
        (7521, 'WARD',   'SALESMAN',  7698,   str_to_date('2/22/1981', '%m/%d/%Y'),   1250,  500,   30);
INSERT INTO EMP VALUES
        (7566, 'JONES',  'MANAGER',   7839,   str_to_date('4/2/1981', '%m/%d/%Y'),    2975,  NULL,  20);
INSERT INTO EMP VALUES
        (7654, 'MARTIN', 'SALESMAN',  7698,   str_to_date('9/28/1981', '%m/%d/%Y'),   1250,  1400,  30);
INSERT INTO EMP VALUES
        (7698, 'BLAKE',  'MANAGER',   7839,   str_to_date('5/1/1981', '%m/%d/%Y'),    2850,  NULL,  30);
INSERT INTO EMP VALUES
        (7782, 'CLARK',  'MANAGER',   7839,   str_to_date('6/9/1981', '%m/%d/%Y'),    2450,  NULL,  10);
INSERT INTO EMP VALUES
        (7788, 'SCOTT',  'ANALYST',   7566,   str_to_date('12/9/1982', '%m/%d/%Y'),   3000,  NULL,  20);
INSERT INTO EMP VALUES
        (7839, 'KING',   'PRESIDENT', NULL,   str_to_date('11/17/1981', '%m/%d/%Y'),  5000,  NULL,  10);
INSERT INTO EMP VALUES
        (7844, 'TURNER', 'SALESMAN',  7698,   str_to_date('9/8/1981', '%m/%d/%Y'),    1500,  0,     30);
INSERT INTO EMP VALUES
        (7876, 'ADAMS',  'CLERK',     7788,   str_to_date('1/12/1983', '%m/%d/%Y'),   1100,  NULL,  20);
INSERT INTO EMP VALUES
        (7900, 'JAMES',  'CLERK',     7698,   str_to_date('12/3/1981', '%m/%d/%Y'),   950,   NULL,  30);
INSERT INTO EMP VALUES
        (7902, 'FORD',   'ANALYST',   7566,   str_to_date('12/3/1981', '%m/%d/%Y'),   3000,  NULL,  20);
INSERT INTO EMP VALUES
        (7934, 'MILLER', 'CLERK',     7782,   str_to_date('1/23/1982', '%m/%d/%Y'),   1300,  NULL,  10);

4. 向DEPT表中插入数据

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');

5. 创建数据透视表 T1、T10、T100和T500,并插入数据

CREATE TABLE `T1` (
  `ID` int NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

insert into T1 values (1);
CREATE TABLE `T10` (
  `ID` int NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO T10 VALUES (1);
INSERT INTO T10 VALUES (2);
INSERT INTO T10 VALUES (3);
INSERT INTO T10 VALUES (4);
INSERT INTO T10 VALUES (5);
INSERT INTO T10 VALUES (6);
INSERT INTO T10 VALUES (7);
INSERT INTO T10 VALUES (8);
INSERT INTO T10 VALUES (9);
INSERT INTO T10 VALUES (10);

T100与T500的创建过程省略

你可能感兴趣的:(SQL,Cookbook学习笔记,sql)