Is CRUD DDL or DML operation? - sql

Is CRUD classified as DDL or DML or both.
CRUD being the operation Create Read Update and Delete

CRUD operations when applied to an SQL database map directly to DML statements. You have to bear in mind that the "C" in CRUD corresponds to the INSERT statement in SQL which creates an instance of something in a table. There is a CREATE statement in SQL that used for DDL operations but this is not the same sense of "Create" as in CRUD.
Create = INSERT
Read = SELECT
Update = UPDATE
Delete = DELETE

I'd say they're closer to Data Manipulation Language, because DDL is more about defining a schema where as DML is about working with objects within a defined schema. I propose the following map between DML and CRUD (using standard insert, create, update, delete methods from dml):
Create -- Insert
Read -- Select
Update -- Update
Delete -- Delete
However, I don't know if thinking of it as either DDL or DML will help you very much. CRUD isn't a language so much as a set of operations for handling objects you wish to expose via some interface.

Related

DML segmentation by read/write?

In standard SQL definitions, DML is defined as CRUD-operations:
SELECT
INSERT
UPDATE
DELETE
Call, explain, merge, etc...
Within DML, is there a further distinction between what might be the 'query' (retrieving information) and what might be 'modification' (modifying information) ? For example, something like:
DQL (query)
SELECT
DWL (write)
INSERT, UPDATE, DELETE
Or does no such distinction exist and people just use the terms 'read' and 'write' ? Another good reference is: What are DDL and DML?.

Is the BEFORE Trigger statement executed before the DDL statement?

Is the BEFORE trigger statement executed before DDL statements or is it inside a DDL statement?
I'm building a small DBMS and currently developing the DDL, and I'm confused about handling the BEFORE statement.
Here's not a defined behaviour for it:
https://web.csulb.edu/colleges/coe/cecs/dbdesign/dbdesign.php?page=sql/ddldml.php
Oracle describes the behaviour but not what can come inside the preceding or following trigger or whether they can be DDL statements or not:
https://www.oracletutorial.com/plsql-tutorial/oracle-trigger/
"BEFORE" is part of the DDL statement that defines the trigger. It describes when the trigger fires in relation to a DML command on a table: i.e. BEFORE or AFTER the change to the data is actually made. This allows you to validate or modify the new data as it is being applied, or to add or modify dependent data like a child table after the parent table update is complete.
While you can technically embed DDL into the body of a trigger using "execute immediate", it is typically considered very bad practice to do so. Is there a specific use case you are considering for including DDL statements in a trigger?

How can Delete be both a DDL and a DML statement

I am currently reading the offical Microsoft book 'Database Administration Fundamentals' in preparation to sitting it's exam.
I understand what DDL and DML are but Microsoft show DELETE as being both a DDL and DML statement. I have googled this but I cannot anything that confirms or denies this.
A good reference to this is the question: What is DDL and DML Which shows it as a DML. Below is the segments from the book:
Data Manipulation Language (DML) is the language element that allows
you to use the core statements INSERT, UPDATE, DELETE, and MERGE to
manipulate data in any SQL Server tables. Core DML statements include
the following: • SELECT: Retrieves rows from the database and enables
the selection of one or many rows or columns from one or many tables
in SQL Server. • INSERT: Adds one or more new rows to a table or a
view in SQL Server. • UPDATE: Changes existing data in one or more
columns in a table or view. • DELETE: Removes rows from a table or
view. • MERGE: Performs insert, update, or delete operations on a
target table based on the results of a join with a source table.
the six main DDL statements are as follows: • USE: Changes the
database context. • CREATE: Creates a SQL Server database object
(table, view, or stored procedure). • ALTER: Changes an existing
object. • DROP: Removes an object from the database. • TRUNCATE:
Removes rows from a table and frees the space used by those rows. •
DELETE: Remove rows from a table but does not free the space used by
those rows removed.
Is the book out of date/ wrong. Can someone help shed light on this I see conflicting lists of what are the full DDL and DML statements.
I agree with you, DELETE is DML. Moreover, I dare say, TRUNCATE should also be considered DML, since logically is equivalent to a DELETE statement. The fact that TRUNCATE is a DROP and CREATE is not enough in my opinion to justify assigning it to DDL, since the two together, carried out as one atomic operation, do not affect the schema of the database.

Is it possible to create a trigger that creates another trigger

Is it possible to have a trigger that creates another trigger?
For example, I have table_1 with a column table_name. On inserting into table_1 the first trigger would create a trigger for the table inserted as table_name? I have not found much info on this.
Trigger enables you to run dynamic SQL, in which you can create what objects you like, including other triggers (assuming you have the right permissions).
You can do that by using sp_executesql, it executes a Transact-SQL statement or batch that can be reused many times, or one that has been built dynamically. The Transact-SQL statement or batch can contain embedded parameters
This seems tough as a (very) bad design. So go ahead and try that (or not*).
*You can state your problem to begin with, may be in programmers (if it's more a design oriented question) or in Database Administrators, and you may get better help.

Determine which table was updated in database level trigger

I am creating a database level trigger which should only perform a certain action based on which tables were updated.
With a regular trigger I would just use IF UPDATED(column).
Is there some way to determine not just the column that was updated but also the table?
You can't create DDL TRIGGER ON UPDATE
CREATE TRIGGER
DDL triggers, like standard triggers, execute stored procedures in
response to an event. But unlike standard triggers, they do not
execute in response to UPDATE, INSERT, or DELETE statements on a table
or view. Instead, they primarily execute in response to data
definition language (DDL) statements. These include CREATE, ALTER,
DROP, GRANT, DENY, REVOKE, and UPDATE STATISTICS statements. Certain
system stored procedures that perform DDL-like operations can also
fire DDL triggers.