sql-delete

PHP MySQL Delete parent and child rows

馋奶兔 提交于 2019-12-07 06:01:01
问题 I have 1 MySQL Table. It looks like this: +---------+-------------+--------+ | item_id | parent_id | Name | +---------+-------------+--------+ | 1 | 0 | Home | +---------+-------------+--------+ | 2 | 1 | Sub | +---------+-------------+--------+ | 3 | 2 | SubSub | +---------+-------------+--------+ If I DELETE item_id 1, I want to delete the rest of the sub also but how can I do it? I have tried the Foreign Key but it works only if you have 2 tables?? I hope someone can help me in MySQL maybe

Sql Server Delete and Merge performance

自古美人都是妖i 提交于 2019-12-07 01:45:32
问题 I've table that contains some buy/sell data, with around 8M records in it: CREATE TABLE [dbo].[Transactions]( [id] [int] IDENTITY(1,1) NOT NULL, [itemId] [bigint] NOT NULL, [dt] [datetime] NOT NULL, [count] [int] NOT NULL, [price] [float] NOT NULL, [platform] [char](1) NOT NULL ) ON [PRIMARY] Every X mins my program gets new transactions for each itemId and I need to update it. My first solution is two step DELETE+INSERT: delete from Transactions where platform=@platform and itemid=@itemid

MySQL insert on duplicate key; delete?

梦想与她 提交于 2019-12-06 19:34:52
问题 Is there a way of removing record on duplicate key in MySQL? Say we have a record in the database with the specific primary key and we try to add another one with the same key - ON DUPLICATE KEY UPDATE would simply update the record, but is there an option to remove record if already exists? It is for simple in/out functionality on click of a button. 回答1: It's a work-around, but it works: Create a new column and call it do_delete , or whatever, making it a tiny-int. Then do On Duplicate Key

MySQL Trigger - delete after update

北城以北 提交于 2019-12-06 13:27:23
For test case in one project I must delete record if one of fields meets one of conditions. Easiest seems be simple trigger: delimiter $$ drop trigger w_leb_go $$ create trigger w_leb_go after update on test for each row begin set @stat=NEW.res; set @id=NEW.id; IF @stat=3 THEN delete from test where id=@id; END IF; end$$ delimiter ; But as I suspect got error: update test set res=3 where id=1; ERROR 1442 (HY000): Can't update table 'test' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. How else can be done only by having access to

php sql delete button

偶尔善良 提交于 2019-12-06 11:37:46
问题 I'm trying to make a delete button from sql. I started a function called $del but I don't know how to complete it, in the form of a delete button echoe'd out beside the current echo statements. $con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $sql = "INSERT INTO camps (city, map, park, day, details) VALUES ('$_POST[city]','$_POST[map]','$_POST[park]','$_POST[day]','$_POST[details]')"; if (!mysql

How to delete records in DB with mySQL using group by [duplicate]

吃可爱长大的小学妹 提交于 2019-12-06 10:47:23
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: SQL Delete: can't specify target table for update in FROM clause I have one table only (call this table TAB), representing University exams. I have the following attributes: CourseName, CourseCode and year. I want to delete all courses that have a cardinality less than 100. If I type select CourseName from TAB group by CourseName having count(CourseName) < 100; I have an exact result. But if I want to delete

ON CASCADE DELETE on JPA2 many-to-many relationship

南楼画角 提交于 2019-12-06 05:38:35
问题 I have read a lot of topics regarding cascading and many-to-many associations, but I haven't been able to find an answer to my particular question. I have a many-to-many relationship between UserProfiles and Roles. When I remove a UserProfile I want the associated records in the join table (userprofile2role) to be removed by the database, so with an actual SQL 'ON DELETE CASCADE' action. Is this possible? Whatever I try, Hibernate always creates the UserProfile table without specifying ON

delete duplicate entries in table [duplicate]

帅比萌擦擦* 提交于 2019-12-06 03:48:38
This question already has answers here : How to delete Duplicates in MySQL table (4 answers) Closed 6 years ago . I want to delete multiple duplicate keys from the below table: id | name | uid 1 | ekta | 5 2 | ekta | 5 3 | sharma | 10 4 | sharma | 10 want it to be like id | name | uid 1 | ekta | 5 3 | sharma | 10 I am using mysql. Is it ossible.? I can't use unique constraint query to make unique enteries because i want this duplicate entries ones entered to the table. One way of doing this is by joining the table on a subquery using LEFT JOIN . The subquery gets the lowest ID for every UID .

Postgresql delete multiple rows from multiple tables

若如初见. 提交于 2019-12-06 02:22:05
问题 Consider 2 or more tables: users (id, firstname, lastname) orders (orderid, userid, orderdate, total) I wish to delete all users and their orders that match first name ' Sam '. In mysql, I usually do left join. In this example userid is unknown to us. What is the correct format of the query? 回答1: http://www.postgresql.org/docs/current/static/sql-delete.html DELETE FROM orders o USING users u WHERE o.userid = u.id and u.firstname = 'Sam'; DELETE FROM users u WHERE u.firstname = 'Sam'; You can

MySQL attempting to delete all rows which are not constrained by foreign key

廉价感情. 提交于 2019-12-05 18:07:24
Okay, this is (probably) a very simple question, but I am afraid I know almost no MySQL, so please put up with me. I'm just trying to delete every row from one table which is not constrained by a Foreign Key in another table - a specific table, there are only two tables involved here. The create statements look a bit like: CREATE TABLE `testschema`.`job` ( `Job_Id` int(10) unsigned NOT NULL AUTO_INCREMENT, `Comment` varchar(255) DEFAULT NULL, PRIMARY KEY (`Job_Id`) USING BTREE, ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; CREATE TABLE `ermieimporttest`.`jobassignment` (