Database Fundamentals Flashcards
What is a database?
- organised collection of data, typically stored in electronic format
- allows you to input, organise and retrieve data quickly
What is a query?
-inquiry into the DB that returns information back
What is an index?
- data structure that improves the speed of the data retrieval operations on a DB table
- have disadvantage in that they need to be created and updated which requires processing resources and takes up disk space
What is a database server?
- DB’s often stored on there so they can be accessed by multiple users and provide a high level of performance
- DB servers do not house any form of application and instead are optimised to serve only the purpose of the DB itself using advanced hardware that can handle the high processing needs
- Usually racks
What is Microsoft SQL server?
A DB server. Has 3 types of files to store DB’s;
- primary data files have an .mdf extension and are the first files created in a DB. Contain user-defined objects, such as tables and views as well as system tables that SQL server requires for keeping track of the DB
- if DB becomes too large and you run out of room on first hard disk you can create secondary data files which have a .ndf extension, on seperate hard disks
- Transaction log file use a .ldf extension and don’t contain objects
What is a DBMS?
-collection of programs that enables you to enter, organise and select data in a database
What is a flat file DB?
- 2D tables consisting of rows and columns
- each column is a field and each row is a record
- access, performance and queries are quick on a flat file DB
- .txt file
What is a Hierarchical DB?
-tree structure, each parent table can have multiple children but the children can only have one parent
What is a relational DB?
- similar to hierarchical in that data is stored in tables and any new info is added into it without need for reorganisation
- can have multiple parents unlike hierarchical
- each column corresponds to an attribute and each row to a record
What are the fundamental characteristics of a DB?
- designed to store billions of rows of data
- limited to the computers available hard disk space
- optimised to use all the computers available memory to improve performance
What is a calculated value?
-value that results from the performance of some sort of calculation or formula on a specified input value
How are DB objects divided?
-into storage and programmability
What are constraints?
-limitations or rules placed on a field or column to ensure that data that is considered invalid is not entered
Some types of constraints available?
- unique = allows DB admin to ID which column should not contain duplicates
- check = allows admin to limit types of data user can insert into DB
- default = used to insert default value into a column if no other value specified
- not null = ensures data is entered into cell
- primary key = uniquely ID’s each record in a DB
- foreign key = in one table points to primary key in another table (can contain null values)
What is self reference?
-where a foreign key constraint references columns in the same table
What is SSMS?
- SQL Sever Management System
- primary tool to manage a server and its DB using a GUI
- can be used to view and optimise DB performance, as well as to create and modify DB’s, tables and indexes
- includes query analyser which provides GUI based interface to write and execute queries.
What are the 3 things SSMS query analyser supports?
- Xquery = query and functional programming language that is designed to query collection of XML data
- SQLCMD =command line app that comes with SQL server and exposes the management features of it. Allows SQL queries to be written and executed from the command prompt. Can also create SQL script files (.sql) to run a set of SQL statements as a script
- Transact-SQL = primary means of programming and managing SQL server. When you use SSMS to perform an action or task you are using this
What is data manipulation language (Dml)
- the language element that allows you to use the core statements SELECT, INSERT, UPDATE, DELETE and MERGE to manipulate data in any SQL server tables
- select = retrieves rows from DB and enables the selection of one or many rows or columns from one or many tables
- insert = adds one or more new rows to a table or a view
- update = change existing data in one or more columns in a table or view
- delete = removes rows from a table or view
- merge =performs insert, update, or delete operations on a target table based on the results of a join with a source table
What is special about DML statements?
- they either succeed or fail
- if you tried to insert 10,000 records into a table but violated one of the primary key the entire 10,000 rows of records would roll back and not one record would be inserted into the table
What is Data definition language?
- subset of the Transact SQL language
- deals with creating DB objects like tables, constraints and stored procedures
- The interface used to create these underlying DDL statements is the SSMS UI
What are the limitations of the SSMS when using DDL statements?
- allows you to visually design DDL statements and a DDL script statement task can be completed through its UI
- but not all the operations you may wish to use can be accomplished through this interface
- have to be familiar with the statements themselves
- most DDL statements can be executed through SSMS but using the statements themselves gives you more power, flexibility and control
What are some key DDL statements?
- USE = changes the DB context, when performing commands you will have to enter the USE command to select the DB first
- CREATE = creates a SQL server DB object
- ALTER = change an existing object
- DROP = removes an object from the DB, error will be raised if other objects are dependent on the object being removed
- TRUNCATE = removes rows from a table and frees the space used by those rows, better for larger DB’s
- DELETE = remove rows from a tables but does not free the space used by those rows removed, better for smaller DB’s
What are system tables?
- keep track of DB
- when you want to query system views to verify where the object(s) you wish to drop are in the DB tables, you need to know what tables are the most useful
- system views belong to the sys schema
- some of those tables include; sys.Tables, sys.Columns, sys.Database, sys.Constraints, sys.Views and sys.Objects
What is a data type?
An attribute that specifies the type of data an object can hold, as well as how many bytes each type take sup
What categories do data types fall into in SQL server 2008?
- exact numbers
- approx numbers
- date and time
- character strings
- Unicode character strings
- binary strings
- other
- CLR
- spatial
Examples of some data types?
- Money = used for money, if you want % best use float. Exact number with fixed precision
- Datetime = used to store date and time data in different formats. Has datetime which is good for dates 1/01/1753-31/12/9999 accurate to 3.33ms and datetime2 which is good for 1/01/1900-06/06/2079 accurate to 1 min. Use 8 and 4 bytes respectively
- integer = used to store math computations. Good when no decimal
- varchar = variable character string good when supporting english attrib. If using foreign use nvarchar
- boolean = columns stored in 8 bits per byte. Converts true and false string values into 1 and 0
- float = approx data type
What does each column, local variable, expression and parameter always have?
Data type and each of the data types is an attribute
What 3 things can happen when two expressions have different data types, collation, precision, scale or length?
- when two expressions have different types, rules for data-type precedence specify that the data type with the lower-precedence is converted to the data type with the higher precedence
- collation refers to a set of rules that determine how data is sorted and compared. SQL server has predefined collation precedence, if you wish to override how data is sorted you must use collation clause
- precision scale and length of the result depend on the precision, scale and length of those values you are performing the operations on
What are exact numeric data types?
Examples?
- most common SQL server DT, used to store numeric info
- some allow only whole numbers, others allow decimals
- int
- bigint
- bit = does 2 things; 1) optimises storage of bit columns meaning <=8 bits turn into a byte, 9-16 into 2 bytes etc 2) TRUE and FALSE string values converted to 1 and 0 respectively
- decimal and numerical have fixed precision (max number of digits that can be stored left and right of decimal point) and scale (max number of digits that can be stored to right of decimal point). expressed as decimal/numerical[(p[ ,s])]
What are approx numeric DT’s?
- not used that often
- used when need extra precision
- float and real give extra precision at cost of increased storage
- syntax is float(n) with n being the number of bits used to store the mantissa. Default value of n is 53 and range is 1-53
Examples of Date and time DT’s?
date = range 01/01/01-31/12/9999 AD
-stores info in string literal format = YYYY/MM/DD
-Takes 3 bytes of data with 1 day of accuracy
-default 1900-01-01
DateTimeOffset = similar to DateTime in using 24 hour clock but also keeps track of time zones
SmallDateTime = Same as DateTime but does not include seconds
Time = defines time based on 24 hour clock and without timezone awareness
What is an implicit conversion?
- automatic conversion of a data type
- eg; multiply an items cost (float) with number of items (integer) the result will be expressed as a float
- some conversions are not allowed; cant convert DateTime to a float, despite it being represented by a float, as its meant to be a Date and/or time
How do you force a conversion?
Cast = in compliance with ANSI standards, which allow you to import or export to other DBMS’s
-cast(source-value AS destination-type), cast(count AS float)
Convert = specific to T-SQL but a little more powerful
-CONVERT( data_type [(length)], expression [,style])
-you can specifics how many digits or characters the value will be
-CONVERT(nvarchar(10), OrderDate, 101)
What is a character string?
- regular char has 1 byte per character meaning you can define 256 characters
- Unicode character uses two bytes per character so you can define 65,536 characters; allows you store any language
- regular character literal is always expressed with single quotes. Unicode character literal needs the letter N prefixing the quotes
What is the VAR element?
- SQL server will preserve space in the row in which that element resides based on the columns defined size and not the actual number of characters in the character string itself, plus an extra two bytes for offset data
- eg; if you want to specify a string that supports a max of only 25 character you would use VARCHAR(25)
- any data type without VAR element within its name is fixed length
What are the pros and cons when using Unicode DT’s?
- storage consumption is reduced allowing for faster read operations
- possibility of row expansion, leading to data movement outside the current page
- Means any update on data using variable length DT’s may be less efficient than updates using fixed-length
- possible to use MAX specifier to define variable length DT’s instead of using max number of characters ID in string
- eg; when column defined with MAX, a value with the size ID up to a certain threshold is stored inline in the row. A value with a size greater than the threshold is stored externally to the row and ID as a large object (LOB)
Difference between char and varchar?
- char is ID as char[(n)] and is a fixed length, regular character with length of n bytes. N must be between 1-8000
- varchar[(n|max)] is variable length that consist of 1-8000
- both have Unicode variants nchar and nvarchar
- use nchar when sizes of the column data entries will be similar and nvarchar when they will vary considerably
- SQL server supports two character string types; regular and unicode (char as opposed to nchar)
What is an SQL DB?
- central container that retrieves data from many different tables and views
- can run queries on these data, thereby interacting with the info stored in the DB to obtain the output you require
- to make DB’s easier to manage your organisation should establish and use a single, consistent standard
How do you create tables using ANSI SQL syntax?
CREATE TABLE planets (name varchar(5), diameter varchar(50))
INSERT INTO planets (name, diameter) VALUES (‘earth’, 10000)
What is a view?
- virtual table consisting of different columns from one or more tables
- unlike a table, stored in a DB as a query object; therfore it is an object that obtains its data from one or more tables (underlying tables)
- meant to be a security mechanism as it ensures that users can retrieve and modify only the data seen by them through their permissions, thus ensuring they cannot see or access remaining data in the underlying tables
How do views ensure security?
Restrict access to;
- Specific rows and/or columns
- rows obtained by using joins
- stat summaries of data in given tables
- subsets of another view or subsets of views and tables
Examples of views?
- subset of rows or columns of a base table
- union of two or more tables
- join of two or more tables
- stat summary of bias tables
Why create a view?
- views allow you to limit the type of data users can access
- reduce complexity for end users so they dont have to write complex SQL queries. Instead you can write them on their behalf and hide them in a view
- Can create one using SSMS, or by writing a Transact-SQL statement
What is a stored procedure?
-previously written SQL statement that has been stored or saved into a DB
-saves time and memory If you have to run the same query over and over again
-eg; “exec usp_displayallusers” exec tells SQL server to execute code in the procedure and usp indicates to SQL that it is a user-created stored procedure
-
What is SQL injection?
- an attack in which malicious code is inserted intro strings that are later passed on to instances of SQL server waiting for parsing and execution
- Any procedure that constructs SQL statements should be reviewed continually for injection vulnerabilities because SQL server will execute all syntactically valid queries from any source
What is the select command?
What is SET NOCOUNT?
- command used to retrieve data from a database
- select query includes up to 3 parts; columns to retrieve (commas separating each column not spaces), tables to retrieve these columns from, conditions the data must satisfy (if any)
- only parts that are needed is SELECT FROM
- ID selects all columns from table
-by default it is set to off but when on it tells the number of rows a query affected
What is the purpose of the AND, OR conjunctions?
- allow multiple conditions to be chosen
- WHERE = ‘attribute1’ OR(/AND) ‘attribute2’
What is the BETWEEN clause?
- can be used to find a range of values
- much nicer than having; WHERE >= ‘X’ AND <= ‘Y’ instead its; WHERE BETWEEN X AND Y