search
HomeDatabaseMysql TutorialHow to implement Scott data mapping in MySQL

How to implement Scott data mapping in MySQL

May 27, 2023 pm 09:28 PM
mysqlscott

    1、SQL

    DROP TABLE IF EXISTS `tb_dept`;
    CREATE TABLE `tb_dept`  (
      `deptno` tinyint(2) UNSIGNED NOT NULL  COMMENT '部门编号',
      `dname` varchar(14) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '部门名称',
      `loc` varchar(13) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '部门地址',
      PRIMARY KEY (`deptno`) USING BTREE
    ) ENGINE = InnoDB  CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    
    INSERT INTO `tb_dept` VALUES (10, 'ACCOUNTING', 'NEW YORK');
    INSERT INTO `tb_dept` VALUES (20, 'RESEARCH', 'DALLAS');
    INSERT INTO `tb_dept` VALUES (30, 'SALES', 'CHICAGO');
    INSERT INTO `tb_dept` VALUES (40, 'OPERATIONS', 'BOSTON');
    
    -------------------------------------------------------------------
    DROP TABLE IF EXISTS `tb_emp`;
    CREATE TABLE `tb_emp`  (
      `empno` int(4) UNSIGNED NOT NULL,
      `ename` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `job` varchar(9) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `mgr` int(4) UNSIGNED  NULL DEFAULT NULL,
      `hiredate` date NULL DEFAULT NULL,
      `sal` decimal(7, 2) NULL DEFAULT NULL,
      `comm` decimal(7, 2) NULL DEFAULT NULL,
      `deptno` tinyint(2) UNSIGNED NULL DEFAULT NULL,
      PRIMARY KEY (`empno`) USING BTREE,
      INDEX `deptno`(`deptno`) USING BTREE,
      CONSTRAINT `tb_emp_ibfk_1` FOREIGN KEY (`deptno`) REFERENCES `tb_dept` (`deptno`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    
    INSERT INTO `tb_emp` VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800.00, NULL, 20);
    INSERT INTO `tb_emp` VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600.00, 300.00, 30);
    INSERT INTO `tb_emp` VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250.00, 500.00, 30);
    INSERT INTO `tb_emp` VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975.00, NULL, 20);
    INSERT INTO `tb_emp` VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250.00, 1400.00, 30);
    INSERT INTO `tb_emp` VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850.00, NULL, 30);
    INSERT INTO `tb_emp` VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450.00, NULL, 10);
    INSERT INTO `tb_emp` VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000.00, NULL, 20);
    INSERT INTO `tb_emp` VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000.00, NULL, 10);
    INSERT INTO `tb_emp` VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500.00, 0.00, 30);
    INSERT INTO `tb_emp` VALUES (7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100.00, NULL, 20);
    INSERT INTO `tb_emp` VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950.00, NULL, 30);
    INSERT INTO `tb_emp` VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000.00, NULL, 20);
    INSERT INTO `tb_emp` VALUES (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300.00, NULL, 10);

    2、Entity class

    2.1、Dept.java

    /**
     * 部门
     * @author HC
     *
     */
    public class Dept {
        /**
         * 部门编号
         */
        private Integer deptno;
        /**
         * 部门名称
         */
        private String dname;
        /**
         * 部门地址
         */
        private String loc;
    
        public Dept() {
        }
    
        public Dept(Integer deptno, String dname, String loc) {
            this.deptno = deptno;
            this.dname = dname;
            this.loc = loc;
        }
    
        public Integer getDeptno() {
            return deptno;
        }
    
        public void setDeptno(Integer deptno) {
            this.deptno = deptno;
        }
    
        public String getDname() {
            return dname;
        }
    
        public void setDname(String dname) {
            this.dname = dname;
        }
    
        public String getLoc() {
            return loc;
        }
    
        public void setLoc(String loc) {
            this.loc = loc;
        }
    
        @Override
        public String toString() {
            return "Dept{" +
                    "deptno=" + deptno +
                    ", dname='" + dname + '\'' +
                    ", loc='" + loc + '\'' +
                    '}';
        }
    }

    2.2、Emp .java

    /**
     * 员工
     * @author HC
     */
    public class Emp {
        /**
         * 员工编号
         */
        private Integer empno;
        /**
         * 员工姓名
         */
        private String ename;
        /**
         * 工作
         */
        private String job;
        /**
         * 上级领导编号
         */
        private Integer mgr;
        /**
         * 受雇日期
         */
        private LocalDate hiredate;
        /**
         * 薪资
         */
        private Double sal;
        /**
         * 奖金
         */
        private Double comm;
        /**
         * 部门编号
         */
        private Integer deptno;
    
        public Emp() {
        }
    
        public Emp(Integer empno, String ename, String job, Integer mgr, LocalDate hiredate, Double sal, Double comm, Integer deptno) {
            this.empno = empno;
            this.ename = ename;
            this.job = job;
            this.mgr = mgr;
            this.hiredate = hiredate;
            this.sal = sal;
            this.comm = comm;
            this.deptno = deptno;
        }
    
        public Integer getEmpno() {
            return empno;
        }
    
        public void setEmpno(Integer empno) {
            this.empno = empno;
        }
    
        public String getEname() {
            return ename;
        }
    
        public void setEname(String ename) {
            this.ename = ename;
        }
    
        public String getJob() {
            return job;
        }
    
        public void setJob(String job) {
            this.job = job;
        }
    
        public Integer getMgr() {
            return mgr;
        }
    
        public void setMgr(Integer mgr) {
            this.mgr = mgr;
        }
    
        public LocalDate getHiredate() {
            return hiredate;
        }
    
        public void setHiredate(LocalDate hiredate) {
            this.hiredate = hiredate;
        }
    
        public Double getSal() {
            return sal;
        }
    
        public void setSal(Double sal) {
            this.sal = sal;
        }
    
        public Double getComm() {
            return comm;
        }
    
        public void setComm(Double comm) {
            this.comm = comm;
        }
    
        public Integer getDeptno() {
            return deptno;
        }
    
        public void setDeptno(Integer deptno) {
            this.deptno = deptno;
        }
    
        @Override
        public String toString() {
            return "Emp{" +
                    "empno=" + empno +
                    ", ename='" + ename + '\'' +
                    ", job='" + job + '\'' +
                    ", mgr=" + mgr +
                    ", hiredate=" + hiredate +
                    ", sal=" + sal +
                    ", comm=" + comm +
                    ", deptno=" + deptno +
                    '}';
        }
    }

    3. Database simulation code

    public class DB {
        private static List<Emp> emps = new ArrayList<>();
        private static List<Dept> depts = new ArrayList<>();
    
        static {
            depts.add(new Dept(10,"ACCOUNTING","NEWYORK"));
            depts.add(new Dept(20,"RESEARCH","DALLAS"));
            depts.add(new Dept(30,"SALES","CHICAGO"));
            depts.add(new Dept(40,"OPERATIONS","BOSTON"));
    
            emps.add(new Emp(7369, "SMITH", "CLERK", 7902,LocalDate.of(1980, 12, 17), 800D, null, 20));
            emps.add(new Emp(7499, "ALLEN", "SALESMAN", 7698, LocalDate.of(1981, 2, 20), 1600D, 300D, 30));
            emps.add(new Emp(7521, "WARD", "SALESMAN", 7698, LocalDate.of(1981, 2, 22), 1250D, 500D, 30));
            emps.add(new Emp(7566, "JONES", "MANAGER", 7893, LocalDate.of(1981, 4, 2), 2975D, null, 20));
            emps.add(new Emp(7654, "MARTIN", "SALESMAN", 7698, LocalDate.of(1981, 9, 28), 1250D, 1400D, 30));
            emps.add(new Emp(7698, "BLAKE", "MANAGER", 7839, LocalDate.of(1981, 5, 1), 2850D, null, 30));
            emps.add(new Emp(7782, "CLARK", "MANAGER", 7839, LocalDate.of(1981, 6, 9), 2450D, 600D, 10));
            emps.add(new Emp(7788, "SCOTT", "ANALYST", 7566, LocalDate.of(1987, 4, 19), 3000D, null, 20));
            emps.add(new Emp(7839, "KING", "PRESIDENT", null, LocalDate.of(1981, 11, 17), 5000D, null, 10));
            emps.add(new Emp(7844, "TURNER", "SALESMAN", 7698, LocalDate.of(1981, 9, 8), 1500D, null, 30));
            emps.add(new Emp(7876, "ADAMS", "CLERK", 7788, LocalDate.of(1987, 5, 23), 1100D, 350D, 20));
            emps.add(new Emp(7900, "JAMES", "CLERK", 7698, LocalDate.of(1981, 12, 3), 950D, null, 30));
            emps.add(new Emp(7902, "FORD", "ANALYST", 7566, LocalDate.of(1981, 12, 3), 3000D, null, 20));
            emps.add(new Emp(7934, "MILLER", "CLERK", 7782, LocalDate.of(1982, 1, 23), 1300D, 400D, 10));
        }
    
        public static List<Emp> getEmps() {
            return emps;
        }
    
        public static List<Dept> getDepts() {
            return depts;
        }
    }

    The above is the detailed content of How to implement Scott data mapping in MySQL. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

    MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

    Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

    ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

    What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

    MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

    MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

    Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

    MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

    Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

    MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

    MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

    MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

    ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

    MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

    ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Hot Tools

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.