From SQL DELETE
Most database management systems allow you to create a foreign key constraint so that if you delete a row in a table, the corresponding rows the related table are also removed automatically. This ensures the integrity of the data.
AND
Answered in SO. (Must be adapted to your situation)
Can't you just separate them by a semicolon?
Delete from messages where messageid = '1';
Delete from usersmessages where messageid = '1'
OR
Just use INNER JOIN as below
DELETE messages , usersmessages FROM messages INNER JOIN usersmessages
WHERE messages.messageid= usersmessages.messageid and messages.messageid = '1'
EDIT due to @NIC's comment (and DV?):
First option: table_constraint (Transact-SQL)
ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } Specifies
what action happens to rows in the table that is altered, if those
rows have a referential relationship and the referenced row is deleted
from the parent table. The default is NO ACTION.
...
CASCADE Corresponding rows are deleted from the referencing table if that row is deleted from the parent table.
...
and an example in SO
Second option:
prepare tables
DROP TABLE IF EXISTS dbo.Student, dbo.Winner
CREATE TABLE dbo.Student
(
ID INT NOT NULL,
name VARCHAR(30) NOT NULL,
class VARCHAR(30) NOT NULL
);
CREATE TABLE dbo.Winner
(
stID INT NOT NULL,
achievement INT NOT NULL
);
GO
INSERT INTO dbo.Student(ID, name, class)
VALUES
(1, 'Roy', '2'),
(2, 'James', '2'),
(3, 'Carl', '2'),
(4, 'Peter', '2'),
(5, 'Alice', '2')
INSERT INTO dbo.Winner(stID, achievement)
VALUES
(1, 1),
(2, 1),
(3, 3),
(4, 5),
(5, 5)
Deleting rows
DELETE FROM Student WHERE ID = 1;
DELETE FROM Winner WHERE stID = 1
Tested in SQL Server 13.0.1722.0
note: DROP TABLE IF EXIST may not work in older versions
