MySQL常用命令

自用
主要针对数据库实训总结常用的MySQL命令
和JDBC相关操作

MySQL数据库操作

一、数据库、数据表

  1. create database university

  2. use university

  3. show university

  4. drop university

  5. create table classroom(
    ID int not null,
    name varvhar(20),
    primary key(ID, name)
    constraint ‘fk’ foreign key(‘ID’) references another_table(‘ID’)
    );

  6. alter table classroom rename new_classroom;

  7. alter table classroom change ID new_ID varchar(20);

  8. alter table classroom change ID ID varchar(40);

  9. alter table classroom add new_col int first/after ID;

  10. alter table classroom drop ID

  11. drop table classroom

  12. describe classroom
    查看表中数据等
    在这里插入图片描述

  13. show create table classroom
    第一次用,试试,可以查看表的属性
    在这里插入图片描述
    二、查、增删改,索引、视图

唔,想起搜索功能实现的时候可以用查询的匹配条件嘞
提升性能也许可以尝试索引,
然后为了存储方便可以弄个触发器自动增减记录中的某字段

  • Query
  1. select * from A
  2. select ID, name from A order by ID DESC/ASC
  3. select ID from A where ID in (‘01’,‘02’)
  4. select ID from A where ID between ‘01’ and ‘05’
  5. select name from A where name like ‘%a%’(%匹配任意串)
  6. select distinct name from A where name like ‘a’ (_匹配单个字符)
  7. select * from A where ID is not null
  8. 分页
    select * from A limit 20,20 (从20行开始,向后查询20行,常常用于处理页面值与每页记录条数的关系)
  9. select ID, max(name) from A group by ID (聚集函数,记得使用Group by)
  • Insert
    insert into A(ID, name) values(‘06’,‘test’)
    insert into A (ID, name) values (‘1’,‘a’), (‘2’,‘b’), (‘3’,‘c’); (多条记录)
    insert into A(ID) values(‘06’);

  • Delete
    delete from A where ID = ‘06’;
    delete from A (删除所有记录,慎)

  • Update
    update A set name = ‘new_name’ where ID =‘06’

  • 索引
    create table A(ID int , unique index idx_ID(ID))
    create index idx_ID on A (ID);
    alter table A add index idx_ID (ID);
    drop index idx_ID on A

  • 视图
    create view v1 as(select语句)
    drop view v1 if exists v1

  • 触发器
    create trigger tri_g
    after insert on A
    for each row
    update Book set num = num+1 where new.ID = Book.ID

    delimiter && (更改结束符)

    && delimiter ;
    show triggers
    drop trigger trig_g

JDBC

显然,也许咕咕了
Java Data Base Connectivity

一、连接数据库

  • 报错 Could not create connection to database server.
    是由于版本问题。

阿,sl,搞了一个小时才弄好,这一切的起因是因为想不使用jsp项目调试jdbc,所以新建了一个java项目。
原来是版本问题,好lay(JDBC包与SQL版本要一致,命令略有差异)
新版本是“com.mysql.cj.jdbc.Driver”,且要加上时区的声明
旧版没有cj,无需声明时区。

  • 学到了,用Class.forName要在try语句里面。
import java.sql.*;

public class test {
    public static void  main(String [] args){
        String jdbcName="com.mysql.cj.jdbc.Driver";
        String dbUrl ="jdbc:mysql://localhost:3306/product?serverTimezone=UTC";
        //登录信息
        String dbUser = "root";
        Connection con = null;
        String dbPassword = "";
        System.out.println("Hi IDEA");
    try{
    //加载数据库驱动
        Class.forName(jdbcName).newInstance();
        System.out.println("OK");

    }
    catch (Exception e){
        e.printStackTrace();
    }
        try{
        //建立数据库连接
            con = DriverManager.getConnection(dbUrl,dbUser,dbPassword);
            System.out.println("connected");
        }
        catch (SQLException e){
            e.printStackTrace();
            System.out.println("Failed");
        }
        finally {
            try {
                con.close();
                System.out.println("Closed");
            }
            catch (SQLException e){
                e.printStackTrace();
                System.out.println("Close failed");
            }
        }

    }


}

因为版本的原因,又过去了1h…心好lay
在这里插入图片描述
- DriverManager驱动管理类

  •   	 Class.forName(jdbcName).newInstance();
    
  •   Connection con = DriverManager.getConnection(dbUrl,dbUser,dbPassword);
    
  •    con.close();
    

二、增删查改
都是差不多,PrepareStatement用的比较多,可以预编译,后续设置参数,执行速度也比较快。
增删查改基本不变,变的是sql里的内容,按照sql语法来即可。
PrepareStatement & Statement

 		//首先要连接
 		 Connection conn = Dbutil.getCon();
        String sql = "update test_vegetables set productID=?, productName=?,salerName=?,days=?,quantity=? where productID = ?";
        //Preparement语句编译
        PreparedStatement pstmt = conn.prepareStatement(sql);

        pstmt.setString(1,vegatables.getProductID());
        pstmt.setString(2,vegatables.getProductName());
        pstmt.setString(3,vegatables.getSalerName());
        pstmt.setInt(4,vegatables.getDays());
        pstmt.setInt(5,vegatables.getQuantity());
		//preparement语句执行更新
        int res = pstmt.executeUpdate();
        //关闭连接
        dbutil.close(pstmt,conn);

statement执行时executeUpdate里要放参数sql

      Statement stmt = con.createStatement();
            int res = stmt.executeUpdate(sql);
            if(res == 1){
                System.out.println("数据库插入成功");
            }
            else{
                System.out.println("数据库插入失败");
            }

总结:
PreparementStatement继承了Statement,支持Statement的基本功能
同时还能动态处理数据

PreparementStatement:
pstmt = con.prepareStatement (sql)
pstmt.excuteUpdate()

Statement
stmt = con.createStatement()
stmt.executeUpdate()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值