Showing posts with label createa. Show all posts
Showing posts with label createa. Show all posts

Thursday, March 29, 2012

create table with recursive relationship

I am fairly new to SQL and I am currently trying to create
a SQL table (using Microsoft SQL) that has a recursive
relationship, let me try to explain:

I have a piece of Data let's call it "Item" wich may again contain one
more "Items". Now how would I design a set of SQL Tables that are
capable of storing this information?

I tried the following two approaches:

1.) create a Table "Item" with Column "ItemID" as primary key, some
colums for the Data an Item can store and a Column "ParentItemID". I
set a foreign key for ParentItemID wich links to the primarykey
"ItemID" of the same table.

2.) create separate Table "Item_ParentItem" that stores
ItemID-ParentItemID-pairs. Each column has a foreign key linked to
primary key of the "Item" Column "ItemID".

In both approaches when I try to delete an Item I get an Exception
saying that the DELETE command could not be executed because it
violates a COLUMN REFERENCE constraint. The goal behind these FK_PK
relations is is that when an Item gets deleted, all childItems should
automatically be deleted recursively.

How is this "standard-problem" usually solved in sql? Or do I inned to
implement the recursive deletion myself using stored
procedures or something ?You can get away with the first approach. However, you may not use ON
DELETE CASCADE. Rather, you are looking at a trigger that can manage this.

--
Tom

----------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
..
"Robert Ludig" <schwertfischtrombose@.gmx.de> wrote in message
news:1147782885.171670.231170@.i40g2000cwc.googlegr oups.com...
I am fairly new to SQL and I am currently trying to create
a SQL table (using Microsoft SQL) that has a recursive
relationship, let me try to explain:

I have a piece of Data let's call it "Item" wich may again contain one
more "Items". Now how would I design a set of SQL Tables that are
capable of storing this information?

I tried the following two approaches:

1.) create a Table "Item" with Column "ItemID" as primary key, some
colums for the Data an Item can store and a Column "ParentItemID". I
set a foreign key for ParentItemID wich links to the primarykey
"ItemID" of the same table.

2.) create separate Table "Item_ParentItem" that stores
ItemID-ParentItemID-pairs. Each column has a foreign key linked to
primary key of the "Item" Column "ItemID".

In both approaches when I try to delete an Item I get an Exception
saying that the DELETE command could not be executed because it
violates a COLUMN REFERENCE constraint. The goal behind these FK_PK
relations is is that when an Item gets deleted, all childItems should
automatically be deleted recursively.

How is this "standard-problem" usually solved in sql? Or do I inned to
implement the recursive deletion myself using stored
procedures or something ?|||On 16 May 2006 05:34:45 -0700, Robert Ludig wrote:

>I am fairly new to SQL and I am currently trying to create
>a SQL table (using Microsoft SQL) that has a recursive
>relationship, let me try to explain:
>I have a piece of Data let's call it "Item" wich may again contain one
>more "Items". Now how would I design a set of SQL Tables that are
>capable of storing this information?
>
>I tried the following two approaches:
(snip)

Hi Robert,

I agree with Tom that the first approach is better than the first. But
there are also some radically different ways to store a recursive
relationship or hierarchy. One of the more popular variants is the
nested set model. It's not nearly as intuitive as the model you are
proposing, but it performs far superior in some scenario's.

Google for "Nested Set Model" if you want to know the details.

--
Hugo Kornelis, SQL Server MVP|||>> How is this "standard-problem" usually solved in sql? <<

Get a copy of TREES & HIERARCHIES IN SQL for several ways to model this
kind of problem.

>> Or do I inned to implement the recursive deletion myself using stored
procedures or something ? <<

No need for recursive procedural code if you use the nested sets model.
Younger programmers who learned HTML, XML, etc. find it to be
intuitive. Older programmers who grew up with pointer chains need to
adjust their mind-set.

Create table with PK on two columns

Hi,
Could you tell me the syntax to set primary key on two columns when I create
a table? I can't find it in the books. The syntax I found is
CREATE TABLE MyTable (c1 INT PRIMARY KEY,c2 INT)
How can put c2 as part of PRIMARY KEY?
I don't know how to use CONSTRAINT. So if CONSTRAINT is need, pls tell me.
Thanks.Chrissi wrote:
> Hi,
> Could you tell me the syntax to set primary key on two columns when I
> create a table? I can't find it in the books. The syntax I found is
> CREATE TABLE MyTable (c1 INT PRIMARY KEY,c2 INT)
> How can put c2 as part of PRIMARY KEY?
> I don't know how to use CONSTRAINT. So if CONSTRAINT is need, pls
> tell me.
> Thanks.
Create table MyTable (
c1 INT NOT NULL,
c2 INT NOT NULL
PRIMARY KEY (c1, c2) )
or
Create table MyTable (
c1 INT NOT NULL,
c2 INT NOT NULL )
Alter Table MyTable
ADD PRIMARY KEY (c1, c2)
David Gugick
Imceda Software
www.imceda.com|||Create Table MyTable
(c1 INT Not Null,
c2 INT Not Null,
Primary Key (C1, c2))
"§Chrissi§" wrote:

> Hi,
> Could you tell me the syntax to set primary key on two columns when I crea
te
> a table? I can't find it in the books. The syntax I found is
> CREATE TABLE MyTable (c1 INT PRIMARY KEY,c2 INT)
> How can put c2 as part of PRIMARY KEY?
> I don't know how to use CONSTRAINT. So if CONSTRAINT is need, pls tell me
.
> Thanks.
>
>|||CREATE TABLE MyTable (
c1 INT NOT NULL,
c2 INT NOT NULL,
CONSTRAINT pk_MyTable PRIMARY KEY(c1 ,c2)
)
or
CREATE TABLE MyTable (
c1 INT NOT NULL,
c2 INT NOT NULL,
PRIMARY KEY(c1 ,c2)
)
It's technically a constraint in both cases, but you aren't
required to give it a name.
Steve Kass
Drew University
Chrissi wrote:

>Hi,
>Could you tell me the syntax to set primary key on two columns when I creat
e
>a table? I can't find it in the books. The syntax I found is
>CREATE TABLE MyTable (c1 INT PRIMARY KEY,c2 INT)
>How can put c2 as part of PRIMARY KEY?
>I don't know how to use CONSTRAINT. So if CONSTRAINT is need, pls tell me.
>Thanks.
>
>|||Server: Msg 1911, Level 16, State 1, Line 1
Column name 'C1' does not exist in the target table.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.
Watch your spelling! Some of us choose a case-sensitive
collation now and then. ;)
SK
CBretana wrote:
>Create Table MyTable
> (c1 INT Not Null,
> c2 INT Not Null,
> Primary Key (C1, c2))
>"§Chrissi§" wrote:
>
>|||Oops! My typing is never good (two finger hint n pec) but I noticed the
upper case C and left it that way anyway... Out of curiousity, why are you
using case-sensitive collation?
"Steve Kass" wrote:

> Server: Msg 1911, Level 16, State 1, Line 1
> Column name 'C1' does not exist in the target table.
> Server: Msg 1750, Level 16, State 1, Line 1
> Could not create constraint. See previous errors.
> Watch your spelling! Some of us choose a case-sensitive
> collation now and then. ;)
> SK
> CBretana wrote:
>
>|||
CBretana wrote:

> Oops! My typing is never good (two finger hint n pec) but I noticed the
> upper case C and left it that way anyway... Out of curiousity, why are you
> using case-sensitive collation?
Mostly so I can generate the appropriate error messages to include in posts
like this one. ;)
I didn't used to pay attention to this, and it didn't matter as much when
keypunch machines were uppercase-only, or with case-insensitive languages
like Pascal. I had to break sloppy habits when C came along, and though I
slipped into old habits when I started using SQL, I've found more and more
reasons not to be sloppy lately, such as keeping Erland from bugging me
if I put "northwind"."orders" in examples I post. ;)
There are plenty of things you can write that will behave differently
according to collation and language settings, and forcing myself to
be careful about case helps me see and avoid them.
SK
> "Steve Kass" wrote:
>|||>> Out of curiousity, why are you using case-sensitive collation? <<
Because Standard SQL is case-sensitive.

Friday, February 17, 2012

create database statement

Where does the SQL Server default the "file name" parameter when you create
a database WITHOUT entering your own parameter for "file name".
You can do a simple "create database somename" and it will create the
database with the defaulted "file name" to be where SQL Server is installed
(location of the mdf and ldf). Does SQL Server take the default location
from the model or master databases or some file group setting?
Thanks in advance
I believe it defaults to the location of model. You can change this in EM
by right-clicking on your server and changing the defaults in the server
properties dialog, or by modifying the registry directly. The defaults are
stored at HKLM\Software\Microsoft\MSSQLServer\MSSQLServer. Two keys:
DefaultData and DefaultLog, which should both be REG_SZ. Note that if you
haven't previously modified this setting in EM, the keys will not yet exist.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
"Homer" <Homer@.discussions.microsoft.com> wrote in message
news:66B065D1-287D-485E-B3C5-425852D83EFE@.microsoft.com...
> Where does the SQL Server default the "file name" parameter when you
> create
> a database WITHOUT entering your own parameter for "file name".
> You can do a simple "create database somename" and it will create the
> database with the defaulted "file name" to be where SQL Server is
> installed
> (location of the mdf and ldf). Does SQL Server take the default location
> from the model or master databases or some file group setting?
> Thanks in advance
|||The following article should explain the New Database default locations.
Basically though these values are stored in the registry for each SQL server
instance.
http://www.wardyit.com/blog/blog/arc.../11/10/58.aspx
- Peter Ward
WARDY IT Solutions
"Homer" wrote:

> Where does the SQL Server default the "file name" parameter when you create
> a database WITHOUT entering your own parameter for "file name".
> You can do a simple "create database somename" and it will create the
> database with the defaulted "file name" to be where SQL Server is installed
> (location of the mdf and ldf). Does SQL Server take the default location
> from the model or master databases or some file group setting?
> Thanks in advance

create database statement

Where does the SQL Server default the "file name" parameter when you create
a database WITHOUT entering your own parameter for "file name".
You can do a simple "create database somename" and it will create the
database with the defaulted "file name" to be where SQL Server is installed
(location of the mdf and ldf). Does SQL Server take the default location
from the model or master databases or some file group setting?
Thanks in advanceI believe it defaults to the location of model. You can change this in EM
by right-clicking on your server and changing the defaults in the server
properties dialog, or by modifying the registry directly. The defaults are
stored at HKLM\Software\Microsoft\MSSQLServer\MSSQ
LServer. Two keys:
DefaultData and DefaultLog, which should both be REG_SZ. Note that if you
haven't previously modified this setting in EM, the keys will not yet exist.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Homer" <Homer@.discussions.microsoft.com> wrote in message
news:66B065D1-287D-485E-B3C5-425852D83EFE@.microsoft.com...
> Where does the SQL Server default the "file name" parameter when you
> create
> a database WITHOUT entering your own parameter for "file name".
> You can do a simple "create database somename" and it will create the
> database with the defaulted "file name" to be where SQL Server is
> installed
> (location of the mdf and ldf). Does SQL Server take the default location
> from the model or master databases or some file group setting?
> Thanks in advance|||The following article should explain the New Database default locations.
Basically though these values are stored in the registry for each SQL server
instance.
http://www.wardyit.com/blog/blog/ar...5/11/10/58.aspx
- Peter Ward
WARDY IT Solutions
"Homer" wrote:

> Where does the SQL Server default the "file name" parameter when you cre
ate
> a database WITHOUT entering your own parameter for "file name".
> You can do a simple "create database somename" and it will create the
> database with the defaulted "file name" to be where SQL Server is installe
d
> (location of the mdf and ldf). Does SQL Server take the default location
> from the model or master databases or some file group setting?
> Thanks in advance