Professional Documents
Culture Documents
• Use uppercase letters for the keywords, which inclues SELECT, FROM, and
WHERE. Use lowercase letters for the user-supplied words (SQL Server 2005 is
not case-sensitive for commands).
• Align the keywords SELECT, FROM, and WHERE on separate lines
Null means that data is missing or unavailable, so the cell has no value.
The AND is a way of combining conditions in a WHERE clause. An AND operator is used
in a WHERE clause if more that one condition is required.
the BETWEEN operator allows you to determine whether a value falls within a given
range of values (inclusive).
The BETWEEN operator can be negated by using the keyword NOT before the BETWEEN
operator.
IS NULL
Null values are used to designate missing data in columns. The IS NULL condition is
the only condition that directly tests for nulls. No value is considered to be equal to,
greater than, or less than NULL. Even a space is not considered to be a NULL, and a null
is not considered to be a space. Nulls are not considered like any other value in a table
either, since nulls do not have data types.
IS NOT NULL
To retrieve all rows that are not nulls, IS NOT NULL can be used.
For example, to see the first 10 rows of the Dependent table, you can type:
SET ROWCOUNT 10
SELECT *
FROM Dependent
After using ROWCOUNT, you should reset the ROWCOUNT property by:
SET ROWCOUNT 0
If you do not reset the ROWCOUNT property, you will keep getting whatever you set
your ROWCOUNT to for the remainder of this session
Using Aliases
Column aliases and table aliases are temporary names assigned within a query to
columns and tables respectively. They are created on the fly in a query, and do not
exist after the query is run.
To use more descriptive column headings, you can include column aliases just
before or after the column name by using AS in the SELECT statement
Synonyms
Table aliases are not permanent, in the sense that they do not exist after the query
has been executed. Synonyms are more permanent; they are available for use until
they are deleted.
SQL Server 2005 allows you to create synonyms for your tables. Synonyms are
usually shorter names that can be used in place of the table name.
DROP SYNONYM s1
You can also delete the synonym by right-clickingon the synonym and selecting
Delete.
Comments are nonexecutable words or phrases included in SQL queries to make the
queries easier to understand (particularly by other people).
There are two ways of including comments in SQL Server 2005. The first way is
by the use of dashes,
The second way of including comments in Server SQL 2005 is by the use of
/*...*/ construction.
Creating a Table
There are several ways to insert values into a table using SQL in SQL Server 2005.
We will illustrate the two most commonly used ways: using INSERT INTO ..
VALUES and using INSERT INTO .. SELECT.
You may INSERT a row with less than all the columns by naming the columns you
want to insert into, like this:
With the INSERT INTO .. VALUES option, you insert only one row at a time into a
table. With the INSERT INTO .. SELECT option, you may (and usually do) insert
many rows into a table at one time.
Another common command used for setting/changing data values in a table is the The
general format for the UPDATE command is:
UPDATE TableName
SET fieldname...
You may add columns to a table with little difficulty. The general syntax for adding a
column to a table is:
ALTER TABLE
Tablename
ADD column-name type
The following is the general syntax for deleting a column from a table:
Deleting a Table
The general syntax to delete or remove an entire table and its contents is:
DROP TABLE Tablename
Joins have to be performed on "compatible" columns; that is, a character column may
be joined to another character column, a numeric column may be joined to another
numeric column, and so forth. So, for example, a CHAR column can be joined to a
VARCHAR column (both being character columns), or an INT column can be joined to a
REAL column (both being numeric columns).
In a SQL statement, a Cartesian product is where every row of the first table in the
FROM clause is joined with each and every row of the second table in the FROM clause.
In SQL Server, a CROSS JOIN can be used to return a Cartesian product of two tables.
The form of the CROSS JOIN is:
SELECT *
FROM Table1 CROSS JOIN Table2
2. Outer Join: Outer join produces the results, which contains matched rows and
unmatched rows.
here we have three types of joins,
1.Left Outer Join 2.Right Outer Join 3.Full Outer Join
Left Outer Join: Left Outer Join producesses the results,
which contains all the rows from Left table and matched
rows from Right Table.
syntax: select * from table1<leftouterjoin>table2
Join Types:
1. INNER JOIN - Joins two tables and returns only those records that match
between the 2 tables.
2. LEFT OUTER JOIN - Joins two tables and returns all records from the first table
with or without matching records on the second table.
3. RIGHT OUTER JOIN - Joins two tables and returns all records from
the second table with or without matching records on the first table.
FULL OUTER JOIN - Joins two tables and returns all records from both
tables even if there's no matching record between them.
CROSS JOIN
In cross joins, each row from first table joins with all the rows of another table.
Strings
A string is a character or a combination of characters.
Temporary Tables
To create a temporary table, start its name with #,
followed by the desired name. Once the table has
been created, it would be available as long as you are
using the same connection to the server.
Deleting a Table
DROP TABLE TableName
Modifying a Column
ALTER TABLE TableName
ADD ColumnName Properties
Deleting a Column
ALTER TABLE TableName
DROP COLUMN ColumnName
DML
DDL
DDL is abbreviation of Data Definition Language. It is used to create and modify the
DCL
and referential integrity as well it is used to control access to database by securing it.
TCL
Built in functions
Different categories
Aggregate functions: perform operations that combine multiple values into one
value e.g, count(), sum(),avg()
String Attributes
To store such attributes, SQL Server 2005 provides four different datatypes: char,
nchar, varchar, and nvarchar.
Fixed and variable-length datatypes Another option to consider when using string
datatypes is whether to use fixed or variable-length datatypes. Fixed datatypes (char,
nchar) use a permanent amount of space regardless of the value of the column or
variable. For example, if you declare a column as type char(10) and it has the value of
“Test,” it will employ ten bytes, even though Test has only four letters. This occurs
because SQL Server 2005 will add six spaces after the word Test, thus always filling
in the ten characters. The same column defined as nchar(10) will employ twenty
bytes. This behavior is known as right padding.
Binary Data
Some applications need to store images (such as JPG, GIF, and BMP files) or
documents (such as Microsoft Excel workbooks or Microsoft Word documents). To
support those needs, SQL Server 2005 supports binary datatypes. Binary datatypes
are raw sequences of bytes that SQL Server 2005 does not try to encode or decode as
it does with string datatypes. Binary information can be stored in three datatypes:
binary(n), varbinary(n), and varbinary(max),