Comparing SQL and Prolog

后端 未结 8 646
深忆病人
深忆病人 2021-01-29 18:34

I\'ve started learning Prolog and wondering about the theoretical difference from the SQL language.

For example:

  • both are declarative languages
  • b
相关标签:
8条回答
  • 2021-01-29 19:17

    xonix, you need more development experience to say whether something can be done in sql or not.

    Below are at least 2 solutions for your fibo series. One using Stored Procedure and the other using CTE. Take your pick.

    METHOD 1

    declare @a int, @b int, @c int, @i int, @N int = 10
    select @a=0, @b=1, @i=0, @c=0
    print @a
    print @b
    while @i < @N 
    Begin
    set @c=@a+@b
    print @c
    set @i=@i+1
    set @a=@b
    set @b=@c
    end
    

    METHOD 2

    WITH FibonacciNumbers (RecursionLevel, FibonacciNumber, NextNumber) 
    AS (
    -- Anchor member definition
    SELECT  
    0  AS RecursionLevel,
    0  AS FibonacciNumber,
    1  AS NextNumber
    UNION ALL
    -- Recursive member definition
    SELECT  a.RecursionLevel + 1             AS RecursionLevel,
    a.NextNumber                     AS FibonacciNumber,
    a.FibonacciNumber + a.NextNumber AS NextNumber
    FROM FibonacciNumbers a
    WHERE a.RecursionLevel < 10
    )
    -- Statement that executes the CTE
    SELECT 
    'F' + CAST( fn.RecursionLevel AS VARCHAR) AS FibonacciOrdinal, 
    fn.FibonacciNumber,
    fn.NextNumber
    FROM FibonacciNumbers fn; 
    GO
    
    0 讨论(0)
  • 2021-01-29 19:17

    Just a few thoughts:

    1. SQL is a query language for (maybe arbitrary complex) accessing to (relational) data, it's not a programming language.
    2. Even if treat SQL as programming language - it's not Turing complete. I can hardly imagine sql query returning sum of 1 to 100 (for example).
    3. SQL is language for accessing/manipulating (DML) to data bases, where Prolog is a language for working with knowledge bases (& rule resolution engine, of cause). Virtually Prolog is no more then simply unification & backtracking.
    4. This is not saying about the application domains of SQL & Prolog, which are of cause totally different - efficient (regular) data storage & AI/symbolic computations/parsing/expert systems/constraint solvers/.../much more.
    0 讨论(0)
提交回复
热议问题