Database mysql basic syntax notes

show databases;
use select_test;
show tables;
show tables from select_test;
select database();
use select_test;
create table stuinfo(
stuid int,
stuname varchar(20),
gender char,
borndate datetime);
show tables;
desc stuinfo;
select * from stuinfo;
insert into stuinfo values(1,'张无忌','男','1990-1-1');
set names utf8;
set names gbk;
insert into stuinfo values(2,'张翠山','男','1980-1-1');
select * from stuinfo;
update stuinfo set borndate='1970-1-1' where stuid=2;
delete from stuinfo where stuid=1;
alter table stuinfo add column email varchar(20);
drop table stuinfo;
show table;
exit;
select first_name from employees;
select first_name,last_name,email from employees;
select database();
select version();
select user();

select last_name from employees;
select last_name as "姓名" from employees;
select last_name  "姓名" from employees;
select concat(first_name,last_name) as '姓名' from employees;
select distinct department_id from employees;
desc employees;
select concat(employee_id,',',first_name) as 'out_put' from employees;
select last_name,first_name from employees where salary>20000;
select * from employees where department_id<>100;
select last_name from employees where not(department_id>=50 and department_id<=100);
select * from employees where last_name like '__x%';
select * from employees where last_name like '_$_%' escape '$';
select * from employees where department_id in(30,50,90);
select * from employees where department_id not between 30 and 90;
select * from employees where commission_pct is not null order by salary desc,department_id asc;
select concat('hello,' ,first_name,last_name) 备注 from employees;
select substr('范星月最美',1,3);
select sum(salary),avg(salary),min(salary),max(salary), count(salary) from employees where department_id=30;
select sum(salary),department_id from employees group by department_id;
select department_id,sum(salary) from employees group by department_id;
select count(*),manager_id from employees group by manager_id;
select max(salary) 最高工资,department_id from employees where email like '%a%' group by department_id;
/*分组查询
分组前筛选:原始表 where group by 前面
分组后筛选:分组后的结果集 having group by的后面
select 查询列表
from 表
where 筛选条件
group by 分组列表
having 分组后的筛选
order by 排序
*/
select count(*) 员工个数,department_id from employees group by department_id having count(*)>5;
select job_id,max(salary) 
from employees 
where commission_pct is not null 
group by job_id 
having max(salary)>12000;

select min(salary) 最低工资, manager_id
from employees
where manager_id>102
group by manager_id
having min(salary)>5000;

select max(salary) 最高工资 ,job_id
from employees
where commission_pct is null
group by job_id
having max(salary)>6000
order by max(salary) asc;


/*SQL92语法
select 查询列表
from 表1 别名,表2 别名
where 连接条件
and 筛选条件
group by
having 
order by
*/


/*SQL99
select 查询列表
from 表1 别名 
jion 表2 别名 on 连接条件
jion 表3 别名 on 连接条件
where 连接条件
group by
having 
order by
*/
/*查询员工和部门名*/
select last_name,department_name
from employees e join departments d
on e.department_id = d.department_id;

#查询部门编号>100的部门名和所在的城市名
select department_name,city
from departments d join locations l
on d.location_id=l.location_id
where d.department_id>100;

#查询每个城市的部门个数
select count(*),l.city
from departments d join locations l
on d.location_id=l.location_id
group by l.city;

#查询部门员工个数>10的部门名,并按员工个数降序
select count(*) 员工个数,department_name
from employees e join departments d
on e.department_id=d.department_id
group by d.department_id
having 员工个数 >10
order by 员工个数 desc;

#自连接 查询员工名和对应的领导名
select e.last_name,m.last_name
from employees e join employees m
on e.manager_id =m.employee_id;

use myemployees;
#子查询
#查询和Zlotkey相同部门的员工姓名和工资
select last_name,salary
from employees 
where department_id =(
	select department_id 
	from employees 
	where last_name='Zlotkey'
);

#查询工资比公司平均工资高的员工的员工号,姓名和工资
select employee_id ,last_name,salary
from employees 
where salary>(
	select avg(salary)
	from employees
);

create database if not exists fxydb;
show databases;
drop database if exists fxydb;
show databases;

use select_test;
alter table student_table add column borndate timestamp not null;
alter table student_table change column borndate birthday datetime;
drop table if exists students;

4 characteristics of transactions (ACID)

Atomicity, consistency, isolation, durability

JDBC: java database connectivity, Java and database connection technology, a set of technical specifications (interfaces) for java applications to access databases launched by sun company

 

Right-click src, create a new resource bundle and name it jdbc, that is, a jdbc.properties has been successfully built

Among them, the contents of jdbc.properties are as follows:

user=root
password=fxy716
url=jdbc:mysql://localhost:3306/girls?useSSL=false&serverTimezone=UTC
driver=com.mysql.cj.jdbc.Driver

The basic SQL query code is as follows:

package test.fxy.jdbc;

import org.w3c.dom.ls.LSOutput;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class TestJDBC1 {
    public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {
        //使用Properties类来加载属性文件
        Properties properties=new Properties();
        String path = Thread.currentThread().getContextClassLoader().getResource  ("jdbc.properties").getPath();
        FileInputStream in = new FileInputStream(path);
        properties.load(in);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        //加载驱动,利用反射
        Class.forName(driver);
        //获取数据库连接,并返回数据库和java的连接connection
        Connection connection=DriverManager.getConnection(url,user,password);
        System.out.println("连接成功");


        String sql="select id,name,sex,borndate from beauty";
        //利用connection创建一个statement对象,即获取执行sql语句的命令对象
        Statement statement=connection.createStatement();
        //执行sql语句
//        boolean execute=statement.execute(sql);//执行任何sql语句
//        int update = statement.executeUpdate(sql); 执行增删改,返回受影响的行数
        ResultSet set = statement.executeQuery(sql);//执行查询语句,返回一个结果集

        while(set.next()){
            int id=set.getInt(1);
            String name=set.getString(2);
            String sex=set.getString(3);
            Date date=set.getDate(4);
            System.out.println(id+"\t"+name+"\t"+sex+"\t"+date);
        }
        set.close();
        statement.close();
        connection.close();


    }
}
JDBC常用API
DriverManager
Connection
Statement
PrepareStatement
ResultSet
JDBC编程步骤:
1.加载驱动,通常使用Class.forname()静态方法加载驱动
2.通过DriverManager获取数据库连接,并返回数据库和java的连接Connection对象
3.通过Connection对象创建Statement对象
4.使用Statement执行SQL语句
5.操作结果集。如果是查询语句,执行结果将返回一个ResultSet对象,该对象以逻辑表格的形式封装了执行数据库操作的结果集
6.回收数据库资源.ResultSet,Statement,Connection依次关闭
Statement VS PreparedStatement
package test.fxy.jdbc;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;


public class TestPrepareStatement {

    @Test
    public void testStatement() throws IOException, SQLException, ClassNotFoundException {
        Scanner input=new Scanner(System.in);
        System.out.println("Please input username");
        String username=input.next();
        System.out.println("Please input password");
        String pwd=input.next();

        //
        Properties info=new Properties();
        String path = Thread.currentThread().getContextClassLoader().getResource  ("jdbc.properties").getPath();
        FileInputStream in = new FileInputStream(path);
        info.load(in);
        String user = info.getProperty("user");
        String password = info.getProperty("password");
        String driver = info.getProperty("driver");
        String url = info.getProperty("url");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        String sql="select count(*) from admin where username='"+username+"' and password='"+pwd+"';";
        Statement statement=connection.createStatement();
        ResultSet set=statement.executeQuery(sql);
        if(set.next()){
            int count=set.getInt(1);
            System.out.println(count>0?"Login success":"Login fair");
        }
        set.close();
        statement.close();;
        connection.close();
    }

    @Test
    public void testPreparedStatement() throws IOException, SQLException, ClassNotFoundException {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input username");
        String username = input.next();
        System.out.println("Please input password");
        String pwd = input.next();

        //
        Properties info = new Properties();
        String path = Thread.currentThread().getContextClassLoader().getResource("jdbc.properties").getPath();
        FileInputStream in = new FileInputStream(path);
        info.load(in);
        String user = info.getProperty("user");
        String password = info.getProperty("password");
        String driver = info.getProperty("driver");
        String url = info.getProperty("url");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

        String sql="select count(*) from admin where username=? and password=?;";
        PreparedStatement statement=connection.prepareStatement(sql);

        //设置占位符的值
        statement.setString(1,username);
        statement.setString(2,pwd);

        //执行sql命令
//        int update=statement.executeUpdate();
        ResultSet set=statement.executeQuery();
        if(set.next()){
            int count=set.getInt(1);
            System.out.println(count>0?"Login success":"Login fair");
        }

        set.close();
        statement.close();;
        connection.close();



    }

}
 

Introducing Dao :

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326283430&siteId=291194637
Recommended