请耐心等待我,我是PHP的新手,所以我还不是很好。我正在尝试创建一个按钮,它具有可以删除MySQL数据库中所有行的功能。
我的numtable中有两列:“n_id(主键)”和“num”。它们都是INT。
现在我在MySQL数据库中填写了3个值:
n_id num
1 4
2 9
3 47
的index.html:
<form method = "post" action = "delete.php">
<input name="delete" type="submit" id="delete" value="Delete">
</form>
delete.php:
$query = ("DELETE FROM numtable WHERE id="delete");
我已经看了很多堆栈溢出,但我只能找到问题,它必须删除一行,而不是整个表。我的代码肯定是错的,但是我还需要更多什么呢?
我可以看到在SQL查询中也可以使用TRUNCATE
而不是DELETE
。据我了解,如果数据库有非常大的数据,最好使用TRUNCATE
,如果数据库不是那么大,最好使用删除。那是对的吗?
答案 0 :(得分:5)
如果您需要删除表中的所有记录 - 那么只需省略where
条件:
DELETE FROM numtable
答案 1 :(得分:2)
--max_old_space_size=4092
DELETE和truncate之间的区别很有意思。
删除:逐行清除行检查条件。
TRUNCATE:快速清除所有行。在内部,它会破坏并重新创建表格。
查看此摘要,这真的很有说服力:
如果有任何“全表操作”保护,DELETE FROM table
也可以做到这一点。
要阅读有关DELETE和TRUNCATE的更多信息,请点击以下链接:
答案 2 :(得分:0)
如果要从表中删除所有行,最好使用Started POST "/actors.json" for 127.0.0.1 at 2016-01-12 16:15:35 +0100
Processing by ActorsController#create as JSON
Parameters: {"_json"=>[{"name"=>"John Travolta", "character"=>"Vincent Vega", "movie_id"=>680}, {"name"=>"Samuel L. Jackson", "character"=>"Jules Winfield", "movie_id"=>680}, {"name"=>"Uma Thurman", "character"=>"Mia Wallace", "movie_id"=>680}, {"name"=>"Bruce Willis", "character"=>"Butch Coolidge", "movie_id"=>680}, {"name"=>"Ving Rhames", "character"=>"Marsellus Wallace", "movie_id"=>680}, {"name"=>"Harvey Keitel", "character"=>"Wolf", "movie_id"=>680}, {"name"=>"Eric Stoltz", "character"=>"Lance", "movie_id"=>680}, {"name"=>"Tim Roth", "character"=>"Pumpkin", "movie_id"=>680}, {"name"=>"Amanda Plummer", "character"=>"Honey Bunny", "movie_id"=>680}, {"name"=>"Maria de Medeiros", "character"=>"Fabienne", "movie_id"=>680}, {"name"=>"Quentin Tarantino", "character"=>"Jimmie Dimmick", "movie_id"=>680}, {"name"=>"Christopher Walken", "character"=>"Captain Koons", "movie_id"=>680}, {"name"=>"Rosanna Arquette", "character"=>"Jody", "movie_id"=>680}, {"name"=>"Peter Greene", "character"=>"Zed", "movie_id"=>680}, {"name"=>"Duane Whitaker", "character"=>"Maynard", "movie_id"=>680}, {"name"=>"Angela Jones", "character"=>"Esmarelda Villalobos", "movie_id"=>680}, {"name"=>"Phil LaMarr", "character"=>"Marvin", "movie_id"=>680}, {"name"=>"Steve Buscemi", "character"=>"Buddy Holly", "movie_id"=>680}, {"name"=>"Bronagh Gallagher", "character"=>"Trudi", "movie_id"=>680}, {"name"=>"Laura Lovelace", "character"=>"Waitress", "movie_id"=>680}, {"name"=>"Frank Whaley", "character"=>"Brett", "movie_id"=>680}, {"name"=>"Burr Steers", "character"=>"Roger", "movie_id"=>680}, {"name"=>"Paul Calderon", "character"=>"Paul", "movie_id"=>680}, {"name"=>"Jerome Patrick Hoban", "character"=>"Ed Sullivan", "movie_id"=>680}, {"name"=>"Michael Gilden", "character"=>"Page for Phillip Morris", "movie_id"=>680}, {"name"=>"Gary Shorelle", "character"=>"Ricky Nelson", "movie_id"=>680}, {"name"=>"Susan Griffiths", "character"=>"Marilyn Monroe", "movie_id"=>680}, {"name"=>"Eric Clark", "character"=>"James Dean", "movie_id"=>680}, {"name"=>"Joseph Pilato", "character"=>"Dean Martin", "movie_id"=>680}, {"name"=>"Brad Blumenthal", "character"=>"Jerry Lewis (as Brad Parker)", "movie_id"=>680}, {"name"=>"Lorelei Leslie", "character"=>"Mamie van Doren", "movie_id"=>680}, {"name"=>"Emil Sitka", "character"=>"Hold Hands You Lovebirds (archive footage)", "movie_id"=>680}, {"name"=>"Brenda Hillhouse", "character"=>"Mrs. Coolidge - Butch's Mother", "movie_id"=>680}, {"name"=>"Chandler Lindauer", "character"=>"Young Butch", "movie_id"=>680}, {"name"=>"Sy Sher", "character"=>"Klondike", "movie_id"=>680}, {"name"=>"Robert Ruth", "character"=>"Sportscaster #1 - Coffee Shop", "movie_id"=>680}, {"name"=>"Rich Turner", "character"=>"Sportscaster #2", "movie_id"=>680}, {"name"=>"Don Blakely", "character"=>"Wilson's Trainer", "movie_id"=>680}, {"name"=>"Carl Allen", "character"=>"Dead Floyd Wilson", "movie_id"=>680}, {"name"=>"Karen Maruyama", "character"=>"Gawker #1", "movie_id"=>680}, {"name"=>"Kathy Griffin", "character"=>"Hit-and-run Witness", "movie_id"=>680}, {"name"=>"Venessia Valentino", "character"=>"Pedestrian / Bonnie Dimmick", "movie_id"=>680}, {"name"=>"Linda Kaye", "character"=>"Shot Woman", "movie_id"=>680}, {"name"=>"Stephen Hibbert", "character"=>"The Gimp", "movie_id"=>680}, {"name"=>"Alexis Arquette", "character"=>"Man No. 4", "movie_id"=>680}, {"name"=>"Julia Sweeney", "character"=>"Raquel", "movie_id"=>680}, {"name"=>"Lawrence Bender", "character"=>"Long Hair Yuppy Scum", "movie_id"=>680}, {"name"=>"Cie Allman", "character"=>"Winston Wolfe's Girlfriend At Party (uncredited)", "movie_id"=>680}, {"name"=>"Rene Beard", "character"=>"Bar Tender (uncredited)", "movie_id"=>680}, {"name"=>"Lori Pizzo", "character"=>"Lucky Lady (uncredited)", "movie_id"=>680}, {"name"=>"Glendon Rich", "character"=>"Drug Dealer (uncredited)", "movie_id"=>680}, {"name"=>"Devan Richardson", "character"=>"Hopalong Cassidy (uncredited)", "movie_id"=>680}, {"name"=>"Ani Sava", "character"=>"Woman in Bathroom (uncredited)", "movie_id"=>680}], "actor"=>{}}
User Load (2.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "actors" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2016-01-12 15:15:35.503383"], ["updated_at", "2016-01-12 15:15:35.503383"]]
(11.9ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 31ms (ActiveRecord: 15.0ms)
。以下是从表中删除所有数据的三种方法:
truncate
有什么区别? truncate table t
delete from t
drop t
完全从数据库中删除表。这包括所有触发器,约束,索引等。该表不存在,因此您可以根据需要重新创建它。
Drop
和Delete
删除表中的所有行,同时保留表的结构。这对于清空表,同时保留触发器,索引,约束等非常方便。区别在于Truncate
记录所有事务(假设底层数据库/存储引擎都这样做)。因此,它更慢。 delete
不进行日志记录,也不会触发删除触发器,因此通常会更快。
这是一个简化的解释。 documentation很好地解释了Truncate
和truncate
之间的差异。
答案 3 :(得分:0)
使用truncate通常要快得多,因为它会使表为零,但它会阻塞,因为它是DDL。阅读更多关于TRUNCATE
和一些缺点(例如,它无法撤消)
如果您希望继续写入并且您有足够的数据,那么您可能会做得更好:
create table table_name_new like table_name;
rename table_name to table_name_YYYYMMDD, table_name_new to table_name;
然后,您可以备份数据,也可以完全删除表,而不会影响对原始表的写入。
答案 4 :(得分:0)
SELECT,UPDATE或DELETE语句中的WHERE标识要将操作应用于哪些行 - 因此您只需要一个标识所有行的WHERE子句。 (请注意,对于MySQL,LIMIT子句也会限制应用操作的行数。)
通常通过完全省略WHERE子句对所有行应用操作 - 这相当于应用对所有行都为true的过滤条件。
但是,从表中删除每一行比明确要求数据库释放分配给表的存储空间要慢得多 - 因此大多数SQL rDBMS也实现了TRUNCATE command。
删除并重新创建表也是一种有效的解决方案(IIRC在Oracle TRUNCATE上更快)。
答案 5 :(得分:0)
index.html:
<form method = "post" action = "delete.php">
<input name="delete" type="submit" id="delete" value="Delete">
</form>
删除所有记录
delete.php: $ query =(&#34; DELETE FROM numtable&#34;);
删除所选记录
$query = ("DELETE FROM numtable [Where Clause] ");