Data Modification Flashcards
What are the most common SQL statements associated with Data Manipulation Language?
SELECT, INSERT, UPDATE, DELETE, TRUNCATE, and MERGE.
What does the acronym DML stand for?
Data Manipulation Language
Name the statements which T-SQL provides for inserting data into tables.
INSERT VALUES, INSERT SELECT, INSERT EXEC, SELECT INTO, and BULK INSERT.
Describe the INSERT VALUES statement.
This statement is used to insert rows into a table based on specified values.
Example:
INSERT INTO dbo.Orders (orderid, orderdate, empid, custid) VALUES (10003, '20160213', 4, 'B'), (10004, '20160214', 1, 'A'), (10005, '20160213', 1, 'C'), (10006, '20160215', 3, 'C');
A single row or multiple rows can be inserted.
Describe the INSERT SELECT statement.
This statement inserts a set of rows returned by a SELECT query into a target table.
Example:
INSERT INTO dbo.Orders(orderid, orderdate, empid, custid)
SELECT orderid, orderdate, empid, custid
FROM Sales.Orders
WHERE shipcountry = N’UK’;
Describe the INSERT EXEC statment
This statement inserts a set of rows returned by a stored procedure or a dynamic SQL batch into a target table.
Example:
INSERT INTO dbo.Orders(orderid, orderdate, empid, custid)
EXEC Sales.GetOrders @country = N’France’;
Describe the SELECT INTO statement.
The SELECT INTO statement is a nonstandard T-SQL statement that creates a target table and populates it with the result set of a query. This statement cannot be used to insert data into an existing table; additionally, the target table’s structure and data are based on the source table. The SELECT INTO statement copies from the source the base structure (such as column names, types, nullability, and identity property) and the data. It does not copy from the source constraints, indexes, triggers, column properties such as SPARSE and FILESTREAM, and permissions.
Example:
SELECT country, region, city
INTO dbo.Locations
FROM Sales.Customers
Describe the BULK INSERT statement.
This statement is used to insert into an existing table, data originating from a file. There are many options which may be specified when using this statement so refer to documentation if this statement is needed.
Example:
BULK INSERT dbo.Orders FROM 'c:\temp\orders.txt' WITH ( DATAFILETYPE = 'char', FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' );
Name the two solutions which SQL Server supports to automatically generate numeric keys.
The identity column property and the sequence object.
Describe how the Identity property is used.
Example:
CREATE TABLE ( keycol INT NOT NULL IDENTITY(1, 1) name VARCHAR NOT NULL )
Identity is a standard column property. You can define this property for a column with any numeric type with a scale of zero (no fraction). When defining the property, you can optionally specify a seed (the first value) and an increment (a step value). If you don’t provide those, the default is 1 for both. You typically use this property to generate surrogate keys, which are keys that are produced by the system and are not derived from the application data.
Describe how to select the Identity column without providing the column name and without using the *
SELECT $identity FROM
What does @@identity return?
The last identity value generated by the session regardless of scope.
What does SCOPE_IDENTITY() return?
This function returns the last identity value generated by the current scope. This function is preferred over @@identity.
What does IDENT_CURRENT() return?
provides the last identity value assigned to a table regardless of session.
True or False: the identity property can be added or removed from an existing column.
False.