Wednesday, January 27, 2010

STORE PROCEDURE


Share/Save/Bookmark
A stored procedure is subroutine available to applications accessing a relational  database system Stored procedures (sometimes called a proc, sproc, StoPro, or SP) are actually stored in the database data dictionary..
Typical uses for stored procedures include data validation (integrated into the database) or acces control mechanisms. Furthermore, stored procedures are used to consolidate and centralize logic that was originally implemented in applications. Large or complex processing that might require the execution of several SQL statements is moved into stored procedures, and all applications call the procedures only.
Stored procedures are similar to UDFs. The major difference is that UDFs can be used like any other expression within SQL statements, whereas stored procedures must be invoked using the CALL statement
CALL procedure(…)
or
EXECUTE procedure(…)
Stored procedures can return result set, i.e. the results of a SELECT statement. Such result sets can be processed using cursor by other stored procedures by associating a result set locator, or by applications. Stored procedures may also contain declared variables for processing data and cursors that allow it to loop through multiple rows in a table. The standard Structured Query Language provides IF, WHILE, LOOP, REPEAT, CASE statements, and more. Stored procedures can receive variables, return results or modify variables and return them, depending on how and where the variable is declared.
The exact and correct implementation of stored procedure varies from one database system to another. Most major database vendors support them in some form. Depending on the database system, stored procedures can be implemented in a variety of programming language, for example SQL, Java, C, or C++. Stored procedures written in non-SQL programming languages may or may not execute SQL statements themselves.
The increasing adoption of stored procedures led to the introduction of procedural elements to the SQL language in the SQL:1999 and SQL:2003 standards in the part SQL/PSM. That made SQL an imperative programming language. Most database systems offer proprietary and vendor-specific extensions, exceeding SQL/PSM.

 
Database System
Implementation Language
Microsoft SQL Server
Transact-SQL and various .NET Framework languages
Oracle
PL/SQL or Java
DB2
SQL/PL
Informix
SPL
PostgreSQL
PL/pgSQL, can also use own function languages such as pl/perl or pl/php
Firebird
PSQL (Fyracle also supports portions of Oracle's PL/SQL)
MySQL
own stored procedures, closely adhering to SQL:2003 standard.

 
from : wikipedia 

VIEW


Share/Save/Bookmark
In database theory, a view consists of a stored query accessible as a virtual table composed of the result set of a query. Unlike ordinary tables (base tables) in a relational database, a view does not form part of the physical schema: it is a dynamic, virtual table computed or collated from data in the database. Changing the data in a table alters the data shown in subsequent invocations of the view.
Views can provide advantages over tables:
  • Views can represent a subset of the data contained in a table
  • Views can join and simplify multiple tables into a single virtual table
  • Views can act as aggregated tables, where the database engine aggregates data (sum, average etc) and presents the calculated results as part of the data
  • Views can hide the complexity of data; for example a view could appear as Sales2000 or Sales2001, transparently partitioning the actual underlying table
  • Views take very little space to store; the database contains only the definition of a view, not a copy of all the data it presents
  • Depending on the SQL engine used, views can provide extra security
  • Views can limit the degree of exposure of a table or tables to the outer world
Just as functions (in programming) can provide abstraction, so database users can create abstraction by using views. In another parallel with functions, database users can manipulate nested views, thus one view can aggregate data from other views. Without the use of views the normalization of databases above  secind normal form would become much more difficult. Views can make it easier to create lossless join decomposition.
Just as rows in a base table lack any defined ordering, rows available through a view do not appear with any default sorting. A view is a relational table, and the relational model defines a table as a set of rows. Since sets are not ordered - by definition - the rows in a view are not ordered, either. Therefore, an order byclause in the view definition is meaningless. The SQL standard does not allow an ORDER BY clause in a subselect in a CREATE VIEW statement, just as it is not allowed in a CREATE TABLE statement. However, sorted data can be obtained from a view, in the same way as any other table - as part of a query statement. In Oracle 10g, a view can be created with an ORDER BY clause in a subquery.

TABLE


Share/Save/Bookmark
In relational database and flat files database,  a table is a set of data elements (values) that is organized using a model of vertical columns (which are identified by their name) and horizontal rows. A table has a specified number of columns, but can have any number of rows. Each row is identified by the values appearing in a particular column subset which has been identified as a candidate key. Table is another term for relations; although there is the difference in that a table is usually a multi-set (bag) of rows whereas a relation is a set and does not allow duplicates. Besides the actual data rows, tables generally have associated with them somemeta information, such as constraints on the table or on the values within particular columns.
The data in a table does not have to be physically stored in the database. Views are also relational tables, but their data are calculated at query time. Another example are nicknames, which represent a pointer to a table in another database.

from: wikipedia.com

TRIGGER


Share/Save/Bookmark
A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database. The trigger is mostly used for keeping the integrity of the information on the database. For example, when a new record (representing a new worker) added to the employees table, new records should be created also in the tables of the taxes, vacations, and salaries.

DML Triggers

There are typically three triggering events that cause data triggers to 'fire':
  • INSERT event (as a new record is being inserted into the database).
  • UPDATE event (as a record is being changed).
  • DELETE event (as a record is being deleted).
Structurally, triggers are either "row triggers" or "statement triggers". Row triggers define an action for every row of a table, while statement triggers occur only once per INSERT, UPDATE, or DELETE statement. DML triggers cannot be used to audit data retrieval via SELECT statements, because SELECT is not a triggering event.
Furthermore, there are "BEFORE triggers" and "AFTER triggers" which run in addition to any changes already being made to the database, and "INSTEAD OF trigger" which fully replace the database's normal activity.
Triggers do not accept parameters, but they do receive information in the form of implicit variables. For row-level triggers, these are generally OLD and NEW variables, each of which have fields corresponding to the columns of the affected table or view; for statement-level triggers, something like SQL Server's Inserted and Deleted tables may be provided so the trigger can see all the changes being made.
For data triggers, the general order of operations will be as follows:
  1. a statement requests changes on a row: OLD represents the row as it was before the change (or is all-null for inserted rows), NEW represents the row after the changes (or is all-null for deleted rows)
  2. each statement-level BEFORE trigger is fired
  3. each row-level BEFORE trigger fires, and can modify NEW (but not OLD); each trigger can see NEW as modified by its predecessor, they are chained together
  4. if an INSTEAD OF trigger is defined, it is run using OLD and NEW as available at this point
  5. if no INSTEAD OF trigger is defined, the database modifies the row according to its normal logic; for updateable view this may involve modifying one or more other tables to achieve the desired effect; if a view is not updatable, and no INSTEAD OF trigger is provided, an error is raised
  6. each row-level AFTER trigger fires, and is given NEW and OLD, but its changes to NEW are either disallowed or disregarded
  7. each statement-level AFTER trigger is fired
  8. implied triggers are fired, such as refrencial action in support of foreign key constraints: on-update or on-delete CASCADE, SET NULL, and SET DEFAULT rules
In ACID databases, an exception raised in a trigger will cause the entire stack of operations to be rolled back, including the original statement.

Sunday, January 17, 2010

How do manage / connect to SQL Server Express


Share/Save/Bookmark
Updated: 6/20/2008 12:25:00 AM
Requests: 150196
a.k.a. "Where is Enterprise Manager?"
a.k.a. "When will Express Manager be released?"
Microsoft has released a trmmed-down version of Management Studio, called Management Studio Express. You can download it from Microsoft Downloads. If you're brave, feel free to download the recent CTP of the tool here, I just can't really tell you yet whether anything worthwhile has been added or, more importantly, if anything useful has become broken.
If you are affected by the limitations of this free tool, you can manage SQL Server Express with a host of other tools:
  • OSQL 
  • SQLCMD 
  • any Express product 
  • Yohz Software's 
  • Lloyd Sheen's 
  • Simego's
Allegedly, you can also manage Express Edition with web data Administrator, but only if you enable mixed authentication (I haven't tried this, and note also that it is not officially supported).
There is no longer any such thing as Enterprise Manager or Query Analyzer, so stop looking for them, and don't try to use existing installations to connect to SQL Server 2005 instances—it simply will not work, even if it seems to at first glance.
As for connecting to Express from application code, this should not be any different from connecting to a named instance of SQL Server 2000. Your connection string should look like this, assuming local machine and an instance name of SQLEXPRESS (you might need a different instance name, and you may have to use a machine name, rather than "." (which means local)).
ConnectionString = "Provider = SQLNCLI;" & _
    "Data Source = .\SQLEXPRESS;" & _
    "Initial Catalog = ;" & _
    "User ID = ;" & _
    "Password = ;"

However, you might come across various errors, depending on your configuration and the tool you are using:
Sqlcmd: Error : Microsoft OLE DB Provider for SQL Server :
Client unable to establish connection.
 
Shared Memory Provider: The specified module could not be found.
 
Shared Memory Provider: The parameter is incorrect.
(Microsoft SQL Server, Error: 87)
 
Named pipes provider: The system cannot find the file specified
 
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
 
Shared Memory Provider: The system cannot find the file specified.
So in addition to the connection string above, some other steps you might have to perform:
  • Make sure that SQL Server Express is functioning properly:
     
    • Start / Run... / type "CMD" without the quotes and hit OK
    • Type "SQLCMD -S.\SQLEXPRESS" without the quotes and hit Enter
    • Verify that you<

    from : http://www.aspfaq.com/

Share/Save/Bookmark
How to use SQL Server Express (where's the UI?) - level 200
If you are used to working with SQL Server 2000, you may expect to find a tool similar to Enterprise Manager and Query Analyzer.  SQL Express is a free product, and it shows, but you get an awful lot for free. . . but you don't get it all.  I think that's perfectly fine.  I'd rather have something for free than nothing.
Here's a few tips to get you started:
  • SQL Express installs as a local instance named "SQLEXPRESS", so your connection string needs to include the instance name:  ".\SQLEXPRESS" or "localhost\SQLEXPRESS".
  • SQL Server Configuration Manager isn't the UI you want to add databases, tables, etc.
  • Use any of the Visual Studio Express products as your database UI.  They all have the database manager built in.
    • Use the Server Explorer window to add your database instance.  Then you can use it to add a new database and add tables to that database. 
    • If you have database create scripts, you can run them inside Visual Studio Express.  If you are used to hitting F5 in Query Analyzer, then you'll want to map a shortcut key to the "Run Selection" command: Right click -> Run Selection.
    • You can create all your database objects here.
    • You can run and step through stored procedures for debugging.

  • You can also use osql.exe to manager your database.  This is useful when you want to automate database scripts using NAnt.
  • You have the option of how you want to connect to a SQL Express database:
    • Through the SqlClient provider: Data Source=localhost\sqlexpress;Initial Catalog=MyNewTestDatabase;Integrated Security=True;Pooling=False
    • Through a file reference: Data Source=.\SQLEXPRESS;AttachDbFilename=C:\opensvn\development\ezweb\trunk\src\website\App_Data\ASPNETDB.MDF;Integrated Security=True;User Instance=True
    • If using ASP.NET, you already have a connection string you can use: LocalSqlServer:  Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|ASPNETDB.MDF;Integrated Security=True;User Instance=True 


    from : http://codebetter.com

Thursday, January 14, 2010


Share/Save/Bookmark
MYSQL
MySQL is a (RDBMS) that has more than 11 million installations. MySQL is named after Monty Widenius's daughter My. The program runs as a server providing multi-user access to a number of databases. MySQL is officially pronounced  (My S-Q-L), but often pronounced  (My SeQueL)
The project has made its source code available under the terms of the GNU General Public License, as well as under a variety of propietary agreements. MySQL is owned and sponsored by a single for profit firm, the Swedish company MySQL AB, now a subsidiary of sun microsystem] As of 2009 Oracle Corpoation began the process of acquiring Sun Microsystems.
In response to the Oracle acquisition, members of the MySQL community have created several forks such as Drizzle and Maria DB in fear that Oracle would hurt the development of MySQL because it is a competitor to the company's own database products.
MySQL is often used in free software projects that require a full-featured database management system, including projects such as WordPress, phpBB and other software built on the LAMP software stack. It is also used in many high-profile, large-scale www products

from http://en.wikipedia.org/wiki/MySQL