Cascade delete for sql tables

We can use cascade delete on property for deleting data from child tables. With cascade delete on, the corresponding data gets deleted from the child table when the data from the parent table gets deleted. This saves a lot of time writing the code for deleting data from child tables. Also it can overall improve your application’s performance.

Illustration:-

parenttable

Deleting data with parentid=2 from the parent table (see above) will result in deletion of corresponding data from the child table(see below). This deletion will be handled automatically by the sql server.

childtable

You can add the cascade delete on property to the child table by this method:-

alter table tblchild 

add constraint fk_parentchild foreign key (parentidFK) references tblparent(parentid)

on delete cascade;

merging 2 or more recordsets in sql.

We can merge 2 or more recordsets in sql. Also we can determine in what order the records should appear.. The rough syntax for merging 3 query resultsets is as follows…

(
SELECT
1
, [Colnames]
FROM
a
WHERE
a.col= @somecolval1
)

UNION ALL

(
SELECT
2
, [Colnames]
FROM
b

WHERE
b.col = @somecolval2

)

UNION ALL

(
SELECT
3
, [Colnames]
FROM
c
WHERE
c.col=@somecolval3
)

ORDER BY 1,2,3

One thing to note here is that the number of col names returned should be same for each single query…