SQL handleiding

SQL TUIS SQL Inleiding SQL-sintaksis SQL Kies SQL Kies Distinct SQL Waar SQL En, of, nie SQL Bestel deur SQL Voeg in SQL nulwaardes SQL-opdatering SQL verwyder SQL Kies Top SQL Min en Max SQL-telling, gemiddeld, som SQL Soos SQL Wildcards SQL in SQL Tussen SQL aliasse SQL sluit aan SQL Inner Sluit aan SQL Links Sluit aan SQL Reg Sluit aan SQL Volledige Aansluiting SQL Self Sluit aan SQL Unie SQL Groep deur SQL het SQL bestaan SQL Enige, Almal SQL Kies In SQL Voeg in Kies SQL-geval SQL nul-funksies SQL gestoorde prosedures SQL opmerkings SQL-operateurs

SQL- databasis

SQL Skep DB SQL Drop DB SQL Backup DB SQL Skep tabel SQL Drop Tabel SQL Verander Tabel SQL-beperkings SQL nie nul nie SQL Uniek SQL Primêre Sleutel SQL buitelandse sleutel SQL Check SQL verstek SQL-indeks SQL Outo-verhoging SQL datums SQL-aansigte SQL-inspuiting SQL Hosting SQL-datatipes

SQL- verwysings

SQL sleutelwoorde MySQL-funksies SQL Server funksies MS Access funksies SQL Vinnige Verw

SQL voorbeelde

SQL voorbeelde SQL vasvra SQL-oefeninge SQL-sertifikaat

SQL INSERT INTO SELECT Statement


Die SQL INSERT INTO SELECT-stelling

Die INSERT INTO SELECTstelling kopieer data uit een tabel en voeg dit in 'n ander tabel in.

Die INSERT INTO SELECTstelling vereis dat die datatipes in bron- en teikentabelle ooreenstem.

Let wel: Die bestaande rekords in die teikentabel word nie aangetas nie.

VOEG IN IN SELECT-sintaksis

Kopieer alle kolomme van een tabel na 'n ander tabel:

INSERT INTO table2
SELECT * FROM table1
WHERE condition;

Kopieer slegs sommige kolomme van een tabel na 'n ander tabel:

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

Demo-databasis

In hierdie tutoriaal sal ons die bekende Northwind-voorbeelddatabasis gebruik.

Hieronder is 'n keuse uit die "Klante"-tabel:

CustomerID CustomerName ContactName Address City PostalCode Country
1

Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico

En 'n keuse uit die "Verskaffers"-tabel:

SupplierID SupplierName ContactName Address City Postal Code Country
1 Exotic Liquid Charlotte Cooper 49 Gilbert St. Londona EC1 4SD UK
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117 USA
3 Grandma Kelly's Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA


SQL INSERT IN TO SELECT Voorbeelde

Die volgende SQL-stelling kopieer "Verskaffers" na "Klante" (die kolomme wat nie met data gevul is nie, sal NULL bevat):

Voorbeeld

INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers;

Die volgende SQL-stelling kopieer "Verskaffers" na "Klante" (vul alle kolomme in):

Voorbeeld

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
SELECT SupplierName, ContactName, Address, City, PostalCode, Country FROM Suppliers;

Die volgende SQL-stelling kopieer slegs die Duitse verskaffers na "Klante":

Voorbeeld

INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers
WHERE Country='Germany';