SQL Oracle Query Structure and Overview

Embed Size (px)

Citation preview

  • 8/2/2019 SQL Oracle Query Structure and Overview

    1/9

    Visit dotnetfuncorner.blogspot.com for more

    Page1

    SQL SERVER Questions And Answers

    1. What is an Entity?

    Ans: The basic data item stored in database is called entity. An entity can be any object, item,place, person, concept, or activity about which data is stored.

    2. What is an attribute?

    Ans: An attribute is a property of an entity. It describes a part of an entity. Entity could haveone or more attributes.

    3. What is ER diagram?

    Ans: An Entity Relationship Diagram is diagrammatic representation of the logical structure ofa database system.

    4. Describe the concept of keys.

    Ans:Candidate key

    An attribute that uniquely identifies a row is called candidate key. It is also called dassurrogate key.

    Primary keyA candidate key that you choose to identify rows uniquely is called a primary key.

    Alternate keyIf there are multiple candidate keys in a table, the candidate keys that are chosen asprimary key are called the alternate keys.

    Composite keyWhen the key that uniquely identifies the rows of a table is made up of more than oneattribute, it is called as a composite key.

    Foreign keyTwo tables can be related using a common attribute. When a primary key of one table isalso available as an attribute in another related table it is called a foreign key.

    5. What are joins?

    Ans: Sometimes, data from multiple tables is to be displayed using select statement. For thispurpose, the tables in use must have a column that is equated. This is termed as simplejoin or multiple join.

    Sometimes, you might want to display all records from one table and some from another.This type of join is called an outer join. An outer join is only possible between two tables.

  • 8/2/2019 SQL Oracle Query Structure and Overview

    2/9

    Visit dotnetfuncorner.blogspot.com for more

    Page2

    There are two types of outer join, namely left and right. In a left outer join, all the rows ofthe first table named in the FROM clause are displayed. In a right outer join all the rowsfrom the second table mentioned in the FROM clause are displayed. In either case, all thematching rows from other table are displayed.

    6. What is a sub query?

    Ans: Sometimes the results of one query are dependant on the results of another query. Forthis purpose one query is nested inside another query, this is called as sub query.

    7. What are the types of constraints?

    Ans: You can enforce data integrity by using constraints. Constraints are divided in to fivecategories.

    Primary key constraint

    A primary key constraint is defined on a column are a set of columns whose valuesuniquely identify the rows in a table. It cannot contain null values.

    Unique constraintUnique constraints are used to enforce uniqueness on non-primary key columns. It allowsnull values but only one row can have a null value.Multiple unique constraints can be created on a table.

    Foreign Key constraint

    You can use the foreign key constraint to remove the inconsistency in two tables whenthe data in the one table is dependant on the other table.

    Check constraintIt enforces domain integrity by restricting the values to be inserted in a column. It ispossible to define multiple check constraints on a single column. These are evaluated inthe order in which they are defined.

    Default ConstraintA default constraint can be use to assign a constant value to a column and the user neednot insert values in to that column.

    8. What is a rule?

    Ans: The required integrity can be enforced by specifying a check constraint or by defining arule. But check constraint modifies the table structure. The constraint can there fore beimplemented using rules with out changing the table structure. This rule is applied beforean insert or update statement .

    A rule must be bound to a column or a user-define d data type. This is done using stored

    procedure sp_bindrule. Rules do not apply to data that has already been inserted in thetable. The existing values in tables do not have to meet the criteria specified by the rule.

  • 8/2/2019 SQL Oracle Query Structure and Overview

    3/9

  • 8/2/2019 SQL Oracle Query Structure and Overview

    4/9

    Visit dotnetfuncorner.blogspot.com for more

    Page4

    Advantages

    Improved performance Applications do not have to compile the procedure over andover again.

    Reduction in network congestion applications need not submit multiple SQL

    statements to server for the purpose of processing.

    Enhanced accuracy SQL statements included in a procedure are designed byexperienced programmers and are therefore more efficient, error free, and tested.

    Better security mechanism users can be granted permission to execute a storedprocedure even if they do not own it.

    Types of stored procedures

    1. User-defined2. System defined

    These are prefixed with sp_. These are for administrative purpose and are stored in thedatabase and are accessible to all users.

    3. Temporary

    These are prefixed with #, stored in tempdb and are automatically dropped whenconnection terminates.

    4. Remote

    These are created and stored in remote servers and can accessed by users withappropriate permissions.

    5. Extended

    These are dlls that are executed outside SQL Server. They are prefixed by xp_.

    12. Explain about BCP and DTS.

    BCP

    The transfer data from an external source to SQL Server is performed using Bulk CopyProgram utility. The external source is a flat file. Data transfer from external source toSQL Server in BCP IN. The transfer of data from SQL Server to external source is BCPOUT

    .

  • 8/2/2019 SQL Oracle Query Structure and Overview

    5/9

    Visit dotnetfuncorner.blogspot.com for more

    Page5

    DTS

    Data transformation services can be used to import and export data betweenheterogeneous data sources and SQL Server. The external data sources include VisualFoxPro, MS Excel, Paradox, MS Access, Dbase, and text files.

    13. What are transactions and their properties?

    A transaction is a sequence of operations performed together as a single unit of logicalwork. It has four properties.

    Atomicity it states that either all the data modifications are performed or none areperformed.

    Consistency - it states that all the data is in a consistent state after a successful completionof transaction.

    Isolation it states any data modification made my concurrent transactions must beisolated from the modifications made by other concurrent transaction.

    Durability it states that ay change made by a completed transaction remainspermanently in the system.

    14. Locking mechanism.

    SQL Server uses the concept of locking to ensure transactional integrity and databaseconsistency. Locking, by functionally prevents users from accessing information beingchanged by other users. In a multi-user environment, Locking prevents users fromchanging the same data at the same time. In SQL Server locking is implementedautomatically.

    SQL Server implements multi-granular locking, which allows transactions to lockdifferent types of resources at different levels. SQL Server can lock the following

    resources.

    RID is a row identifier that individually locks a row in a table.Key is a rowlock with in an index.Page is an 8k page or an index page.Extent is a contiguous group of 8k data pages or index pages.

    Table is the entire table, including all data and indexes.Database is the complete database.

  • 8/2/2019 SQL Oracle Query Structure and Overview

    6/9

    Visit dotnetfuncorner.blogspot.com for more

    Page6

    Shared Locks

    It is used for operations that do not change or update the data. This allows concurrenttransactions to read a resource and no other transaction can modify the data on thatresource.

    Update Locks

    This lock is implemented when a transaction modify a row. Only one update lock isallowed on a resource at a time.

    Exclusive locks

    Intent locks

    Schema Locks

    15. What is deadlock?

    A dead lock is a situation in which two users (or transactions) have locks on separateobjects, and each user is waiting for a lock on the others object. It usually occurs in amulti-user environment.

    16. What are triggers?

    A trigger is a block of code that constitutes with a set of T-SQL statements that areactivated in response to certain actions. A trigger can also be interpreted as a special kindof stored procedure that is executed whenever an action, such as data modification, takesplace.

    A trigger is always defined on a table, and is said to have fired whenever the data in theunderlying table is affected by any of the Data Manipulation Language (DML)statements-INSERT, UPDATE, or DELETE. A trigger fires in response to an event likeinsertion, updation, and deletion of data.

    Triggers help in maintaining consistent, reliable, and correct data in tables. They enablethe performance of complex actions and cascade these actions to other dependant tables.Characteristics of a trigger:

    It can be associated with tables.

    It cannot be defined on temporary tables or views. However, it can reference temporarytables and views.

    Whenever any data modification statement is issued then SQL Server fires itautomatically.

    It cannot be explicitly invoked or executed, as in the case of stored procedures.

    Triggers can be nested up to 16 levels. The nesting of triggers occurs when a triggerperforms an action that initiates another trigger.

    It prevents incorrect, unauthorized, and inconsistent changes in data.

  • 8/2/2019 SQL Oracle Query Structure and Overview

    7/9

    Visit dotnetfuncorner.blogspot.com for more

    Page7

    It cannot return data to the user.

    There are three types of triggers1) INSERT2) DELETE3) UPDATE

    17. What is Normalization? Explain three normal forms?

    Normalization is a scientific method of breaking down complex table structures in tosimple table structures by using certain rules. Hence reduce redundancy in a table andeliminate inconsistency problems and disk space usage.

    First Normal Form

    A table is said to be in 1NF when each cell of the table contains precisely one value.

    Functional dependency

    If you have two attribute A and B, A is said to be functionally dependant on B, if for eachvalue of B, there is exactly one value of A.

    Second Normal Form

    Identify the functionally dependent keys and place them in a different table.

    Third Normal Form

    A relation is said to be 3NF when every non-key attribute is functionally dependant onlyon the primary key.

    18. What are Cursors and types of cursors?

    A cursor is a work area called private SQL area, which executes SQL statements andstores the results.

    Cursor Types

    So you can specify the four-cursor types for Transact-SQL cursors. These cursors vary intheir ability to detect changes to the result set and in the resources, such as memory andspace in tempdb, they consume. The four API server cursor types supported by SQLServer are:

    Static cursors Dynamic cursors

    Forward-only cursors Key set-driven cursors

  • 8/2/2019 SQL Oracle Query Structure and Overview

    8/9

    Visit dotnetfuncorner.blogspot.com for more

    Page8

    Static cursors detect few or no changes but consume relatively few resources whilescrolling, although they store the entire cursor in tempdb.

    Dynamic cursors detect all changes but consume more resources while scrolling, althoughthey make the lightest use of tempdb.

    Key set-driven cursors lie in between, detecting most changes but at less expense thandynamic cursors.

    Although the database API cursor models consider a forward-only cursor to be a distincttype of cursor, SQL Server does not. SQL Server considers both Forward only and scrollto be options that can be applied to static, key set-driven, and dynamic cursors.

    19. What is Encryption option in SQL Server?

    Encryption is a method for keeping sensitive information confidential by changing data

    into an unreadable form. Encryption ensures that data remains secure by keeping theinformation hidden from everyone, even if the encrypted data is viewed directly.Decryption is the process of changing encrypted data back into its original form and soauthorized users can view it.

    20. What are time-stamped data types?

    It is a database-wide unique number. The storage size is 8 bytes. A table can have onlyone timestamp column. The value in the timestamp column is updated every time a rowcontaining a timestamp column is inserted or updated. This property makes a timestampcolumn a poor candidate for keys, especially primary keys. Any update made to the rowchanges the timestamp value, thereby changing the key value. If the column is in aprimary key, the old key value is no longer valid, and foreign keys referencing the old

    value are no longer valid. If the table is referenced in a dynamic cursor, all updates changethe position of the rows in the cursor. If the column is in an index key, all updates to thedata row also generate updates of the index.

    21. Correlated queries.In correlated queries a sub query is executed for each row the parent query is executed.

    Ex: Select e. * from EMP e where e.sal >(select Avg (Sal) from emp where e.deptno =

    emp.deptno

    22. What is OLAP and how it works with SQL Server?

    OLAP Services is a new middle-tier server for online analytical processing (OLAP).OLAP Services provides wizards, editors, and information to make OLAP technologyeasier to use. OLAP Services supports various data and storage models to help you createand maintain an OLAP system that meets your organizations needs.

  • 8/2/2019 SQL Oracle Query Structure and Overview

    9/9

    Visit dotnetfuncorner.blogspot.com for more

    Page9

    25. What are the advantages of SQL Server 7.0 over SQL Server 6.5?

    Trigger Enhancements

    Recursive triggers Multiple triggers per INSERT, UPDATE, or DELETE statement

    Data Transformation Services

    Web Assistant Wizard

    The Web Assistant Wizard has been enhanced in SQL Server 7.0. In addition to exportingSQL Server data out to an HTML file, it can also import tabular data from an HTML fileinto SQL Server, and post to and read from HTTP and FTP locations.

    Row-level locking

    SQL Server 7.0 supports complete row-level locking on both data pages and index pages.

    SQL Server 7.0 supports applications that span a broad range of platforms.

    Gigabytes of memory, and a terabyte or more of disk storage.

    Stored Procedures

    The stored procedure model has been enhanced in SQL Server 7.0 to provide improvedperformance and increased application flexibility. When a stored procedure is compiledand placed in the procedure cache, all users of the stored procedure share that one copy

    of the compiled plan.

    The most notable feature is update replication. Using update replication, data replicatedby SQL Server 7.0 can be modified at multiple sites.