Creating and Altering Tables: Objective
Creating and Altering Tables: Objective
LAB # 4
CREATING AND ALTERING TABLES
OBJECTIVE
Creating and altering tables
THEORY
Creating a Table
When you started this section, you looked at the standard CREATE syntax of:
And then you moved on to a more specific start (indeed, it’s the first line of the statement that creates the table) and created a
table called Customers:
INTO is optional and columns list is also optional and may be omitted if you list values in the default order of the columns in the
table.
This result could also have been achieved by using this query if the column order is default:
Explicit Method: Specify the NULL keyword or empty string (‘ ‘) in the values list.
Here an employee names Shahnawaz has been added to the employee table, it uses the GETDATE() function for current
date and time. To confirm the addition of new employee data into the employee’s table, type this query into SSMS:
Here we have declared three variables @dept_number, @dept_name, and @dept_location with data types as int, nvarchar
(50) and nvarchar (50) respectively. Before using them into our INSERT statement we have set their values accordingly as
90, ‘Training’, and ‘Karachi’. Now we can use them into an insert statement and run them concurrently/simultaneously.
The above example is treated as a script that is you are running the entire script or nothing at all. They are all working in
unison to accomplish one task—to insert one record into the database.
Check it existence by running the following query:
Where: table is the table name where insert statement is sought for
Column_list name of the columns in the table to be populated
Subquery subquery that returns values into the table
Here Bonus table has been populated by a SELECT statement from Employee table using the INSERT statement. Check
the Bonus table by using this query.
a. Consider the following query: Specific row(s) can be updated while using the WHERE clause.
update employee set dno=10 where eno=259
This query updates the department number information in the employee table from 30 to 10.
i. Consider the following query: All rows in the table will be updated, if we omit the WHERE clause.
update temp set Dno=20
Temp table has the same data as of the Employee table. You will be provided this in the lab.
Updating with Multiple Columns Subquery
Multiple column sub-query can be used to be implemented in the SET clause to update a row.
Syntax
UPDATE table
SET (column, column,) =
(SELECT column, column, …
FROM table
WHERE condition
This changes the department number of all employees in the temp table with that of the employee number 104 in the Employee
table and vice versa his job title.
Removing Data With The DELETE Statement
You can delete records of table by the DELETE statement.
Syntax
DELETE FROM table
WHERE condition
Here FROM keyword is optional. Always remember that all the rows of the table will be deleted if not mentioning the WHERE
clause.
Deleting Rows from a Table
a. Consider the following query: Specific row(s) can be deleted while using the WHERE clause.
delete from department where dname='development'
b. Consider the following query: All rows in the table will be deleted, if we omit the WHERE clause.
delete from dept