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.
[START WITH
[INCREMENT BY
[MINVALUE
[MAXVALUE
[CYCLE | NO CYCLE]
[CACHE value | NO CACHE];
The syntax works as follows:
- Data types can be Decimal, Int, SmallInt, TinyInt, and BigInt.
- The default value for the data type is BigInt