Chapter 8 Creating Tables and Enforcing Data Integrity Flashcards
What is the main method used for storing data?
Tables (Base tables).
When you query a database for data, that data is located in tables.
How do tables work with the SQL Server backup/restore process?
When you back up a database, all its tables are backed up, and when you restore the database, all those tables are restored with the same data they had when the backup occurred.
What are the system tables?
They store system data for SQL Server in specially reserved tables called system tables.
What is a temporary table?
Tables that exist in tempdb and last only as long as a session or scope referencing them endures.
What are table variables?
Variables that can store table data but only for the duration of a T-SQL batch.
What are views?
Views appear just like tables but they do not store any data. The are derived from queries against tables.
What are indexed views?
Indexed views store data but are defined as views and are updated whenever the base tables are updated.
What are derived tables and table expressions?
Subqueries that are referenced like tables in queries.
What are the two ways to create a table in T-SQL?
- CREATE TABLE: A statement where you explicitly define the components of the table.
- SELECT INTO: A statement which creates a table automatically by using the output of a query for the basic table definition.
What options are allowed in the CREATE TABLE statement?
- Schema name
- Table name
- Table columns
- For each column: data type, data type length/precision, special types of columns (computed, IDENTITY), collation
- Constraints,
- Storage options (file group, partition schema, table compression).
What is a database schema?
A database schema is a named container, or namespace,
It is used to group tables and objects.
A database schema allows tables with the same name to belong to the same database.
The database schema is part of the table’s name and helps identify the table.
It is a best practice to use both Schema and Table/Object Name.
What happens if you create a table without specifying the database schema?
SQL Server will fill in the database schema with your user name’s default schema.
What are four built in database schemas that cannot be dropped?
- dbo - The default database schema for new objects created by users having db_owner or db_ddl_admin roles.
- guest - The schema used to contain objects that would be available to the guest user - rarely used.
- INFORMATION_SCHEMA - This schema is used by the Information Schema views which provide ANSI standard access to metadata,
- sys - The sys schema is reserved by SQL Server for system objects such as system tables and views.
- There are additional database schemas named after the built-in database roles.
Can you create schemas that aren’t linked to users?
Yes.
Starting with SQL 2005, you can create schemas that have no intrinsic relationship to users and can serve to give a finer-grained permissions structure to the tables of a database.
Can a database schema contain another database schema?
No.
There can only be one level of database schema; one schema cannot contain another.
How many users can own a database schema?
A database schema is (and must be) owned by exactly one authorized database user.
The database schema owner grants permissions to other users for objects in the schema.
One user can own many database schemas.
How do you create a database schema and define who owns it?
CREATE SCHEMA Production AUTHORIZATION [dbo];
How can you move a table from one schema to another?
ALTER SCHEMA Sales TRANSFER Production.Categories;
This moves the table, Production.Categories, into the Sales schema.
It becomes Sales.Categories.
To move it back, use:
ALTER SCHEMA Production TRANSFER Sales.Categories;
What are SQL Server identifiers?
What are the length restrictions on SQL Server identifiers?
- The database object name is referred to as its identifier. Everything in Microsoft SQL Server can have an identifier. Servers, databases, and database objects, such as tables, views, columns, indexes, triggers, procedures, constraints, and rules, can have identifiers. Identifiers are required for most objects, but are optional for some objects such as constraints.
- Both regular and delimited identifiers must contain from 1 through 128 characters. For local temporary tables, the identifier can have a maximum of 116 characters.
What are the two types of SQL Server identifiers?
- Regular
- Regular identifiers are names that follow a set of rules and don’t need to be surrounded by delimiters like square brackets ([]) or quotes (‘’).
- Delimited
- Delimited identifiers are names that do not adhere to the rules for regular identifiers. You must use delimiters in order to reference them.
What are the rules for regular identifiers?
- The first character is a letter in the range A-Z (upper or lower), _, @, or #.
- After the first character, characters can include letters, numbers, @, $, #, _.
- Must not have spaces.
- Must not be a reserved keyword in SQL.
- Variables must begin with the at (@) sign
- Temporary tables must begin with the number sign (#).
What are the rules for delimited identifiers?
There is no restriction on what characters can be embedded in them as long as they are delimited, e.g. [Yesterday’s News].
What’s the difference between the brackets and quotes delimiters?
- Quotes are the ANSI SQL standard.
- In T-SQL the use of quotes requires SET QUOTED_IDENTIFIER is set to ON
- The SQL Server default is SET QUOTED_IDENTIFIER ON;
- Since it is possible for the setting to be OFF, using quotation marks is risky.
- Square brackets are the T-SQL Standard
Why is it a best practice to use regular identifiers?
If one of your users does not use the delimiters in a query, their query will still succeed.
When should you use NVARCHAR vs NCHAR data types?
For character strings that vary in length use the NVARCHAR or VARCHAR data type
For character strings that are frequenntly updated or short use NCHAR or CHAR.
When should you use DATETIME vs DATETIME2 data types?
The DATE, TIME, and DATETIME2 data types can store data more efficiently and with better precision than DATETIME and SMALLDATETIME.
When should you use TEXT, NTEXT, and IMAGE data types?
Never.
They are deprecated.
Use the VARCHAR(MAX), NVARCHAR(MAX), and VARBINARY(MAX).
When should you use DECIMAL/NUMERIC vs FLOAT/REAL?
DECIMAL and NUMERIC are the same data type. DECIMAL is prefered because the name is a bit more descriptive.
Use DECIMAL/NUMERIC instead of FLOAT/REAL data types unless you really need floating-point precision and are familiar with the error and possible rounding issues.
When should you use ROWVERSION vs TIMESTAMP?
Never use TIMESTAMP. It is deprecated.
Use ROWVERSION to version-stamp table rows. It is just an incrementing number and does not preserve a date or time.
ROWVERSION is a data type that exposes an automatically generated, unique binary number within a database. The storage size is 8 bytes. The rowversion data type is just an incrementing number and does not preserve a date or a time.
Each database has a counter that is incremented for each insert or update operation that is performed on a table that contains a rowversion column within the database. This counter is the database rowversion. This tracks a relative time within a database, not an actual time that can be associated with a clock. A table can have only one rowversion column. Every time that a row with a rowversion column is modified or inserted, the incremented database rowversion value is inserted in the rowversion column. This property makes a rowversion column a poor candidate for keys, especially primary keys. Any update made to the row changes the rowversion value and, therefore, changes the key value. If the column is in a primary key, the old key value is no longer valid, and foreign keys referencing the old value are no longer valid.
What should you use if you don’t want to allow NULL in the column but you do want to specify some default value to indicate the column has not been populated?
Use a DEFAULT constraint by adding the DEFAULT clause right after saying NOT NULL,
CREATE TABLE Production.Categories
(
description VARCHAR(200) NOT NULL DEFAULT (‘’)
)