Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

contention. You could wrap your new tables in a transaction, but that would only tie up valuable locks for a process that really doesn’t need locking at all. Therefore, you do not wrap the statements that create the tables in transaction statements. Once the tables are added to the database, you need to add initial data to the Movie Format table and the Inventory table. Each of these processes entails writing SQL statements, and they depend on the previous operation succeeding or the next operation is not valid. For example, you need to build the Movie Format table and place two records in that table. The three formats are DVD, VHS, and Beta. If the statement that builds the table fails, obviously the process of placing the three records in the format table also fails. Again, transactions are not necessary, and you receive errors if the previous CREATE TABLE statements fail. Next, you need to build the Inventory table and then place several inventory records in the table, which tells you what movies are available to be rented and in what format. Then you can start renting movies. The process of renting movies consists of running a query to find out what is available, selecting a specific inventory item and marking it as rented, and building a new record in the Rentals table with the member ID, the inventory ID, and the date rented. Try It Out Renting Movies with Transactions Select all available inventory records so that you can pick a movie. To do this, you really should wrap the stuff that follows in a transaction. You are filling an order and don’t want the items you are modifying to also be modified by other users. 1. Remember that in ANSI SQL you have to specifically end the previous transaction in order to begin another transaction, so the first thing you do is run a COMMIT statement: COMMIT In Transact-SQL (SQL Server for example) this would not be required since you never explicitly started a transaction. In addition, for Transact-SQL you would need to explicitly start a transaction: BEGIN TRANSACTION 2. For non-Transact-SQL you do not need to run a BEGIN TRANSACTION statement because it is implicit. SELECT Inventory.InventoryId, Films.FilmName, Films.PlotSummary, Format.Format FROM (Inventory INNER JOIN Films ON Inventory.FilmId = Films.FilmId) INNER JOIN Format ON Inventory.FormatId = Format.FormatId WHERE (((Inventory.CheckedOut)= ‘n’)); 3. Select a specific movie and mark it as rented: UPDATE Inventory SET Inventory.CheckedOut = ‘Y’ WHERE (((Inventory.InventoryId)=7)); 4. Build a record in the Rentals table: INSERT INTO Rentals ( InventoryId, MemberId, DateOut ) SELECT 7 AS InventoryId, 3 AS MemberId, ‘3/21/2004’ AS dateout; Transactions 323

contention. You could wrap your new tables in a transaction, but that would only tie up valuable locks<br />

for a process that really doesn’t need locking at all. Therefore, you do not wrap the statements that create<br />

the tables in transaction statements. Once the tables are added to the database, you need to add initial<br />

data to the Movie Format table and the Inventory table. Each of these processes entails writing <strong>SQL</strong><br />

statements, and they depend on the previous operation succeeding or the next operation is not valid. For<br />

example, you need to build the Movie Format table and place two records in that table. The three formats<br />

are DVD, VHS, and Beta. If the statement that builds the table fails, obviously the process of placing<br />

the three records in the format table also fails. Again, transactions are not necessary, and you receive<br />

errors if the previous CREATE TABLE statements fail.<br />

Next, you need to build the Inventory table and then place several inventory records in the table, which<br />

tells you what movies are available to be rented and in what format. Then you can start renting movies.<br />

The process of renting movies consists of running a query to find out what is available, selecting a specific<br />

inventory item and marking it as rented, and building a new record in the Rentals table with the<br />

member ID, the inventory ID, and the date rented.<br />

Try It Out Renting Movies with Transactions<br />

Select all available inventory records so that you can pick a movie. To do this, you really should wrap<br />

the stuff that follows in a transaction. You are filling an order and don’t want the items you are modifying<br />

to also be modified by other users.<br />

1. Remember that in ANSI <strong>SQL</strong> you have to specifically end the previous transaction in order to<br />

begin another transaction, so the first thing you do is run a COMMIT statement:<br />

COMMIT<br />

In Transact-<strong>SQL</strong> (<strong>SQL</strong> Server for example) this would not be required since you never explicitly<br />

started a transaction.<br />

In addition, for Transact-<strong>SQL</strong> you would need to explicitly start a transaction:<br />

BEGIN TRANSACTION<br />

2. For non-Transact-<strong>SQL</strong> you do not need to run a BEGIN TRANSACTION statement because it is<br />

implicit.<br />

SELECT Inventory.InventoryId, Films.FilmName, Films.PlotSummary, Format.Format<br />

FROM (Inventory<br />

INNER JOIN Films ON Inventory.FilmId = Films.FilmId)<br />

INNER JOIN Format ON Inventory.FormatId = Format.FormatId<br />

WHERE (((Inventory.CheckedOut)= ‘n’));<br />

3. Select a specific movie and mark it as rented:<br />

UPDATE Inventory<br />

SET Inventory.CheckedOut = ‘Y’<br />

WHERE (((Inventory.InventoryId)=7));<br />

4. Build a record in the Rentals table:<br />

INSERT INTO Rentals ( InventoryId, MemberId, DateOut )<br />

SELECT 7 AS InventoryId, 3 AS MemberId, ‘3/21/2004’ AS dateout;<br />

Transactions<br />

323

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!