Student: Estera
Anul universitar: 2024-2025
Facultatea de Științe Economice
Universitatea "Lucian Blaga" din Sibiu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- ======================================== | |
| -- 7. CURSOARE SQL (MINIMUM 2) | |
| -- ======================================== | |
| -- Cursor 1: Procesarea comenzilor pentru actualizarea scorului de fidelitate | |
| DECLARE @ClientID INT, @TotalComanda DECIMAL(12,2), @StatusComanda NVARCHAR(50); | |
| DECLARE @PuncteBonus DECIMAL(5,2); | |
| DECLARE cursor_fidelitate CURSOR FOR | |
| SELECT co.ID_Client, co.Total, co.Status |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Trigger 1: Actualizare automată total comandă la inserare/modificare detalii | |
| CREATE TRIGGER tr_ActualizeazaTotalComanda | |
| ON Detalii_Comanda | |
| AFTER INSERT, UPDATE, DELETE | |
| AS | |
| BEGIN | |
| SET NOCOUNT ON; | |
| -- Pentru INSERT și UPDATE | |
| IF EXISTS(SELECT * FROM inserted) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- ======================================== | |
| -- 6. PROCEDURI STOCATE (MINIMUM 3) | |
| -- ======================================== | |
| -- Procedura stocată 1: Adăugare comandă nouă | |
| CREATE PROCEDURE sp_AdaugaComanda | |
| @ID_Client INT, | |
| @Produse NVARCHAR(MAX), -- Format: 'ID_Produs:Cantitate,ID_Produs:Cantitate' | |
| @Adresa_Livrare NVARCHAR(255) = NULL, | |
| @Observatii NTEXT = NULL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- ======================================== | |
| -- 5. STRUCTURI DE CONTROL (MINIMUM 2) | |
| -- ======================================== | |
| -- Structura de control 1: IF-ELSE pentru actualizarea stocului | |
| DECLARE @ID_Produs INT = 1; | |
| DECLARE @Cantitate_Vanduta INT = 5; | |
| DECLARE @Stoc_Actual INT; | |
| SELECT @Stoc_Actual = Stoc FROM Produse WHERE ID_Produs = @ID_Produs; |
Views (Vederile) sunt interogări SQL predefinite care se comportă ca tabele virtuale, oferind o modalitate convenabilă de a accesa și prezenta datele complexe într-un format simplificat. Acestea nu stochează date fizic, ci execută interogarea de fiecare dată când sunt accesate.
- Simplificare: Ascund complexitatea interogărilor cu multiple JOIN-uri
- Securitate: Permit accesul controlat la anumite coloane/rânduri
- Reutilizare: Pot fi folosite în multiple contexte fără rescrierea codului
- Consistență: Asigură o logică uniformă de accesare a datelor
Această documentație descrie 25 de interogări SQL pentru un sistem de management al comenzilor, produselor și clienților. Interogările acoperă operațiuni de bază până la cele complexe, inclusiv SELECT, INSERT, UPDATE, DELETE și crearea de tabele temporare.
Sistemul utilizează următoarele tabele principale:
- Clienti - informații despre clienți (ID_Client, Nume, Email, Tip_Client, Scor_Fidelitate, etc.)
- Comenzi - comenzile plasate (ID_Comanda, ID_Client, Data_Comanda, Total, Status, etc.)
- Produse - catalogul de produse (ID_Produs, Denumire, Pret, Stoc, Categorie, etc.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- ======================================== | |
| -- 2. INSERAREA DATELOR DE TEST | |
| -- ======================================== | |
| -- Inserare Clienti | |
| INSERT INTO Clienti (Nume, Email, Data_Inscriere, Tip_Client, Telefon, Data_Nasterii, Scor_Fidelitate, Adresa) VALUES | |
| ('Popescu Ion', 'ion.popescu@email.com', '2024-01-15', 'Premium', '0721234567', '1985-03-20', 150.5, 'Str. Libertatii nr. 10'), | |
| ('Ionescu Maria', 'maria.ionescu@email.com', '2024-02-20', 'Standard', '0731234567', '1990-07-15', 75.0, 'Bd. Victoriei nr. 25'), | |
| ('Georgescu Ana', 'ana.georgescu@email.com', '2024-03-10', 'Gold', '0741234567', '1988-11-30', 300.0, 'Str. Mihai Viteazu nr. 5'), | |
| ('Dumitrescu Radu', 'radu.dumitrescu@email.com', '2024-04-05', 'Standard', '0751234567', '1992-05-10', 50.0, 'Calea Nationala nr. 15'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Tabel: Clienti (cu diverse tipuri de date) | |
| CREATE TABLE Clienti ( | |
| ID_Client INT PRIMARY KEY IDENTITY(1,1), | |
| Nume NVARCHAR(100) NOT NULL, | |
| Email NVARCHAR(100) UNIQUE NOT NULL, | |
| Data_Inscriere DATE DEFAULT GETDATE(), | |
| Tip_Client NVARCHAR(50) DEFAULT 'Standard', | |
| Telefon VARCHAR(20), | |
| Data_Nasterii DATE, | |
| Activ BIT DEFAULT 1, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Interogare 1: SELECT simplu cu WHERE și ORDER BY | |
| SELECT Nume, Email, Tip_Client, Scor_Fidelitate | |
| FROM Clienti | |
| WHERE Scor_Fidelitate > 100 | |
| ORDER BY Scor_Fidelitate DESC; | |
| -- Interogare 2: SELECT cu JOIN (două tabele) și funcții de agregare | |
| SELECT c.Nume, COUNT(co.ID_Comanda) as Numar_Comenzi, SUM(co.Total) as Total_Cumparaturi | |
| FROM Clienti c | |
| LEFT JOIN Comenzi co ON c.ID_Client = co.ID_Client |