Welcome to Sign in | Help

Re: update ciudat

  •  09-24-2007, 8:57 PM

    Re: update ciudat

    Pentru varianta in care apelezi la o cheie externa pentru a putea lega fiecare comanda de articole comandate se poate utiliza o singura tabelă (test , nu este recomandabilă) si poti crea o relaţie circulară de la cheia primară test.col1 la cheia externa  test.col4 :

    ALTER TABLE test
    ADD col4 INT  CONSTRAINT test_cheie_externa_1 FOREIGN KEY (col4) REFERENCES test(col1)

    UPDATE test
    SET col4 = (SELECT MAX(T2.col1) FROM test T2 WHERE T2.col2 = '6a' AND T2.col1 < T1.col1)
    FROM test T1
    WHERE T1.col2 = '6b'

     

    Exemplu interogare:

    SELECT Comanda.*, ArticolComandat.*
    FROM
    (SELECT * FROM test WHERE col2 = '6a') AS Comanda
    INNER JOIN
    (SELECT * FROM test WHERE col2 = '6b') AS ArticolComandat
    ON Comanda.col1 = ArticolComandat.col4

    A doua solutie daca apelezi la crearea unei chei externe consta in creare a doua tabele separate: o tabela care contine doar comenzile, si o tabela care contine doar articolele comandate.

    UPDATE test_articol_comanda SET col4 = NULL  --optional

    SELECT *
    INTO test_comanda
    FROM test
    WHERE col2 = '6a'

    SELECT *
    INTO test_articol_comanda
    FROM test
    WHERE col2 = '6b'

    SELECT * FROM test_comanda
    SELECT * FROM test_articol_comanda

    UPDATE test_articol_comanda
    SET col4 = (SELECT MAX(T2.col1) FROM test T2 WHERE T2.col2 = '6a' AND T2.col1 < T1.col1)
    FROM test_articol_comanda T1
    WHERE T1.col2 = '6b'


    Exemplu interogare:

    SELECT *
    FROM test_comanda C
    INNER JOIN
    test_articol_comanda AC
    ON C.col1 = AC.col4


     

     

View Complete Thread
Powered by Community Server (Commercial Edition), by Telligent Systems