You will find the two tables below, ideas on how to enforce constaint that Nodes which have NodeTypeId 2 (CannotBeParent) can not be a pops in place of result in?
- Modified by the gao.seng Wednesday,
Solutions
- PM
I would begin by including a flag to dining table [NodeTypes], to indicate in the event your node kind of can participate since the father or mother into the a romance.
See that And i am incorporating a different limitation by (NodeTypeId, CanBeParent). This is redundant, nonetheless it will allow me to later on explore a different trick pointing to they.
The next phase is always to include so it column and to chatroulette mobiele site desk [Nodes], to ensure your couples (NodeTypeId, CanBeParent) is present into the dining table [NodeTypes]. We are going to also add a computed continuing line (ParentNodeId_CanBeParent), which have value equivalent 1, to force [ParentNodeId] to participate as mother or father automagically, and resource desk [Nodes] utilizing the few (ParentNodeId, ParentNodeId_CanBePArent).
It might be smoother if the managed to have fun with a choose report into the a check constraint, or being capable perform assertions (examine limitation at database level), however, T-SQL lacks of them has to date.
- 6:58 PM
All of the reactions
Where was Alex Kuznetsov when you require him? Alex came up with constraintss to help you impose business regulations really anybody do merely consider is you are able to that have produces.
Then again, he commonly reaches you to by adding an enthusiastic auxillary column. Maybe you will give more history on what you are seeking go? That’ll to provide motivation to a few solution.
If Can be acquired (Look for Better 1 step 1 Away from NodeTypes Inner Signup Nodes Towards the Nodes.NodeId = And you can Nodes.FK_NodeTypeId = NodeTypes.NodeTypeId And you can NodeTypes.NodeType = ‘CanBeParent’) Start Place = step 1 Avoid Otherwise Begin Lay = 0 Stop Get back Avoid;
, NodeType varchar(255) not null ) do table Nodes (NodeId int limitation PK__Nodes_NodeId Top Secret , FK_NodeTypeId int restriction FK__NodeTypes_NodeTypeId sources NodeTypes (NodeTypeId) , FK_ParentNodeid int limitation FK__Nodes_ParentNodeId__Nodes_NodeId references Nodes (NodeId) Take a look at (dbo.CanBeParent(FK_ParentNodeid) = 1) )
insert Nodes values (1,1,1) <– this will pass, because it has a parent 1 insert Nodes values (2,2,1) <– this will pass, because it has a parent 1 which can be a parent insert Nodes values (3,2,2) <– this will fail, the parent nodeId=2, is of NodeTypeId 2, which cant be a parent
- Suggested because the address of the sdsuser Wednesday,
We realize Alex a great deal for the SQLBlog I am able to dig greater indeed there, but you try right the greater amount of I believe about it demand the nearer I have so you can an answer. It’s brand of like which have a buyers that have an assigned conversion process associate and have now all members of the fresh new representatives strings of command each other up-and-down, nevertheless the buyers is far more such as an online personnel but can’t manage some body. In this instance an effective workstation is also strings together with her a set of equipment regarding a residential area pond such as for example adult cams and you may microphones, nevertheless the workstation by itself cannot be a parent device. I do believe the customer-sales professional desk framework may implement here, I can feedback the application design file to find out if one is practical.
- Advised just like the respond to because of the sdsuser Wednesday,
- Unproposed because the respond to by the sdsuser Wednesday,
- PM
Notably less undetectable just like the a trigger, it could functions. I will observe nHibernate enjoys they the next day, and you will my personal director.
- Modified from the Kalman Toth Friday,
I’d start with adding a banner so you can dining table [NodeTypes], to indicate whether your node form of can take part since father or mother during the a relationship.
Observe that I am also adding a separate restriction by (NodeTypeId, CanBeParent). That is redundant, it enable me to after explore a foreign secret directing in order to they.
The next thing is always to incorporate it column and also to dining table [Nodes], to make sure that pair (NodeTypeId, CanBeParent) can be found in desk [NodeTypes]. We will also add a determined proceeded column (ParentNodeId_CanBeParent), with worthy of equal 1, to force [ParentNodeId] to join just like the parent automagically, and you can resource dining table [Nodes] by using the couple (ParentNodeId, ParentNodeId_CanBePArent).
It would be easier if the been able to fool around with a choose report in to the a check constraint, or becoming in a position to would assertions (take a look at restriction at the database height), however, T-SQL lacks ones possess currently.
- 6:58 PM
There is no such thing as a “node_type_id” there can be a “_type” or a “_id” in a correct data model but not a weird hybrid. An identifier is for entities (emp_id) a type encoding a scalar value (blood_type).
We never use meta data like “pk_” or “fk_” during the analysis feature identity. The reason for a document function name is to tell united states what it is because of the its characteristics and never how it was used in this particular desk.
When you use more than-sized sequence such as this, your desk usually fill which have scrap. A beneficial coders explore references if gang of values are large otherwise volition. Such, an order detail UPC you’ll reference the brand new Collection table to help you demand a rule that we just take purchases getting gifts within the stock.
In the event that place is small and secure, up coming i play with a (x Into the (..)) constants. Eg, “sex_code TINYINT Standard 0 Not NULL Evaluate (sex_code For the (0, step one, 2, 9)) — iso-5218
Manage Table Nodes (node_id INTEGER Maybe not NULL No. 1 Key, node_sorts of INTEGER Perhaps not NULL Sources Node_Sizes (node_type), parent_node_id INTEGER References Nodes (node_id));
What you have created are an enthusiastic adjacency checklist design. They imitates low-relational pointer stores within the SQL. It contains one another agencies (nodes) and also the hierarchy (relationship). However, a correct can not be each other (Chen? E-Roentgen modeling? Freshman database classification at school?). Thus although this is nonetheless an invalid research design, you have authored it as
Create Table Tree_and_Nodes (node_id INTEGER Maybe not NULL Number 1 Trick, node_particular CHAR(1) Standard ‘N’ Not NULL Consider (node_type in (‘P’, ‘N’), — P= Possible Parent, N= Non-Parent mother or father_node_id INTEGER Sources Forest_and_Nodes (node_id), See (Instance Whenever node_type = ‘N’ And you can mother or father_node_id Is not NULL Up coming ‘F’ Otherwise ‘T’ Stop = ‘T’) );
–CELKO– Guides for the Celko Collection to own Morgan-Kaufmann Posting: Analytics and you will OLAP inside SQL / Studies and Database: Maxims in practice Study / Dimensions and you can Criteria in the SQL SQL to possess Sming Build / SQL Puzzles and Responses / Considering during the Establishes / Woods and Hierarchies in the SQL