Showing posts with label DB. Show all posts
Showing posts with label DB. Show all posts

Wednesday, January 22, 2014

SQL Azure Bacpac - Local Restore

If you need to export a SQL Azure database and then import it into a local SQL 2008 R2 server (Note I am also using Visual Studio 2010). Microsoft certainly went out of their way to make this a painful task, however, I was able to do it by doing the following:

Goto this link http://msdn.microsoft.com/en-us/jj650014 and install the SQL Server Data Tools for Visual Studio 2010

This will install on your local drive. In my case here is where it put it: C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin

Browse to this via the command line or powershell

You are going to want to execute the SqlPackage.exe

Open up this link to see a list of all the parameter options for SqlPackage.exe (http://msdn.microsoft.com/en-us/library/hh550080(v=vs.103).aspx)

Here is my command line that I needed to execute to import a .bacpac file into my local SQL 2008 R2 server:

.\SqlPackage.exe /a:Import /sf:C:\mydatabasefile.bacpac /tdn:NorthWind /tsn:BINGBONG

/tdn is the name of the database you want your bacpac file to restore to. /tsn is the name of your SQL server.

You can see all these parameter descriptions on the link from #5.

Saturday, April 21, 2012

Android - Access SQLite Database File in Emulator


You have to be on the DDMS perspective on your Eclipse IDE
(Window > Open Perspective > Other > DDMS)
and the most important:
YOUR APPLICATION MUST BE RUNNING SO YOU CAN SEE THE HIERARCHY OF FOLDERS AND FILES.
Then in the File Explorer Tab you will follow the path :
data > data > your-package-name > databases > your-database-file.
Then select the file, click on the disket icon in the right corner of the screen to download the .db file. If you want to upload a database file to the emulator you can click on the phone icon(beside disket icon) and choose the file to upload.
If you want to see the content of the .db file, I advise you to use SQLite Database Browser, which you can download here.
PS: If you want to see the database from your real device, you must root your phone.

Monday, April 11, 2011

Stored Procedure vs. Function


Functions

Stored Procedure

Functions must return a value (scalar, inline table or multi statement table).
stored Procedure may or may not return a value
Functions can return a table
Stored Procedure can create a table but can't return table
Functions are called using select statements.
Stored Procedure can be called independently using exec keyword
Function can't be used to change server configuration(in terms of security-i.e. setting granular permissions of user rights) 
Stored Procedure can be used to change server configuration(in terms of security-i.e. setting granular permissions of user rights) 
XML and output parameters can't be passed to functions
XML and output parameters can be passed to stored Procedure
Transaction related statement can’t be handled in function.
Transaction related statement can be handled in stored procedure.


·        Stored Procedures can call a function or another stored Procedure similarly a function can call another function and a stored Procedure. The catch with function is that no user defined stored Procedure can be called. Only extended/system defined Procedures can be called

Database Normalization


Normalization

(Description with Examples)

Main Table:

Suppliers and Parts By Company Division
Company
Company Founder
Company
Logo
Division
Part Type
Supplier
Supplier
Country
Supplier
Continent
Allied Clock and Watch
Horace Washington
Sundial
Clocks
Spring
Pendulum
Spring
Toothed Wheel
Tensile Globodynamics
Tensile Globodynamics
Pieza de Acero
Pieza de Acero
USA
USA
Mexico
Mexico
N. Amer.
N. Amer.
N. Amer.
N. Amer.
Allied Clock and Watch
Horace Washington
Sundial
Watches
Quartz Crystal
Tuning Fork
Battery
Microflux
Microflux
Dakota Electrics
Belgium
Belgium
USA
Europe
Europe
N. Amer.
Global Robot
Nils Neumann
Gearbox
Industrial Robots
Flywheel
Axle
Axle
Mechanical Arm
Wheels 4 Less
Wheels 4 Less
TransEuropa
TransEuropa
USA
USA
Italy
Italy
N. Amer.
N. Amer.
Europe
Europe
Global Robot
Nils Neumann
Gearbox
Domestic Robots
Artificial Brain
Artificial Brain
Metal Housing
Backplate
Prometheus Labs
Frankenstein Labs
Pieza de Acero
Pieza de Acero
Luxembourg
Germany
Mexico
Mexico
Europe
Europe
N. Amer.
N. Amer.

First normal form (1NF)

·         A table must be guaranteed not to have any duplicate records; therefore it must have at least one candidate key.
·         There must be no repeating groups, i.e. no attributes which occur a different number of times on different records

Example:

Suppliers and Parts By Company Division
Company
Company Founder
Company
Logo
Division
Part Type
Supplier
Supplier
Country
Supplier
Continent
Allied Clock and Watch
Horace Washington
Sundial
Clocks
Spring
Tensile Globodynamics
USA
N. Amer.
Allied Clock and Watch
Horace Washington
Sundial
Clocks
Pendulum
Tensile Globodynamics
USA
N. Amer.
Allied Clock and Watch
Horace Washington
Sundial
Clocks
Spring
Pieza de Acero
Mexico
N. Amer.
Allied Clock and Watch
Horace Washington
Sundial
Clocks
Toothed Wheel
Pieza de Acero
Mexico
N. Amer.
Allied Clock and Watch
Horace Washington
Sundial
Watches
Quartz Crystal
Microflux
Belgium
Europe
Allied Clock and Watch
Horace Washington
Sundial
Watches
Tuning Fork
Microflux
Belgium
Europe
Allied Clock and Watch
Horace Washington
Sundial
Watches
Battery
Dakota Electrics
USA
N. Amer.
Global Robot
Nils Neumann
Gearbox
Industrial Robots
Flywheel
Wheels 4 Less
USA
N. Amer.
Global Robot
Nils Neumann
Gearbox
Industrial Robots
Axle
Wheels 4 Less
USA
N. Amer.
Global Robot
Nils Neumann
Gearbox
Industrial Robots
Axle
TransEuropa
Italy
Europe
Global Robot
Nils Neumann
Gearbox
Industrial Robots
Mechanical Arm
TransEuropa
Italy
Europe
Global Robot
Nils Neumann
Gearbox
Domestic Robots
Artificial Brain
Prometheus Labs
Luxembourg
Europe
Global Robot
Nils Neumann
Gearbox
Domestic Robots
Artificial Brain
Frankenstein Labs
Germany
Europe
Global Robot
Nils Neumann
Gearbox
Domestic Robots
Metal Housing
Pieza de Acero
Mexico
N. Amer.
Global Robot
Nils Neumann
Gearbox
Domestic Robots
Backplate
Pieza de Acero
Mexico
N. Amer.

Second normal form (2NF)

·         The table must be in 1NF.
·         None of the non-prime attributes of the table are functionally dependent on a part (proper subset) of a candidate key; in other words, all functional dependencies of non-prime attributes on candidate keys are fully functional dependencies. For example, consider a "Department Members" table whose attributes are Department ID, Employee ID, and Employee Date of Birth; and suppose that an employee works in one or more departments. The combination of Department ID and Employee ID uniquely identifies records within the table. Given that Employee Date of Birth depends on only one of those attributes – namely, Employee ID – the table is not in 2NF.
·         Note that if none of a 1NF table's candidate keys are composite – i.e. every candidate key consists of just one attribute – then we can say immediately that the table is in 2NF

Example:

                            Suppliers and Parts By Company Division
Company
Division
Part Type
Supplier
Allied Clock and Watch
Clocks
Spring
Tensile Globodynamics
Allied Clock and Watch
Clocks
Pendulum
Tensile Globodynamics
Allied Clock and Watch
Clocks
Spring
Pieza de Acero
Allied Clock and Watch
Clocks
Toothed Wheel
Pieza de Acero
Allied Clock and Watch
Watches
Quartz Crystal
Microflux
Allied Clock and Watch
Watches
Tuning Fork
Microflux
Allied Clock and Watch
Watches
Battery
Dakota Electrics
Global Robot
Industrial Robots
Flywheel
Wheels 4 Less
Global Robot
Industrial Robots
Axle
Wheels 4 Less
Global Robot
Industrial Robots
Axle
TransEuropa
Global Robot
Industrial Robots
Mechanical Arm
TransEuropa
Global Robot
Domestic Robots
Artificial Brain
Prometheus Labs
Global Robot
Domestic Robots
Artificial Brain
Frankenstein Labs
Global Robot
Domestic Robots
Metal Housing
Pieza de Acero
Global Robot
Domestic Robots
Backplate
Pieza de Acero
Companies
Company
Company Founder
Company Logo
Allied Clock and Watch
Horace Washington
Sundial
Global Robot
Nils Neumann
Gearbox
Suppliers
Supplier
Supplier Country
Supplier Continent
Tensile Globodynamics
USA
N. Amer.
Pieza de Acero
Mexico
N. Amer.
Microflux
Belgium
Europe
Dakota Electrics
USA
N. Amer.
Wheels 4 Less
USA
N. Amer.
TransEuropa
Italy
Europe
Prometheus Labs
Luxembourg
Europe
Frankenstein Labs
Germany
Europe

Third normal form (3NF)

·         The table must be in 2NF.
·         There are no non-trivial functional dependencies between non-prime attributes. A violation of 3NF would mean that at least one non-prime attribute is only indirectly dependent (transitively dependent) on a candidate key, by virtue of being functionally dependent on another non-prime attribute. For example, consider a "Departments" table whose attributes are Department ID, Department Name, Manager ID, and Manager Hire Date; and suppose that each manager can manage one or more departments. {Department ID} is a candidate key. Although Manager Hire Date is functionally dependent on {Department ID}, it is also functionally dependent on the non-prime attribute Manager ID. This means the table is not in 3NF.

Example

Not in 3NF:

Tournament Winners
Tournament
Year
    Winner
Winner Date of Birth
Indiana Invitational
Cleveland Open
Des Moines Masters
Indiana Invitational
1998
1999
1999
1999      
Al Fredrickson
Bob Albertson  
Al Fredrickson
Chip Masterson
21 July 1975
28 September 1968
21 July 1975
14 March 1977
*The only candidate key is {Tournament, Year}.

In 3NF:




Tournament Winners
Tournament
Year
Winner
Indiana Invitational
Cleveland Open
Des Moines Masters
Indiana Invitational
1998      
1999
1999
1999
Al Fredrickson
Bob Albertson
Al Fredrickson
Chip Masterson
Player Dates of Birth
Player
Date of Birth
Chip Masterson
Al Fredrickson
Bob Albertson
14 March 1977
21 July 1975
28 September 1968

 

 

 

 

                                                                               
                               

Boyce-Codd normal form (BCNF):

The criteria for Boyce-Codd normal form (BCNF) are:
·         The table must be in 3NF.
·         Every non-trivial functional dependency must be a dependency on a superkey

Example:

Suppliers and Parts By Company Division
Company
Division
Part Type
Supplier
Allied Clock and Watch
Clocks
Spring
Tensile Globodynamics
Allied Clock and Watch
Clocks
Pendulum
Tensile Globodynamics
Allied Clock and Watch
Clocks
Spring
Pieza de Acero
Allied Clock and Watch
Clocks
Toothed Wheel
Pieza de Acero
Allied Clock and Watch
Watches
Quartz Crystal
Microflux
Allied Clock and Watch
Watches
Tuning Fork
Microflux
Allied Clock and Watch
Watches
Battery
Dakota Electrics
Global Robot
Industrial Robots
Flywheel
Wheels 4 Less
Global Robot
Industrial Robots
Axle
Wheels 4 Less
Global Robot
Industrial Robots
Axle
TransEuropa
Global Robot
Industrial Robots
Mechanical Arm
TransEuropa
Global Robot
Domestic Robots
Artificial Brain
Prometheus Labs
Global Robot
Domestic Robots
Artificial Brain
Frankenstein Labs
Global Robot
Domestic Robots
Metal Housing
Pieza de Acero
Global Robot
Domestic Robots
Backplate
Pieza de Acero
Suppliers
Supplier
Supplier Country
Tensile Globodynamics
USA
Pieza de Acero
Mexico
Microflux
Belgium
Dakota Electrics
USA
Wheels 4 Less
USA
TransEuropa
Italy
Prometheus Labs
Luxembourg
Frankenstein Labs
Germany
Companies
Company
Company Founder
Company Logo
Allied Clock and Watch
Horace Washington
Sundial
Global Robot
Nils Neumann
Gearbox
Countries
Country
Continent
USA
N. Amer.
Mexico
N. Amer.
Belgium
Europe
Italy
Europe
Luxembourg
Europe

Fourth normal form (4NF):

The criteria for fourth normal form (4NF) are:
·         The table must be in BCNF.
·         There must be no non-trivial multivalued dependencies on something other than a superkey. A BCNF table is said to be in 4NF if and only if all of its multivalued dependencies are functional dependencies.

Example:

Companies
Company
Company Founder
Company Logo
Allied Clock and Watch
Horace Washington
Sundial
Global Robot
Nils Neumann
Gearbox
International Broom
Gareth Patterson
Whirlwind
International Broom
Sandra Patterson
Whirlwind
International Broom
Gareth Patterson
Sweeper
International Broom
Sandra Patterson
Sweeper
Suppliers and Parts By Company Division
Company
Division
Part Type
Supplier
Allied Clock and Watch
Clocks
Spring
Tensile Globodynamics
Allied Clock and Watch
Clocks
Pendulum
Tensile Globodynamics
Allied Clock and Watch
Clocks
Spring
Pieza de Acero
Allied Clock and Watch
Clocks
Toothed Wheel
Pieza de Acero
Allied Clock and Watch
Watches
Quartz Crystal
Microflux
Allied Clock and Watch
Watches
Tuning Fork
Microflux
Allied Clock and Watch
Watches
Battery
Dakota Electrics
Global Robot
Industrial Robots
Flywheel
Wheels 4 Less
Global Robot
Industrial Robots
Axle
Wheels 4 Less
Global Robot
Industrial Robots
Axle
TransEuropa
Global Robot
Industrial Robots
Mechanical Arm
TransEuropa
Global Robot
Domestic Robots
Artificial Brain
Prometheus Labs
Global Robot
Domestic Robots
Artificial Brain
Frankenstein Labs
Global Robot
Domestic Robots
Metal Housing
Pieza de Acero
Global Robot
Domestic Robots
Backplate
Pieza de Acero
Companies
Company
Allied Clock and Watch
Global Robot
International Broom
Company Logos
Company
Company Logo
Allied Clock and Watch
Sundial
Global Robot
Gearbox
International Broom
Whirlwind
International Broom
Sweeper
Company Founders
Company
Company Founder
Allied Clock and Watch
Horace Washington
Global Robot
Nils Neumann
International Broom
Gareth Patterson
International Broom
Sandra Patterson
Suppliers
Supplier
Supplier Country
Tensile Globodynamics
USA
Pieza de Acero
Mexico
Microflux
Belgium
Dakota Electrics
USA
Wheels 4 Less
USA
TransEuropa
Italy
Prometheus Labs
Luxembourg
Frankenstein Labs
Germany
Countries
Country
Continent
USA
N. Amer.
Mexico
N. Amer.
Belgium
Europe
Italy
Europe
Luxembourg
Europe

Fifth normal form (5NF):

The criteria for fifth normal form (5NF and also PJ/NF) are:
·         The table must be in 4NF.
·         There must be no non-trivial join dependencies that do not follow from the key constraints. A 4NF table is said to be in the 5NF if and only if every join dependency in it is implied by the candidate keys.


Example:

Part Types By Company Division
Company
Division
Part Type
Allied Clock and Watch
Clocks
Spring
Allied Clock and Watch
Clocks
Pendulum
Allied Clock and Watch
Clocks
Toothed Wheel
Allied Clock and Watch
Watches
Quartz Crystal
Allied Clock and Watch
Watches
Tuning Fork
Allied Clock and Watch
Watches
Battery
Global Robot
Industrial Robots
Flywheel
Global Robot
Industrial Robots
Axle
Global Robot
Industrial Robots
Mechanical Arm
Global Robot
Domestic Robots
Artificial Brain
Global Robot
Domestic Robots
Metal Housing
Global Robot
Domestic Robots
Backplate
Suppliers By Company Division
Company
Division
Supplier
Allied Clock and Watch
Clocks
Tensile Globodynamics
Allied Clock and Watch
Clocks
Pieza de Acero
Allied Clock and Watch
Watches
Microflux
Allied Clock and Watch
Watches
Dakota Electrics
Global Robot
Industrial Robots
Wheels 4 Less
Global Robot
Industrial Robots
TransEuropa
Global Robot
Domestic Robots
Prometheus Labs
Global Robot
Domestic Robots
Frankenstein Labs
Global Robot
Domestic Robots
Pieza de Acero
Parts By Supplier
Part Type
Supplier
Spring
Tensile Globodynamics
Pendulum
Tensile Globodynamics
Spring
Pieza de Acero
Toothed Wheel
Pieza de Acero
Quartz Crystal
Microflux
Tuning Fork
Microflux
Battery
Dakota Electrics
Flywheel
Wheels 4 Less
Axle
Wheels 4 Less
Axle
TransEuropa
Mechanical Arm
TransEuropa
Artificial Brain
Prometheus Labs
Artificial Brain
Frankenstein Labs
Metal Housing
Pieza de Acero
Backplate
Pieza de Acero
Companies
Company
Company Logo
Allied Clock and Watch
Sundial
Global Robot
Gearbox
Company Founders
Company
Company Founder
Allied Clock and Watch
Horace Washington
Global Robot
Nils Neumann
International Broom
Gareth Patterson
International Broom
Sandra Patterson
Suppliers
Supplier
Supplier Country
Tensile Globodynamics
USA
Pieza de Acero
Mexico
Microflux
Belgium
Dakota Electrics
USA
Wheels 4 Less
USA
TransEuropa
Italy
Prometheus Labs
Luxembourg
Frankenstein Labs
Germany
Countries
Country
Continent
USA
N. Amer.
Mexico
N. Amer.
Belgium
Europe
Italy
Europe
Luxembourg
Europe