SQL Lab 3
SQL Lab 3
TASKS
The SQL SELECT Statement
SELECT column_name,column_name
FROM table_name;
and
Demo Database
The following SQL statement selects the "CustomerName" and "City" columns from the
"Customers" table:
Example
SELECT CustomerName,City FROM Customers;
SELECT * Example
The following SQL statement selects all the columns from the "Customers" table:
Example
SELECT * FROM Customers;
The first form does not specify the column names where the data will be inserted, only their
values:
The second form specifies both the column names and the values to be inserted:
Demo Database
89 White Clover Karl Jablonski 305 - 14th Ave. S. Seattle 98128 USA
Markets Suite 3B
Example
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
The selection from the "Customers" table will now look like this:
89 White Clover Karl Jablonski 305 - 14th Ave. Seattle 98128 USA
Markets S. Suite 3B
The following SQL statement will insert a new row, but only insert data in the
"CustomerName", "City", and "Country" columns (and the CustomerID field will of course
also be updated automatically):
Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
The selection from the "Customers" table will now look like this:
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Seattle 98128 USA
Suite 3B
Demo Database
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Assume we wish to delete the customer "Alfreds Futterkiste" from the "Customers" table.
Example
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
It is possible to delete all rows in a table without deleting the table. This means that the
table structure, attributes, and indexes will be intact:
DELETE FROM table_name;
or
Note: Be very careful when deleting records. You cannot undo this statement!
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Demo Database
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Example
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
The selection from the "Customers" table will now look like this:
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Update Warning!
Be careful when updating records. If we had omitted the WHERE clause, in the example
above, like this:
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg';
EXPECTED DELIVERABLE