MySQL数据库——基本查询(Create)

CRUD:Create(创建)Retrieve(读取)Update(更新)Delete(删除)

1.Create

①单行数据+全列插入

insert [into] table_name [(colume[,colume]……)] values (value_list) [,(value_list)]……
value_list:value,[,value]……
mysql> create table stud(
  -> id int unsigned primary key auto_increment,
  -> sn int unsigned unique key,
  -> name varchar(20) not null,
  -> qq varchar(32) unique key
  -> );
Query OK, 0 rows affected (0.02 sec)

mysql> desc stud;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| sn    | int(10) unsigned | YES  | UNI | NULL    |                |
| name  | varchar(20)      | NO   |     | NULL    |                |
| qq    | varchar(32)      | YES  | UNI | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

-- id自增,可以忽略不写
mysql> insert into stud (sn,name,qq) values (123,'张飞','1234');
Query OK, 1 row affected (0.00 sec)

mysql> select* from stud;
+----+------+--------+------+
| id | sn   | name   | qq   |
+----+------+--------+------+
|  1 |  123 | 张飞   | 1234 |
+----+------+--------+------+
1 row in set (0.00 sec)

-- 也可以不指定列进行全列插入
mysql> insert into stud values (10,124,'关羽','1236');
Query OK, 1 row affected (0.00 sec)

mysql> select* from stud;
+----+------+--------+------+
| id | sn   | name   | qq   |
+----+------+--------+------+
|  1 |  123 | 张飞   | 1234 |
| 10 |  124 | 关羽   | 1236 |
+----+------+--------+------+
2 rows in set (0.00 sec)

-- 也可以省略into
mysql> insert stud values (11,125,'刘备','1238');
Query OK, 1 row affected (0.01 sec)

mysql> select* from stud;
+----+------+--------+------+
| id | sn   | name   | qq   |
+----+------+--------+------+
|  1 |  123 | 张飞   | 1234 |
| 10 |  124 | 关羽   | 1236 |
| 11 |  125 | 刘备   | 1238 |
+----+------+--------+------+
3 rows in set (0.00 sec)

②多行数据+指定列插入

-- 多行插入
mysql> insert stud values (12,126,'曹操','1231'),(13,127,'许攸','1233');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select* from stud;
+----+------+--------+------+
| id | sn   | name   | qq   |
+----+------+--------+------+
|  1 |  123 | 张飞   | 1234 |
| 10 |  124 | 关羽   | 1236 |
| 11 |  125 | 刘备   | 1238 |
| 12 |  126 | 曹操   | 1231 |
| 13 |  127 | 许攸   | 1233 |
+----+------+--------+------+
5 rows in set (0.00 sec)

-- 指定列进行多行插入
mysql> insert stud (sn,name,qq) values (128,'孙权','1235'),(129,'诸葛亮','1232');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select* from stud;
+----+------+-----------+------+
| id | sn   | name      | qq   |
+----+------+-----------+------+
|  1 |  123 | 张飞      | 1234 |
| 10 |  124 | 关羽      | 1236 |
| 11 |  125 | 刘备      | 1238 |
| 12 |  126 | 曹操      | 1231 |
| 13 |  127 | 许攸      | 1233 |
| 14 |  128 | 孙权      | 1235 |
| 15 |  129 | 诸葛亮    | 1232 |
+----+------+-----------+------+
7 rows in set (0.00 sec)

③插入否则更新

如果由于主键或者唯一键对应的值已经存在而导致插入失败,可以选择进行同步更新操作,即将原数据不一样的值替换为自己要改的

mysql> insert [into] stud values (10,130,'lvbu','4321') 
on duplicate key update sn=130,name='lvbu',qq='4321';
-- 主键冲突
mysql> insert stud values (10,130,'lvbu','4321');
ERROR 1062 (23000): Duplicate entry '10' for key 'PRIMARY'
-- 唯一键冲突
mysql> insert stud values (16,124,'lvbu','4321');
ERROR 1062 (23000): Duplicate entry '124' for key 'sn'
-- 更新
mysql> insert stud values (10,130,'lvbu','4321') on duplicate key update sn=130,name='lvbu',qq='4321';
Query OK, 2 rows affected (0.00 sec)

mysql> select * from stud;
+----+------+-----------+------+
| id | sn   | name      | qq   |
+----+------+-----------+------+
|  1 |  123 | 张飞      | 1234 |
| 10 |  130 | lvbu      | 4321 |
| 11 |  125 | 刘备      | 1238 |
| 12 |  126 | 曹操      | 1231 |
| 13 |  127 | 许攸      | 1233 |
| 14 |  128 | 孙权      | 1235 |
| 15 |  129 | 诸葛亮    | 1232 |
+----+------+-----------+------+
7 rows in set (0.00 sec)

影响的数据行数

-- 此时表中没有数据,直接插入,qq=1111,影响一行数据
mysql> insert stud values (16,131,'貂蝉','1111') on duplicate key update sn=131,name='貂蝉',qq='1122';
Query OK, 1 row affected (0.00 sec)

-- 此时表中已有id=16,数据冲突,所以更新,数据qq变为1122,影响两行数据
mysql> insert stud values (16,131,'貂蝉','1111') on duplicate key update sn=131,name='貂蝉',qq='1122';
Query OK, 2 rows affected (0.00 sec)

-- 此时表中有冲突数据,但冲突数据的值和 update 的值相等,不影响数据
mysql> insert stud values (16,131,'貂蝉','1111') on duplicate key update sn=131,name='貂蝉',qq='1122';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from stud;
+----+------+-----------+------+
| id | sn   | name      | qq   |
+----+------+-----------+------+
|  1 |  123 | 张飞      | 1234 |
| 10 |  130 | lvbu      | 4321 |
| 11 |  125 | 刘备      | 1238 |
| 12 |  126 | 曹操      | 1231 |
| 13 |  127 | 许攸      | 1233 |
| 14 |  128 | 孙权      | 1235 |
| 15 |  129 | 诸葛亮    | 1232 |
| 16 |  131 | 貂蝉      | 1122 |
+----+------+-----------+------+
8 rows in set (0.00 sec)

④替换

insert换成replace

-- 插入新数据
mysql> replace into stud (sn,name,qq) values (140,'许攸','4322');
Query OK, 1 row affected (0.00 sec)

mysql> select * from stud;
+----+------+-----------+------+
| id | sn   | name      | qq   |
+----+------+-----------+------+
|  1 |  123 | 张飞      | 1234 |
| 10 |  130 | lvbu      | 4321 |
| 11 |  125 | 刘备      | 1238 |
| 12 |  126 | 曹操      | 1231 |
| 13 |  127 | 许攸      | 1233 |
| 14 |  128 | 孙权      | 1235 |
| 15 |  129 | 诸葛亮    | 1232 |
| 16 |  131 | 貂蝉      | 1122 |
| 17 |  140 | 许攸      | 4322 |
+----+------+-----------+------+
9 rows in set (0.00 sec)

-- 插入不能重复的数据
mysql> replace into stud (sn,name,qq) values (140,'许攸1','4322');
Query OK, 2 rows affected (0.01 sec)

-- id会继续增,这里其实相当于继续添加一行然后删除原来的数据
mysql> select * from stud;
+----+------+-----------+------+
| id | sn   | name      | qq   |
+----+------+-----------+------+
|  1 |  123 | 张飞      | 1234 |
| 10 |  130 | lvbu      | 4321 |
| 11 |  125 | 刘备      | 1238 |
| 12 |  126 | 曹操      | 1231 |
| 13 |  127 | 许攸      | 1233 |
| 14 |  128 | 孙权      | 1235 |
| 15 |  129 | 诸葛亮    | 1232 |
| 16 |  131 | 貂蝉      | 1122 |
| 18 |  140 | 许攸1     | 4322 |
+----+------+-----------+------+
9 rows in set (0.00 sec)

-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,删除后重新插入

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我会尽量回答你的问题。关于Java连接MySQL数据库实现登录注册功能,以下是一个简单的实现步骤: 1.下载并安装MySQL数据库,安装完成后,创建一个名为“test”的数据库。 2.下载并安装JDBC驱动程序,将下载的JDBC驱动程序的jar包添加到项目的classpath中。 3.编写Java代码,实例化JDBC驱动程序,连接MySQL数据库,创建用户表,实现用户的注册和登录功能。 以下是一个简单的Java代码示例: ``` import java.sql.*; public class JdbcTest { public static void main(String[] args) { // 数据库连接信息 String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; String username = "root"; String password = "123456"; // 注册JDBC驱动程序 try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("找不到JDBC驱动程序!"); e.printStackTrace(); return; } // 连接MySQL数据库 Connection conn = null; try { conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { System.out.println("连接MySQL数据库失败!"); e.printStackTrace(); return; } // 创建用户表 Statement stmt = null; try { stmt = conn.createStatement(); String sql = "create table if not exists user(id int primary key auto_increment, username varchar(50) not null, password varchar(50) not null)"; stmt.executeUpdate(sql); } catch (SQLException e) { System.out.println("创建用户表失败!"); e.printStackTrace(); return; } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } // 用户注册 PreparedStatement pstmt = null; try { String sql = "insert into user(username, password) values (?, ?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, "user1"); pstmt.setString(2, "123456"); pstmt.executeUpdate(); } catch (SQLException e) { System.out.println("用户注册失败!"); e.printStackTrace(); return; } finally { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } // 用户登录 ResultSet rs = null; try { String sql = "select * from user where username=? and password=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, "user1"); pstmt.setString(2, "123456"); rs = pstmt.executeQuery(); if (rs.next()) { System.out.println("用户登录成功!"); } else { System.out.println("用户名或密码错误!"); } } catch (SQLException e) { System.out.println("用户登录失败!"); e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } // 关闭数据库连接 try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们先注册了JDBC驱动程序,然后连接了MySQL数据库,接着创建了一个名为“user”的用户表,实现了用户的注册和登录功能。你可以根据自己的需求修改代码中的数据库连接信息和SQL语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Outlier_9

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值