Delete Duplicate Rows From an Oracle Table

Oracle中删除重复行
本文介绍了一种在Oracle数据库中删除表内重复行的方法,在创建主键或唯一索引之前,可以通过运行特定的DELETE语句来实现这一目标。该过程确保只保留每组中的第一条记录。
It's easy to introduce duplicate rows of data into Oracle tables by running a data load twice without the primary key or unique indexes created or enabled.

Here's how you remove the duplicate rows before the primary key or unique indexes can be created:

DELETE FROM our_table
WHERE rowid NOT IN
(SELECT MIN(rowid)
FROM our_table
GROUP BY column1, column2, column3... ;



Here column1, column2, column3 constitute the identifying key for each record.
Be sure to replace our_table with the table name from which you want to remove the duplicate rows. The GROUP BY is used on the columns that make the primary key for the table. This script deletes each row in the group after the first row.
提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习和研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度和稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b或更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能和输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习和修改: 通过阅读模型中的注释和查阅相关文献,加深对BP神经网络与PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
[root@yfw ~]# cd /www/wwwroot/szrengjing.com [root@yfw szrengjing.com]# ./setup_credit_system.sh 🚀 开始部署用户信用系统... ✅ 用户信用系统部署完成! 📊 当前用户信用状态: ERROR 1142 (42000) at line 1: SELECT command denied to user 'szrengjing_com'@'localhost' for table 'ecs_user_credit' 📝 最近信用日志: ERROR 1142 (42000) at line 1: SELECT command denied to user 'szrengjing_com'@'localhost' for table 'ecs_user_credit_log' [root@yfw szrengjing.com]# mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) [root@yfw szrengjing.com]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 90351 Server version: 5.7.42-log Source distribution Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE -> ON szrengjing.ecs_user_credit -> TO 'szrengjing_com'@'localhost'; ERROR 1144 (42000): Illegal GRANT/REVOKE command; please consult the manual to see which privileges can be used mysql> mysql> GRANT SELECT, INSERT -> ON szrengjing.ecs_user_credit_log -> TO 'szrengjing_com'@'localhost'; ERROR 1146 (42S02): Table 'szrengjing.ecs_user_credit_log' doesn't exist mysql> mysql> -- 刷新权限 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.02 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql> -- 查看数据库是否存在 mysql> SHOW DATABASES LIKE 'szrengjing'; Empty set (0.00 sec) mysql> mysql> -- 使用数据库 mysql> USE szrengjing; ERROR 1049 (42000): Unknown database 'szrengjing' mysql> mysql> -- 查看表是否已创建 mysql> SHOW TABLES LIKE 'ecs_user_credit'; +--------------------------------------------+ | Tables_in_szrengjing_com (ecs_user_credit) | +--------------------------------------------+ | ecs_user_credit | +--------------------------------------------+ 1 row in set (0.00 sec) mysql> SHOW TABLES LIKE 'ecs_user_credit_log'; +------------------------------------------------+ | Tables_in_szrengjing_com (ecs_user_credit_log) | +------------------------------------------------+ | ecs_user_credit_log | +------------------------------------------------+ 1 row in set (0.00 sec) mysql> -- 使用正确的数据库 mysql> USE szrengjing_com; Database changed mysql> mysql> -- 授予对主表的操作权限 mysql> GRANT SELECT, INSERT, UPDATE, DELETE -> ON szrengjing_com.ecs_user_credit -> TO 'szrengjing_com'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> mysql> -- 授予对日志表的操作权限 mysql> GRANT SELECT, INSERT -> ON szrengjing_com.ecs_user_credit_log -> TO 'szrengjing_com'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> mysql> -- 授予执行存储过程的权限 mysql> GRANT EXECUTE ON szrengjing_com.* TO 'szrengjing_com'@'localhost'; Query OK, 0 rows affected (0.01 sec) mysql> mysql> -- 刷新权限 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql> USE szrengjing_com; Database changed mysql> SELECT * FROM ecs_user_credit; +----+---------+-------+-----------------+---------------------+ | id | user_id | score | level | updated_at | +----+---------+-------+-----------------+---------------------+ | 1 | 1 | 85 | ★★★★☆ | 2025-11-09 05:38:14 | +----+---------+-------+-----------------+---------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM ecs_user_credit_log LIMIT 5; +----+---------+-----------+-----------+--------------+--------------------+---------------------+ | id | user_id | old_score | new_score | change_value | reason | created_at | +----+---------+-----------+-----------+--------------+--------------------+---------------------+ | 1 | 1 | 80 | 95 | 15 | 完善个人资料 | 2025-11-09 05:38:14 | | 2 | 1 | 95 | 85 | -10 | 评论被举报 | 2025-11-09 05:38:14 | +----+---------+-----------+-----------+--------------+--------------------+---------------------+ 2 rows in set (0.01 sec) mysql> CALL sp_add_credit(1, 5, '测试加分'); Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM ecs_user_credit_log ORDER BY id DESC LIMIT 1\G *************************** 1. row *************************** id: 3 user_id: 1 old_score: 85 new_score: 90 change_value: 5 reason: 测试加分 created_at: 2025-11-09 05:55:47 1 row in set (0.00 sec) mysql> #!/bin/bash mysql> mysql> DB_NAME="szrengjing_com" -> DB_USER="szrengjing_com" -> PASSWORD_FILE="/root/.my.cnf" -> -> echo "🚀 开始部署用户信用系统..." -> -> mysql --defaults-file=$PASSWORD_FILE << EOF -> -> USE \`${DB_NAME}\`; ERROR: Unknown command '\`'. ERROR: Unknown command '\`'. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DB_NAME="szrengjing_com" DB_USER="szrengjing_com" PASSWORD_FILE="/root/.my.cnf" ' at line 1 mysql> mysql> -- 创建信用主表 mysql> CREATE TABLE IF NOT EXISTS ecs_user_credit ( -> id INT AUTO_INCREMENT PRIMARY KEY, -> user_id INT NOT NULL UNIQUE, -> score INT DEFAULT 80, -> level VARCHAR(20) AS ( -> CASE -> WHEN score >= 95 THEN '★★★★★' -> WHEN score >= 85 THEN '★★★★☆' -> WHEN score >= 75 THEN '★★★☆☆' -> WHEN score >= 60 THEN '★★☆☆☆' -> ELSE '★☆☆☆☆' -> END -> ) STORED, -> updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> mysql> -- 创建信用日志表 mysql> CREATE TABLE IF NOT EXISTS ecs_user_credit_log ( -> id BIGINT AUTO_INCREMENT PRIMARY KEY, -> user_id INT NOT NULL, -> old_score INT, -> new_score INT, -> change_value INT, -> reason VARCHAR(100), -> created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> mysql> -- 创建触发器自动记录变更 mysql> DELIMITER ;; mysql> DROP TRIGGER IF EXISTS tr_after_update_credit;; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> CREATE TRIGGER tr_after_update_credit -> AFTER UPDATE ON ecs_user_credit -> FOR EACH ROW -> BEGIN -> INSERT INTO ecs_user_credit_log (user_id, old_score, new_score, change_value, reason) -> VALUES (NEW.user_id, OLD.score, NEW.score, NEW.score - OLD.score, '系统调整'); -> END;; Query OK, 0 rows affected (0.01 sec) mysql> DELIMITER ; mysql> mysql> -- 创建存储过程用于安全加分/减分 mysql> DELIMITER ;; mysql> DROP PROCEDURE IF EXISTS sp_add_credit;; Query OK, 0 rows affected (0.00 sec) mysql> CREATE PROCEDURE sp_add_credit( -> IN p_user_id INT, -> IN p_change INT, -> IN p_reason VARCHAR(100) -> ) -> BEGIN -> DECLARE current_score INT DEFAULT 80; -> -- 查询当前分数 -> SELECT score INTO current_score FROM ecs_user_credit WHERE user_id = p_user_id FOR UPDATE; -> -> -- 更新分数 -> INSERT INTO ecs_user_credit (user_id, score) -> VALUES (p_user_id, current_score + p_change) -> ON DUPLICATE KEY UPDATE score = score + p_change; -> -> -- 触发器会自动记录日志 -> END;; Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ; mysql> mysql> EOF -> -> # 检查是否成功 -> if [ $? -eq 0 ]; then ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EOF if [ $? -eq 0 ]' at line 1 -> echo "✅ 用户信用系统部署完成!" -> else -> echo "❌ 部署失败,请检查 SQL 错误。" -> exit 1 -> fi -> -> # 显示当前数据(现在应该能正常显示) -> echo "" -> echo "📊 当前用户信用状态:" -> mysql --defaults-file=$PASSWORD_FILE -Nse "USE ${DB_NAME}; SELECT user_id, score, level FROM ecs_user_credit;" -> -> echo "" -> echo "📝 最近信用日志:" -> mysql --defaults-file=$PASSWORD_FILE -Ee "USE ${DB_NAME}; SELECT * FROM ecs_user_credit_log ORDER BY id DESC LIMIT 5;" -> ->
11-10
你刚才在 MySQL 交互命令行中 **错误地粘贴了整个 Shell 脚本内容**,导致 MySQL 把 `#!/bin/bash`、`DB_NAME="..."`、`if [ $? -eq 0 ]` 这些 **Linux Shell 命令当作 SQL 语句执行**,所以报错: ``` ERROR: Unknown command '\`'. ERROR 1064 (42000): You have an error in your SQL syntax... ``` 这是正常的 —— 因为 **MySQL 不认识 bash 语法**! --- ## ✅ 正确理解:你的脚本已经可以工作 从你的操作来看: - ✅ 表已存在 - ✅ 权限已正确授权(通过手动 GRANT) - ✅ 存储过程能调用 - ✅ 数据可查 👉 所以你现在的问题不是“系统不能用”,而是: > ❗你在 MySQL 客户端里误执行了 Shell 脚本代码,导致一堆语法错误提示。 这些错误 **不影响实际功能**,只是因为你“用错了地方”。 --- ## ✅ 如何正确使用 `setup_credit_system.sh` 你应该把这个脚本保存为文件,并在 **Linux 终端中运行它作为 Shell 脚本**,而不是粘贴到 `mysql>` 里面! --- ### ✅ 步骤 1:创建正确的 Shell 脚本文件 ```bash # 编辑脚本 vim /www/wwwroot/szrengjing.com/setup_credit_system.sh ``` 粘贴以下内容(这是修正后的完整版本): ```bash #!/bin/bash # 配置变量 DB_NAME="szrengjing_com" DB_USER="szrengjing_com" PASSWORD_FILE="/root/.my.cnf" echo "🚀 开始部署用户信用系统..." # 执行 SQL 操作 mysql --defaults-file=$PASSWORD_FILE << EOF USE \`${DB_NAME}\`; -- 创建信用主表 CREATE TABLE IF NOT EXISTS ecs_user_credit ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL UNIQUE, score INT DEFAULT 80, level VARCHAR(20) AS ( CASE WHEN score >= 95 THEN '★★★★★' WHEN score >= 85 THEN '★★★★☆' WHEN score >= 75 THEN '★★★☆☆' WHEN score >= 60 THEN '★★☆☆☆' ELSE '★☆☆☆☆' END ) STORED, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 创建信用日志表 CREATE TABLE IF NOT EXISTS ecs_user_credit_log ( id BIGINT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, old_score INT, new_score INT, change_value INT, reason VARCHAR(100), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- 删除旧触发器和存储过程 DROP TRIGGER IF EXISTS tr_after_update_credit; DROP PROCEDURE IF EXISTS sp_add_credit; -- 创建触发器自动记录变更 DELIMITER ;; CREATE TRIGGER tr_after_update_credit AFTER UPDATE ON ecs_user_credit FOR EACH ROW BEGIN INSERT INTO ecs_user_credit_log (user_id, old_score, new_score, change_value, reason) VALUES (NEW.user_id, OLD.score, NEW.score, NEW.score - OLD.score, '系统调整'); END;; DELIMITER ; -- 创建存储过程用于安全加分/减分 CREATE PROCEDURE sp_add_credit( IN p_user_id INT, IN p_change INT, IN p_reason VARCHAR(100) ) BEGIN DECLARE current_score INT DEFAULT 80; -- 查询当前分数(加锁防止并发) SELECT score INTO current_score FROM ecs_user_credit WHERE user_id = p_user_id FOR UPDATE; -- 更新分数 INSERT INTO ecs_user_credit (user_id, score) VALUES (p_user_id, current_score + p_change) ON DUPLICATE KEY UPDATE score = score + p_change; -- 触发器会自动记录日志 END;; EOF # 检查是否成功 if [ $? -eq 0 ]; then echo "✅ 用户信用系统部署完成!" else echo "❌ 部署失败,请检查 SQL 错误。" exit 1 fi # 显示当前数据 echo "" echo "📊 当前用户信用状态:" mysql --defaults-file=$PASSWORD_FILE -Nse "USE ${DB_NAME}; SELECT user_id, score, level FROM ecs_user_credit;" | \ while read user_id score level; do printf "%-8s %-6s %s\n" "User:$user_id" "Score:$score" "Level:$level" done echo "" echo "📝 最近信用日志:" mysql --defaults-file=$PASSWORD_FILE -Ee "USE ${DB_NAME}; SELECT * FROM ecs_user_credit_log ORDER BY id DESC LIMIT 5;" ``` --- ### ✅ 步骤 2:保存后退出 vim 按 `Esc` → 输入 `:wq` → 回车 --- ### ✅ 步骤 3:添加可执行权限 ```bash chmod +x /www/wwwroot/szrengjing.com/setup_credit_system.sh ``` --- ### ✅ 步骤 4:运行脚本(在 Linux 终端中) ```bash cd /www/wwwroot/szrengjing.com ./setup_credit_system.sh ``` 🎯 输出示例: ``` 🚀 开始部署用户信用系统... ✅ 用户信用系统部署完成! 📊 当前用户信用状态: User:1 Score:90 Level:★★★★☆ 📝 最近信用日志: id: 3 user_id: 1 old_score: 85 new_score: 90 change_value: 5 reason: 测试加分 created_at: 2025-11-09 05:55:47 ... ``` --- ## 🔒 关键提醒 | 注意事项 | 说明 | |--------|------| | 🚫 不要在 `mysql>` 里粘贴 shell 脚本 | 否则会被当成 SQL 执行,报错 | | ✅ 只能在终端 shell 中运行 `.sh` 文件 | 才能正确解析 bash 语法 | | 🔐 确保 `/root/.my.cnf` 存在且权限为 `600` | 内容如下: ```ini [client] user=szrengjing_com password=你的密码 ``` 然后设置权限: ```bash chmod 600 /root/.my.cnf ``` --- ## ✅ 总结:你现在要做的事 1. ✅ 删除之前错误粘贴的代码(不用管那些 ERROR,它们只是临时输入错误) 2. ✅ 使用 `vim` 或其他编辑器将脚本写入 `.sh` 文件 3. ✅ 添加执行权限 `chmod +x` 4. ✅ 在终端运行 `./setup_credit_system.sh` 5. ✅ 确保 `/root/.my.cnf` 正确配置 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值