SQL -zelfstudie

SQL HOME SQL-intro SQL-syntaxis SQL-selectie SQL Select Distinct SQL waar SQL en, of, niet SQL-bestelling op SQL invoegen in SQL-nulwaarden SQL-update SQL verwijderen SQL Selecteer Top SQL Min en Max Aantal SQL, Gem, Som SQL Vind ik leuk SQL-jokertekens SQL In SQL Tussen SQL-aliassen SQL-joins SQL Inner Join SQL Left Join SQL juiste join Volledige deelname aan SQL Zelf lid worden van SQL SQL-unie SQL Groeperen op SQL hebben SQL bestaat SQL Alle, Alle SQL Selecteren in SQL invoegen in selecteren SQL-geval SQL Null-functies Op SQL opgeslagen procedures SQL-opmerkingen SQL-operators

SQL- database

SQL creëren DB SQL Drop-DB SQL back-up DB SQL-tabel maken SQL-droptabel SQL-wijzigingstabel SQL-beperkingen SQL niet null SQL Uniek Primaire SQL-sleutel SQL externe sleutel SQL-controle SQL-standaard SQL-index Automatische SQL-verhoging SQL-datums SQL-weergaven SQL injectie SQL-hosting SQL-gegevenstypen

SQL- verwijzingen

SQL-trefwoorden MySQL-functies SQL Server-functies MS Access-functies Snelle SQL-referentie

SQL- voorbeelden

SQL-voorbeelden SQL-quiz SQL-oefeningen SQL-certificaat

SQL INSERT INTO SELECT- instructie


De SQL INSERT INTO SELECT-instructie

De INSERT INTO SELECTinstructie kopieert gegevens uit de ene tabel en voegt deze in een andere tabel in.

De INSERT INTO SELECTinstructie vereist dat de gegevenstypen in bron- en doeltabellen overeenkomen.

Opmerking: De bestaande records in de doeltabel worden niet beïnvloed.

INSERT INTO SELECT Syntaxis

Kopieer alle kolommen van de ene tabel naar de andere tabel:

INSERT INTO table2
SELECT * FROM table1
WHERE condition;

Kopieer slechts enkele kolommen van de ene tabel naar een andere tabel:

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

Demodatabase

In deze tutorial gebruiken we de bekende Northwind-voorbeelddatabase.

Hieronder vindt u een selectie uit de tabel "Klanten":

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 een greep uit de tabel "Leveranciers":

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 INTO SELECT Voorbeelden

De volgende SQL-instructie kopieert "Leveranciers" naar "Klanten" (de kolommen die niet met gegevens zijn gevuld, bevatten NULL):

Voorbeeld

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

De volgende SQL-instructie kopieert "Leveranciers" naar "Klanten" (vul alle kolommen in):

Voorbeeld

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

De volgende SQL-instructie kopieert alleen de Duitse leveranciers naar "Klanten":

Voorbeeld

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