How implement object orientation features in sql server?

  • I know features of object orientation could not implemented, but i want to find a way to implement them. For example for implementing inheritance we can use foreign key.

    Please help me to find some ways for encapsulation and other features.

  • tz.bahrami (12/26/2012)


    I know features of object orientation could not implemented, but i want to find a way to implement them. For example for implementing inheritance we can use foreign key.

    Please help me to find some ways for encapsulation and other features.

    Can you rephrase your question? I don't understand what you are asking here at all.

    How can you a foreign key to implement inheritance?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • tz.bahrami (12/26/2012)


    I know features of object orientation could not implemented, but i want to find a way to implement them.

    Only advice I can give you there is don't. SQL is not an object database. You may be able to force some object stuff into the DB if you hammer hard enough, doesn't make it a good idea.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Only expose stored procedures as the interface to the front end. Do whatever you like behind the scenes. It is about as object oriented as you can get with a database.

  • Here is some available reading on the topic that may help you gain a deeper understanding of what things to consider before implementing your database:

    Object-relational impedance mismatch, From Wikipedia

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • In inheritance a child table can gain father's features, so if i add child table's primary key to father's table as foreign key, it can gain father's features it is like inheritance.is'nt it?

  • tz.bahrami (12/27/2012)


    In inheritance a child table can gain father's features, so if i add child table's primary key to father's table as foreign key, it can gain father's features it is like inheritance.is'nt it?

    That would limit you to one child. If you make a "join" table between them, it will allow you to have many children or parents. A join table row would contain keys for the parent and child.

    Once again...if you use stored procedure calls you can encapsulate everything, hide implementation, and have loose coupling. Think of stored procedures as method calls.

  • Thanks for your instruction, i am familiar with junction table, but i look a way to implement something like inheritance. In addition i agree with you for encapsulating by stored procedures.

  • tz.bahrami (12/27/2012)


    Thanks for your instruction, i am familiar with junction table, but i look a way to implement something like inheritance. In addition i agree with you for encapsulating by stored procedures.

    Your request still doesn't make sense. If you MUST think of sql as objects think of the table as the object and each row as the instance and each column is the property. What you are describing is you want to inherit the row. This doesn't make sense because in OOP that would be like inheriting the properties of an instance. It doesn't work like that. You inherit the object.

    I think that what you are really trying to do is to have a parent - child relationship in sql. This of course is not only possible, it is a fundamental base for normalized data.

    Is the following example something along the lines of what you talking about?

    create table #Parent

    (

    ParentID int identity,

    SomeValue varchar(10)

    )

    create table #Child

    (

    ChildID int identity,

    ParentID int,

    SomeValue varchar(10)

    )

    insert #Parent

    select 'Parent 1' union all

    select 'Parent 2'

    insert #Child

    select 1, 'Child 1' union all

    select 1, 'Child 2' union all

    select 2, 'Child 3' union all

    select 2, 'Child 4' union all

    select 2, 'Child 5'

    select *

    from #Parent p

    join #Child c on p.ParentID = c.ParentID

    drop table #Parent

    drop table #Child

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • You can create views to "merge" two tables but they get tricky when you try to update columns from both tables.

    You could create a wide table (sparse) containing all the sub-types as nullable columns and implement views on it to give you your subset that you can update.

  • Thanks alot.

    How about polymorphism in sql? Could we use stored procedures for polymorphism?

    And for nested relationships can we define an attribute as OId and use it in a select query to point to the table that we want use it in our nested relationship?

  • tz.bahrami (12/27/2012)


    Thanks alot.

    How about polymorphism in sql? Could we use stored procedures for polymorphism?

    And for nested relationships can we define an attribute as OId and use it in a select query to point to the table that we want use it in our nested relationship?

    HUH???? How can a stored procedure be used to implement polymorphism? sql is NOT an OO programming language. It is a data storage system. It is not ever going to be this type of OO entity that you are talking about.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • tz.bahrami (12/27/2012)


    Thanks alot.

    How about polymorphism in sql? Could we use stored procedures for polymorphism?

    And for nested relationships can we define an attribute as OId and use it in a select query to point to the table that we want use it in our nested relationship?

    It depends on what you mean by that and how you want to class your entities. In the context of a database you could contrive a qualifying description of polymorphism, but in general, no. Relational database theory is built on the notion of a relation (manifested as a table in an RDBMS) representing a specific entity, and only one. In other words you would not store cars and trees in the same table even though they both inherit from 'System.Object' (drawing on .NET object hierarchy). You might store different models of cars or species of trees where car inherits from vehicle and tree inherits from plant. Or you might want a vehicle table with properties common to cars, trucks, airplanes, etc. with child tables for each of those I mentioned. It depends on how granular you want to get.

    For example say you had a table that stored types of trees (named tree) and in that table you wanted to store a deciduous tree and an evergreen. You might have a column named 'max_recorded_height' and another named 'average_life_expectancy.' However, you may not want a column called 'average_needle_length' because deciduous trees do not have needles, only evergreens do. So, you may want to create a child table to tree called evergreen that would only be referred to by objects of type evergreen (who inherit from your tree object) that was instantiated within your application so it could fill those properties that only pertained to evergreens. That said, you would still look into having those properties normalized out of the tree table since not all trees will have needles.

    I would suggest you do some reading on normalization, especially normal forms, start with 3NF, and read the article about the impedance mismatch I linked to above.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • tz.bahrami (12/27/2012)


    How about polymorphism in sql? Could we use stored procedures for polymorphism?

    And for nested relationships can we define an attribute as OId and use it in a select query to point to the table that we want use it in our nested relationship?

    Once again... SQL Server is not an object database. T-SQL is not an object orientated language.

    While you may be able to force some OO-like features into a DB, you're very likely going to end up with a poor design and a badly performing database as a result. Keep the OO in the front end, where your language is OO and not in the database.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks alot of your instructions.i find my answer.

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply