230 likes | 459 Views
Tables. Lesson 6. Skills Matrix. Tables. Tables store data. Tables are relational They store data organized as row and columns.
E N D
Tables Lesson 6
Tables • Tables store data. • Tables are relational • They store data organized as row and columns. • Data can be retrieved efficiently because the RDBMS can locate a specific field (the intersection of a row and column) without having the read the entire table (as in a flat-file database system).
Creating Tables • Open SQL Server Management Studio. In Object Explorer, expand ServerDatabasesMSSQL_Training. • Right-click the Tablesicon and select New Table to open the Table Designer. • Tables are often created using SQL scripts containing CREATE TABLE commands.
Options when Creating Tables • Indexes: Specifies the type of data indexing initially created. • Collation: Individual columns of textual data may have a different collation setting than that of the server instance or the database. • Compression: A table option that can control optional ROW or PAGE type data compression.
Options when Creating Tables • Constraint: Table and column level constraints may be specified. • Sparse: Individual columns may be defined as SPARSE in order to conserve storage space for columns often containing a high percentage of NULL data.
Data Compression • SQL Server 2008 includes two new data compression features for reducing the disk space requirements – Row Compression and Page Compression. • Only one type of compression can be specified at a time on the same object. • Compression can be used on both regular tables and nonclustered indexes. • The space savings will, as with all forms of data compression, depend upon the nature of the data being compressed.
Data Compression • Data Compression is available only in the Enterprise, Developer, or Evaluation Editions of SQL Server 2008.
Data Compression • A new stored procedure named sp_estimate_data_compression_savings has been provided with SQL Server 2008 to provide estimated space savings without having to actually compress a table first. • This stored procedure needs a table or index name and either ‘ROW’ or ‘PAGE’ as a compression method.
Data Compression • Both row based and page based compression are enabled via either the CREATE TABLE, CREATE INDEX, ALTER TABLE, or ALTER INDEX commands. ALTER TABLE mytable REBUILD WITH (DATA_COMPRESSION = ROW); ALTER TABLE mytable REBUILD WITH (DATA_COMPRESSION = PAGE);
Row Compression • Row Compression compresses all columns in all rows in a table (or index). • Involves compressing each row individually. Row compression is preferred over page compression when the data to be compressed has a higher percentage of unique data as compared to repetitive data.
Page Compression • Page Compression also compresses all columns in all rows in a table however the method of compression spans multiple rows thus involving an entire page of data. • The purpose of page compression is to reduce the amount of redundant data stored in a page regardless of which row it is in. • Thus page compression is preferred over row compression when the data on a page to be compressed has a higher percentage of repetitive data as compared to unique data.
Sparse Columns • SQL Server 2008 includes a new storage space savings feature known as sparse columns. • Normally, even if a column often has NULL data, space must be allocated for the column. • Using a sparse column option for a fixed length column potentially alters the fixed space allocation.
Sparse Columns • When the majority of the rows in a table have null data for a particular column, then that column is a probable candidate for use as a sparse column. • Defining a column as sparse can actually increase the space used if the majority of the rows have data in the column. • Sparse columns also require some additional processing overhead so like most things, using sparse columns is a tradeoff and you should use your best judgment depending upon the data.
Sparse Columns • You require Enterprise, Developer, or Evaluation Edition of SQL Server 2008 to use sparse columns.
Partitioning Tables • Tables in SQL Server can range from small, having only a single record, to huge, with millions of records. • These large tables can be difficult for users to work with simply because of their size. • To make them smaller without losing any data, you can partition your tables. • Partitioning tables and views across multiple servers results in a federation of servers.
Partition Function • To partition this table, you first need to decide on a partition column and a range of values for the column. • In a table of order data, you probably have an order date column, which becomes an excellent candidate. • Now you can use the partition column and range to create a partition function, which SQL Server uses to allocate the data across the partitions.
Partition Scheme • After you figure out how to divide the table, you next need to decide where to keep the partitioned data physically. • You can keep archived data on one hard disk and current data on another disk by storing the partitions in separate filegroups, which can be assigned to different disks.
Summary • You learned you must sit down and plan before you create tables. • You need to decide what the tables should contain, making them as specific as possible. • Use Microsoft’s Visio or even pencil and paper. • You also learned that tables consist of entities (which contain a specific type of data) and rows (an entity in the table that spans all columns). • Each of the columns in the table has a specific data type that restricts the data it can hold.
Summary • You learned the mechanics of creating tables in the database using both Transact-SQL and Management Studio. • You learned the basics of data compression and implementing sparse columns. • Depending upon the data, either no compression, row compression, or page compression should be chosen. • You then learned you can spread the contents of a table over multiple resources, creating ultimately a federation of servers.
Summary for Certification Examination • Partitioning tables allows you to break a table into multiple pieces stored in separate files on multiple disks across multiple servers. • To partition a table, you need to select a column, create a partition function, and then create a partition scheme. • The partition function can be a LEFT or RIGHT range, so make sure you know how and why to choose.
Summary for Certification Examination • A table collects records and attributes (rows and columns) that SQL Server uses to store and organize data.