MONDE-HISTOIRE-CULTURE GÉNÉRALE
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
MONDE-HISTOIRE-CULTURE GÉNÉRALE

Vues Du Monde : ce Forum MONDE-HISTOIRE-CULTURE GÉNÉRALE est lieu d'échange, d'apprentissage et d'ouverture sur le monde.IL EXISTE MILLE MANIÈRES DE MENTIR, MAIS UNE SEULE DE DIRE LA VÉRITÉ.
 
AccueilAccueil  PortailPortail  GalerieGalerie  RechercherRechercher  Dernières imagesDernières images  S'enregistrerS'enregistrer  Connexion  
Derniers sujets
Marque-page social
Marque-page social reddit      

Conservez et partagez l'adresse de MONDE-HISTOIRE-CULTURE GÉNÉRALE sur votre site de social bookmarking
QUOI DE NEUF SUR NOTRE PLANETE
LA FRANCE NON RECONNAISSANTE
Ephémerides
-20%
Le deal à ne pas rater :
Ecran PC GIGABYTE 28″ LED M28U 4K ( IPS, 1 ms, 144 Hz, FreeSync ...
399 € 499 €
Voir le deal

 

 SQL Basics :INSERT INTO

Aller en bas 
AuteurMessage
mihou
Rang: Administrateur
mihou


Nombre de messages : 8092
Localisation : Washington D.C.
Date d'inscription : 28/05/2005

SQL Basics :INSERT INTO Empty
03022006
MessageSQL Basics :INSERT INTO

The INSERT INTO Statement

The INSERT INTO statement is used to insert new rows into a table.
Syntax
In the previous sections, we have seen how to retrieve information from tables. But how do these rows of data get into these tables in the first place? This is what this section, covering the INSERT statement, and next section, covering tbe UPDATE statement, are about.

There are essentially basically two ways to INSERT data into a table. One is to insert it one row at a time, and the other is to insert multiple rows at a time. Let's first look at how we may INSERT data one row at a time:

The syntax for inserting data into a table one row at a time is as follows:

INSERT INTO "table_name" ("column1", "column2", ...)
VALUES ("value1", "value2", ...)

Assuming that we have a table that has the following structure,

Table Store_Information

Column Name Data Type
store_name char(50)
Sales float
Date datetime

and now we wish to insert one additional row into the table representing the sales data for Los Angeles on January 10, 1999. On that day, this store had $900 in sales. We will hence use the following SQL script:

INSERT INTO Store_Information (store_name, Sales, Date)
VALUES ('Los Angeles', 900, 'Jan-10-1999')

The second type of INSERT INTO allows us to insert multiple rows into a table. Unlike the previous example, where we insert a single row by specifying its values for all columns, we now use a SELECT statement to specify the data that we want to insert into the table. If you are thinking whether this means that you are using information from another table, you are correct. The syntax is as follows:

INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2"

Note that this is the simplest form. The entire statement can easily contain WHERE, GROUP BY, and HAVING clauses, as well as including table joins and aliases.

So for example, if we wish to have a table, Store_Information, that collects the sales information for year 1998, and you already know that the source data resides in the Sales_Information table, you'll type in:

INSERT INTO Store_Information (store_name, Sales, Date)
SELECT store_name, Sales, Date
FROM Sales_Information
WHERE Year(Date) = 1998

Here I have used the SQL Server syntax to extract the year information out of a date. Other relational databases will have different syntax. For example, in Oracle, you will use WHERE to_char(date,'yyyy')=1998.
INSERT INTO table_name
VALUES (value1, value2,....)

You can also specify the columns for which you want to insert data:

INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)

Insert a New Row

This "Persons" table:
LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger

And this SQL statement:

INSERT INTO Persons
VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes')

Will give this result:
LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes

Insert Data in Specified Columns

This "Persons" table:
LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes

And This SQL statement:

INSERT INTO Persons (LastName, Address)
VALUES ('Rasmussen', 'Storgt 67')

Will give this result:
LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes
Rasmussen Storgt 67
Revenir en haut Aller en bas
https://vuesdumonde.forumactif.com/
Partager cet article sur : reddit

SQL Basics :INSERT INTO :: Commentaires

Aucun commentaire.
 

SQL Basics :INSERT INTO

Revenir en haut 

Page 1 sur 1

Permission de ce forum:Vous ne pouvez pas répondre aux sujets dans ce forum
MONDE-HISTOIRE-CULTURE GÉNÉRALE :: SCIENCES :: INFORMATIQUE-INTERNET/COMPUTER SCIENCES-
Sauter vers: