zoukankan      html  css  js  c++  java
  • java & SQL

     !!!!!!!

    如果在程序中执行mysql语句放回的结果有误!!而在terminal中执行mysql没错

    那么:

    ①、如果是中文问题,首先要在mysql中设置编码格式,utf-8.。其次在连接mysql的string中,要加上一些charset=xxx之类的东西。

    string connString = "server=localhost;user id=root;password=123456;database=flightsys;Charset=utf8";
            
    

    SQL_procedure

    DELIMITER $
    CREATE PROCEDURE AlreadyExist (IN C_name VARCHAR(16), OUT res INT)
    BEGIN
        SET res=0;
        if EXISTS (select customer.c_name from customer where customer.c_name = C_name) then 
        SET res=1;
      END IF;
    END;
    $
    DELIMITER $
    CREATE PROCEDURE LeftSeat (IN P_NAME VARCHAR(16), IN TYPE VARCHAR(16), OUT res INT)
    BEGIN
        SET res=0;
    
        set res = (select seat.count from seat where seat.p_name = P_NAME and seat.type = TYPE) - 
                            (select count(*) from ticket_order where p_name = P_NAME and type = TYPE);
         
    
    END;
    $
    还不确定

    忘记密码怎么办:------

    http://blog.csdn.net/u012730299/article/details/51840416

    在cmd命令中,控制sql

    大多数语句都要用分号结束。

    SQL的关键字不区分大小写

    SQL语言中,字符串的值是区分大小写的, 并且包含在单引号中。

    SQL的判断是否相等的符合是直接一个 = ,而不是==

    1、开启mysql服务

    2、登录mysql 

    mysql -uroot -p

    3、输入use mysql;

    4、创建用户名为scott,密码为tiger的账户,输入

    create user 'scott' @ 'localhost' identified by 'tiger';

    5、赋予特权给scott,输入

    grant select, insert,  update, delete, create, create view, drop, execute, references on *.* to 'scott'@'localhost';

    6、如果希望该帐号可以从任意的IP地址来远程访问,输入

    grant all privileges on *.* to 'scott'@'%' identified by 'tiger';

    7、如果希望该帐号从一个特定的IP地址可以进行远程访问,输入

    grant all privileges on *.* 'scott'@'ipAddress' identified by 'tiger';

    8、登录,mysql -uscott -ptiger

    然后创建一个数据库,名为javabook,create database javabook;

    转到javabook这个数据库,use javabook;

    创建一个表,

    create table Course (
    	courseId char(5),
    	subjectId char(4) not null,
    	courseNumber integer,
    	title varchar(50) not null,
    	numOfCredits integer,
    	primary key (courseId)
    );
    

     插入东西:

    insert into Course (courseId, subjectId, courseNumber, title, numOfCredits)
    values ('11113', 'CSCI', '3720', 'Database System', 3);
    

    修改:

    update Course
    set numOfCredits = 4
    where title = 'Database Systems';
    

    删除表中某一个

    delete from Course
    where title = 'Database Systems';
    

     删除表中的所有东西,不代表删除了整个表

    delete from Course;
    

    删除整个表::

    drop table Course;
    

    同理删除整个数据库:

    drop database javabook;
    

      

     搜索表中的东西

     select courseId, subjectId, courseNumber
     from Course
     where courseId = '11113';
    

    -----------------------------------------------------------------------------------------------

    2017年10月11日 14:21:20

    为一个表再增加一个属性,alter table teacher add salary int

    删除一个表的一个属性, alter table teacher drop liuweiming

    更改表的名字:  ALTER table teacher rename teachers;

    ------所以修改表的属性名称之类的,用的是alter
    更新一个行的某一个属性, UPDATE teacher set salary = '5555' WHERE teacher_id = '2016'

    修改行的值的,用的是update

    SQL语句的group by后,你按那个属性group了,以后的select和where只能出现那些属性,不然识别不了。

    除非你把它写在聚集函数上,还是可以得。

    但是这个不知道为啥可以

    SELECT *
    from instructor
    GROUP BY instructor.dept_name
    having MAX(instructor.salary) = instructor.salary;
    View Code

     不过结果是错误的。

    /*
    SELECT
        T.a
    FROM 
        (
            SELECT
                MAX(salary)
            FROM
                instructor
            GROUP BY
                dept_name
        ) AS T (a);
    */
    错误,版本不支持
    SELECT
        MIN(T.maxSalary)
    FROM 
        (
            SELECT
                MAX(salary) as maxSalary
            FROM
                instructor
            GROUP BY
                dept_name
        ) AS T;
    正确

    ------------------------------------------------------------2017年11月29日 23:42:32-------------------------------------------------------

    MYSQL触发器的用法

    第一句用DELIMITER $

     因为BEGIN中有了分号就自动结束,那么用那句话,就是把分号临时改变为$,使得系统碰到分号不会结束。

    据说最后还需要用DELIMITER 清除,但是 naivecat报错

    https://www.cnblogs.com/CraryPrimitiveMan/p/4206942.html

    下面那个是错误的,判断不存在这个项,select出来的不是和NULL比较,是要用not exists

    DELIMITER $
    CREATE trigger my BEFORE INSERT on student for each row
    BEGIN
        DECLARE cnt int DEFAULT(0);
        -- IFNULL((select class.classId from class), INSERT into class VALUES(new.classId, 0));
        if (select class.classId from class where class.classId = new.classId) = NULL
        then INSERT into class VALUES(new.classId, 0);
        end if;
        set cnt = (select class.studentNumber from class where class.classId = new.classId);
        UPDATE class set class.studentNumber = cnt + 1 where class.classId = new.classId;
    
    END;

    下面的正确

    DELIMITER $
    CREATE trigger my BEFORE INSERT on student for each row
    BEGIN
        DECLARE cnt int DEFAULT(0);
        if not exits (select class.classId from class where class.classId = new.classId)
        then INSERT into class VALUES(new.classId, 0);
        END IF;
        set cnt = (select class.studentNumber from class where class.classId = new.classId);
        UPDATE class set class.studentNumber = cnt + 1 where class.classId = new.classId;
    
    END;

    正则覆盖

    假设F是关系R上的函数依赖集合,F+是它的闭包。FC是函数F的正则覆盖,FC+是它的闭包。

    如果FC是函数F的正则覆盖,则有FC+ = F+

    怎么找FC,就在于,在F中有一个依赖a à b的话,如果我说a中有一个属性A是没用的。也就是(a - A) à b也可以成立,那么就可以把这个依赖缩小一个属性。那么就需要证明它是没用的。

    书上的是:

    在函数依赖集 F中,如果F逻辑蕴含 ( F – (a à b) ) ∪ ((a - A) à b ),则可以

    第二种,如果我说b中有一个属性A是没用的,也就是 a à (b - A)也可以成立。需要证明

    书上的是:

    在函数依赖集(F – (a à b)) ∪ (a à (b - A)) 能逻辑蕴含 F,则可以。

    总结就是两个方向相反的证明

    简单证明的方法是:

    对于第一种

    求出属性的闭包(a - A)+

    看看是否能包含b,那么属性的闭包要怎么求?

    FC中求,而不是在F中求。

    对于第二种,也是求 属性a+注意这个时候是在FC中求其属性的闭包,也就是这个时候这一条函数依赖(a à b属性中的A) 是先不用的。(a à b中其他属性) 还可以用

    比如 

    a --> bc

    b --> c

    a --> b

    ab --> c

    的一个正则覆盖是

    a --> b 和 b --> c

    正则覆盖不唯一

    -----------------------------------------------------------------------------------------------------------------------------

    超码:能确定所有属性的

    候选码:能确定所有属性,并且它的任何真子集都不能确定所有属性,所以长度不唯一。

    2018年1月17日 01:16:35

    1、任何满足BCNF的都满足3NF

     3NF

    --------------------------------------------------------------------------------------------------------------------------------------

    1、任何满足BCNF的都满足3NF

    对于关系表R(A,B,C…Z)来说

    BCNF的判定标准

    1、α—> β是一个平凡的函数依赖

    2、α是R的一个超码

    分解算法:

    若一个表不满足BCNF,则需分解,分解步骤如下:

    感觉BCNF在理论上分解是不可行的,因为其需要计算F+来判断,因为用原来的函数依赖来判断一个表是否满足BCNF,很有可能原来的函数依赖中没有任何一条依赖在这个表中体现出来,这个时候可能误认为其符合BCNF,但有可能在F+中有体现。P196页也有说明必须求出F+才能判断这个表是否属于BCNF。

    ★、有一个快速判断表是否属于BCNF的方法,不需要求F+

             对于需求判定的表Ri,其每一个属性子集α,确保α+(在原本函数依赖下的闭包)要不包含Ri – α的任何属性,要么包含Ri的全部属性。

    真正的分解算法很简单,如果一个函数不满足BCNF的判定条件,则分解成两个表。

    一个是Rleft = (α∪β),Rright = (Ri – (β - α))

    对于关系表R(A,B,C…Z)来说

    3NF的判定标准(前两条和BCNF的一样)

    1、α—> β是一个平凡的函数依赖

    2、α是R的一个超码。

    3、β – α的每个属性A都包含于R的候选码中。

    注意第三个条件并没有说每个属性A都包含于同一个候选码中,可以包含于不同的候选码中。

    分解算法:

    若一个表不满足3NF,则需分解,分解步骤如下:

    ①、求出FC,正则覆盖

    ②、对于每一个FC里面的函数(α—> β),都开一个新表,Ri = (α∪β)

             如果Ri不包含R的候选码(所以有一个包含就可以

                       Then:增加一个新表,Rnew= (R的任意一个候选码)

             最后要删除多余的表,因为有一些表是包含另外一个表的。

             (所以这里是最多循环一次,要么增加表,要么不增加)

    --------------------------------------------------------------------------------------------------------------------------------------

    无损分解:如果R分解成两个表R1和R2,如果是无损分解的,则需要R1∩R2,是R1的超码或者是R2的超码。

     如何判断一个分解算法是否有保持函数依赖?

    1、判断原来的函数依赖集F,如果每条F都可以再分解得到的某一个关系上验证,那么这个分解是保持依赖的。

    验证就是,比如有A --> D,那么在R(A,B,C,D)这个表上已经验证了。

    如果不能,也不能说他不保持依赖。//这个还不太懂。

    通解:

    需要验证:

    在F上的每一个a-->b(函数依赖)

    result = a

    do {

      for each 分解后的Ri

        t = (result ∩ Ri)的闭包(在F下的闭包) ∩ Ri

        result = result ∪ t

    } while (result还能变化)

    如果result包含b中的所有属性,则函数依赖a-->b保持,分解保持依赖当且仅当上述过程中F的所有依赖都保持。P195

    --------------------------------------------------------------------------2017年12月22日 14:28:34---------------------------------------------

    1、sql查看用户权限 show grants for liuweiming@'localhost';

    2、可以用status查看当前用户的状态

    --------------------------------------2018年1月6日 20:14:05------------------------------------------------------------

    jdbc调用数据库

    1、去mysql官网下载驱动。

    2、导入去项目中,具体操作是:

     

     然后添加一个lib文件,然后把jdbc驱动复制去那里,然后配置一下:

    点击library,addJars那里选中jdbc即可。

    问题一:连接不上数据库,抛出异常错误。我是按照那个菜鸟教程那里弄的,然后连接不上去。

    百度了一下,要在URL哪里添加上:

    ?serverTimezone=UTC

    问题二:工程编译不了,运行的结果与上一次相同,怎样都相同。

    原因:导入了某一些包后,又删除了。这样会GG的。结论就是要把所有包都删了,然后重新导入需要的包。

    package com.datebase;
    
    import java.sql.*;
    
    public class DbUtil {
        private static final String URL = "jdbc:mysql://localhost:3306/liuweiming?serverTimezone=UTC";
        private static final String USER = "root";
        private static final String PASSWORD = "请输入密码";
        private static Connection connection = null;
        static { //只加载一次,以后都不加载
            try {
                //1.加载驱动程序
                Class.forName("com.mysql.cj.jdbc.Driver");
                //2. 获得数据库连接
                connection = DriverManager.getConnection(URL, USER, PASSWORD);
            } catch (SQLException e) {
                System.out.println("DbUtil22" + "   " + e.getMessage());
                System.out.println(PASSWORD);
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static Connection getConnection() {
            return connection;
        }
    }
    View Code
    package com.datebase;
    import java.util.List;
    public class Main {
        public static void main(String[] args) {
            StudentDao studentDao = new StudentDao();
            try {
                studentDao.addStudent(new Student("201527010414", "liuweiming"));
                List<Student> res = studentDao.select();
                for (Student i : res) {
                    System.out.println(i.getStudentID() + "  " + i.getStudentName());
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }
    View Code
    package com.datebase;
    
    public class Student {
        private String studentID, studentName;
    
        public String getStudentID() {
            return studentID;
        }
    
        public void setStudentID(String studentID) {
            this.studentID = studentID;
        }
    
        public String getStudentName() {
            return studentName;
        }
    
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
        
        
        
        public Student() {
            studentID = null;
            studentName = null;
        }
    
        public Student(String studentID, String studentName) {
            super();
            this.studentID = studentID;
            this.studentName = studentName;
        }
    }
    View Code
    package com.datebase;
    
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.mysql.cj.api.jdbc.Statement;
    import com.mysql.cj.api.mysqla.result.Resultset;
    import com.mysql.cj.jdbc.PreparedStatement;
    
    public class StudentDao {
        public void addStudent(Student student) {
        try {
            Connection connection = DbUtil.getConnection();
            String sql = "insert into student(student_id, student_name) values(?, ?)";
            PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
        
                preparedStatement.setString(1, student.getStudentID());
                preparedStatement.setString(2, student.getStudentName());
                preparedStatement.execute();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public List<Student> select() {
            try {
                Connection connection = DbUtil.getConnection();
                Statement statement = (Statement) connection.createStatement();
                Resultset resultset = (Resultset) statement.executeQuery("select * from student");
                List<Student> ans = new ArrayList<>();
                Student student = null;
                while (((ResultSet) resultset).next()) {
                    student = new Student();
                    student.setStudentID(((ResultSet) resultset).getString("student_id")); //数据库中的名字
                    student.setStudentName(((ResultSet) resultset).getString("student_name"));
                    ans.add(student);
                }
                return ans;
            } catch(Exception e) {
                e.getMessage();
            }
            return null;
        }
    }
    View Code

    对于多次执行的sql语句来说,用PreparedStatement执行快很多,它只编译一次,就记住。。具体我也不知道。没深入理解。求明天java考试爆发。

    Tomcat的话,需要把jdbc放进去Tomcat的lib目录下才可以

  • 相关阅读:
    使用pyppeteer 下载chromium 报错 或速度慢
    Splash抓取jd
    Splash抓取javaScript动态渲染页面
    Django3+websocket+paramiko实现web页面实时输出
    django3 websockets
    MySQL数据库OLTP基准测试( sysbench)
    数据库链接池大小设置和相关测试
    Linux 性能调优IO篇:工具命令篇
    Linux 性能调优内存篇:工具命令篇
    Vue笔记:vue项目引入bootstrap、elementUI、echarts
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6457007.html
Copyright © 2011-2022 走看看