Data Manipulation Using SQL
Data Manipulation Using SQL
Objective
Like many Transact-SQL statements, the CREATE DATABASE statement has a required
parameter: the name of the database. CREATE DATABASE also has many optional parameters,
such as the disk location where you want to put the database files. When you
execute CREATE DATABASE without the optional parameters, SQL Server uses default values
for many of these parameters.
1. In a Query Editor window, type but do not execute the following code:
SQLCopy
CREATE DATABASE TestData
GO
2. Use the pointer to select the words CREATE DATABASE, and then press F1. The CREATE
DATABASE topic in SQL Server Books Online should open. You can use this technique
to find the complete syntax for CREATE DATABASE and for the other statements that
are used in this tutorial.
3. In Query Editor, press F5 to execute the statement and create a database
named TestData.
When you create a database, SQL Server makes a copy of the model database, and
renames the copy to the database name. This operation should only take several seconds,
unless you specify a large initial size of the database as an optional parameter.
To create a table, you must provide a name for the table, and the names and data types
of each column in the table. It is also a good practice to indicate whether null values are
allowed in each column. To create a table, you must have the CREATE TABLE permission,
and the ALTER SCHEMA permission on the schema that will contain the table.
The db_ddladmin fixed database role has these permissions.
Most tables have a primary key, made up of one or more columns of the table. A
primary key is always unique. The Database Engine will enforce the restriction that any
primary key value cannot be repeated in the table.
In a Query Editor window, type and execute the following code to change your
connection to the TestData database.
SQLCopy
USE TestData
GO
In a Query Editor window, type and execute the following code to create a table
named Products. The columns in the table are named ProductID, ProductName, Price,
and ProductDescription. The ProductID column is the primary key of the
table. int, varchar(25), money, and varchar(max) are all data types. Only
the Price and ProductionDescription columns can have no data when a row is inserted or
changed. This statement contains an optional element (dbo.) called a schema. The schema
is the database object that owns the table. If you are an administrator, dbo is the default
schema. dbo stands for database owner.
SQLCopy
CREATE TABLE dbo.Products
(ProductID int PRIMARY KEY NOT NULL,
ProductName varchar(25) NOT NULL,
Price money NULL,
ProductDescription varchar(max) NULL)
GO
Now that you have created the Products table, you are ready to insert data into the table
by using the INSERT statement. After the data is inserted, you will change the content of
a row by using an UPDATE statement. You will use the WHERE clause of the UPDATE
statement to restrict the update to a single row. The four statements will enter the
following data.
The basic syntax is: INSERT, table name, column list, VALUES, and then a list of the
values to be inserted. The two hyphens in front of a line indicate that the line is a
comment and the text will be ignored by the compiler. In this case, the comment
describes a permissible variation of the syntax.
1. Execute the following statement to insert a row into the Products table that was
created in the previous task.
SQLCopy
-- Standard syntax
INSERT dbo.Products (ProductID, ProductName, Price, ProductDescription)
VALUES (1, 'Clamp', 12.48, 'Workbench clamp')
GO
Note
If the insert fails, it may be because the Product table already has a row with that product
ID in it. To proceed, delete all the rows in the table and repeat the preceding
step. TRUNCATE TABLE deletes all the rows in the table.
Run the following command to delete all the rows in the table:
SQLCopy
TRUNCATE TABLE TestData.dbo.Products;
GO
After you truncate the table, repeat the INSERT command in this step.
2. The following statement shows how you can change the order in which the
parameters are provided by switching the placement of
the ProductID and ProductName in both the field list (in parentheses) and in the
values list.
SQLCopy
-- Changing the order of the columns
INSERT dbo.Products (ProductName, ProductID, Price, ProductDescription)
VALUES ('Screwdriver', 50, 3.17, 'Flat head')
GO
3. The following statement demonstrates that the names of the columns are optional,
as long as the values are listed in the correct order. This syntax is common but is
not recommended because it might be harder for others to understand your
code. NULL is specified for the Price column because the price for this product is
not yet known.
SQLCopy
-- Skipping the column list, but keeping the values in order
INSERT dbo.Products
VALUES (75, 'Tire Bar', NULL, 'Tool for changing tires.')
GO
4. The schema name is optional as long as you are accessing and changing a table in
your default schema. Because the ProductDescription column allows null values
and no value is being provided, the ProductDescription column name and value
can be dropped from the statement completely.
SQLCopy
-- Dropping the optional dbo and dropping the ProductDescription column
INSERT Products (ProductID, ProductName, Price)
VALUES (3000, '3 mm Bracket', 0.52)
GO
Type and execute the following UPDATE statement to change the ProductName of the
second product from Screwdriver, to Flat Head Screwdriver.
SQLCopy
UPDATE dbo.Products
SET ProductName = 'Flat Head Screwdriver'
WHERE ProductID = 50
GO
Use the SELECT statement to read the data in a table. The SELECT statement is one of
the most important Transact-SQL statements, and there are many variations in the
syntax. For this tutorial, you will work with five simple versions.
1. Type and execute the following statements to read the data in the Products table.
SQLCopy
-- The basic syntax for reading data from a single table
SELECT ProductID, ProductName, Price, ProductDescription
FROM dbo.Products
GO
2. You can use an asterisk (*) to select all the columns in the table. The asterisk is for
ad hoc queries. In permanent code, provide the column list so that the statement
returns the predicted columns, even if a new column is added to the table later.
SQLCopy
-- Returns all columns in the table
-- Does not use the optional schema, dbo
SELECT * FROM Products
GO
3. You can omit columns that you do not want to return. The columns will be
returned in the order that they are listed.
SQLCopy
-- Returns only two of the columns from the table
SELECT ProductName, Price
FROM dbo.Products
GO
4. Use a WHERE clause to limit the rows that are returned to the user.
SQLCopy
-- Returns only two of the records in the table
SELECT ProductID, ProductName, Price, ProductDescription
FROM dbo.Products
WHERE ProductID < 60
GO
5. You can work with the values in the columns as they are returned. The following
example performs a mathematical operation on the Price column. Columns that
have been changed in this way will not have a name unless you provide one by
using the AS keyword.
SQLCopy
-- Returns ProductName and the Price including a 7% tax
-- Provides the name CustomerPays for the calculated column
SELECT ProductName, Price * 1.07 AS CustomerPays
FROM dbo.Products
GO
Reference:
https://docs.microsoft.com/en-us/sql/t-sql/lesson-1-creating-database-objects?view=sql-server-ver15