1 Exercices sur SQL server 2000 La diagramme de classe : Exercices sur SQL server 2000 Le modèle relationnel correspondant :
1
Exercices sur SQL server 2000La diagramme de classe :
Exercices sur SQL server 2000Le modèle relationnel correspondant :
2
Créer les tables
Clic-droit on Tables et choisir « New Table »Clic-droit on Tables et choisir « New Table »
Créer la table ‘Client’
Pour clé primaire mettre « Identity » à yesPour clé primaire mettre « Identity » à yes
Mettre N_client en clé primaireMettre N_client en clé primaire
4
Créer la table ‘Produit’
Data types
5 through 17 bytes, depending on precision
Fixed-precision and fixed-scale numbers. (The data type numeric is a synonym for decimal.)Precision (p) specifies the total number of digits that can be stored, both to the left and to the right of the decimal point. Scale (s) specifies the maximum number of digits that can be stored to the right of the decimal point. Scale must be less than or equal to precision. The minimum precision is 1, and the maximum precision is 28 unless SQL Server is started with the -p parameter, in which case, precision can be up to 38.
decimal[(p,[s])] or numeric[(p,[s])]
8 bytesDate and time data from January 1, 1753 through
December 31, 9999, with accuracy to 3.33 milliseconds
datetime
Not applicableA reference to a cursor. Can be used only for variables and stored procedure parameterscursor
n bytesFixed-length non-Unicode character data with length of
n characters, where n is a value from 1 through 8000
char[(n)]
1 byte for a table with up to 8-bit columns, 2 bytes for a table with 9-bit through 16-bit columns, and so on
Integer data type that can be a value of 1, 0, or NULL. Bit columns cannot have indexes on thembit
n + 4 bytes
Fixed-length binary data of n bytes, where n is a value from 1 through 8000. Use binary when data entries in a column are expected to be close to the same size.
binary[(n)]
8 bytesAn 8-byte integer (whole number).bigint
Storage sizeDescriptionData type
5
16 bytes for the pointer and 2 bytes * the number of characters entered for the data
Variable-length Unicode data with a maximum length of 2^30 - 1 (1,073,741,823) characters.The column entry for ntextis a pointer to the location of the data. The data is stored separately from the table data
ntext
2 bytes * the number of characters entered
Fixed-length Unicode character data of n characters, where n is a value from 1 through 4000.Unicode characters use 2 bytes per character and can support all international characters-
nchar[(n)]
8 bytesMonetary data values from -2^63 (-922,337,203,685,477.5808)
through 2^63 - 1 (922,337,203,685,477.5807), with accuracy to one ten-thousandth of a monetary unit
money
4 bytesInteger (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647)
integer or int
16 bytes for the pointer
Used for variable-length binary data longer than 8000 bytes, with a maximum of 2^31 - 1 bytes. An image column entry is a pointer to the location of the image data value. The data is stored separately from the table data
image
4 through 8 bytes, depending on precision
Floating-precision numerical data that can range from -1.79E +308 through 1.79E +308. The value n is the number of bits used to store the mantissa of the float number and can range from 1 to 53
float[(n)]
Varies with table definitionSimilar to using a temporary table—the declaration includes a column list and
data types. Can be used to define a local variable or for the return value of a user-defined function.
table
256 bytes
A special, system-supplied, SQL Server user-defined data type. The sysnamedata type is defined by SQL Server as nvarchar(128), which means that it can contain 128 Unicode characters (or 256 bytes). Use sysname to refer to columns that store object names
sysname
Size variesAllows values of different data types. The data value and data describing that
value—its base data type, scale, precision, maximum size, and collation are stored in this column
sql_variant
4 bytesMonetary data values from -214,748.3648 through 214,748.3647, with accuracy to one ten-thousandth of a monetary unit smallmoney
2 bytesInteger data from -2^15 (-32,768) through 2^15 - 1 (32,767)smallint
4 bytesDate and time data from January 1, 1900 through June 6, 2079, with accuracy to the minute (less precise than the datetime data type)smalldatetime
4 bytesFloating-precision numerical data that can range from ?3.40E+38 through 3.40E+38. The synonym for real is float(24)real
2 bytes * the number of characters enteredVariable-length Unicode data of n characters, where n is a value from 1 through
4000. Recall that Unicode characters use 2 bytes per character and can support all international characters.
nvarchar
6
Actual length of data enteredVariable-length non-Unicode character data with a length of n characters, where n is a value from 1 through 8000. varchar[(n)]
Actual length of data entered + 4 bytesVariable-length binary data of n bytes, where n is a value from 1 through 8000. Use varbinary when data entries in a column are expected to vary considerablyin size.
varbinary
16 bytesStores a 16-byte binary value that is a globally unique identifier (GUID)unique-identifier
1 byteInteger data from 0 through 255.tinyint
8 bytesA timestamp column is automatically updated every time a row is inserted orupdated. Each table can have only one timestamp column. timestamp
16 bytes for the pointerUsed for variable-length non-Unicode character data longer than 8000 bytes. A text column entry can hold up to 2^31 - 1 characters. It is a pointer to the location of the data value. The data is stored separately from the table data
text
Créer un nouveau diagramme
7
Ajouter les tables avec wizard
Ajouter les liens entre les tables
Tirer un lien de Client.N_clientVers Commande.N_Client
Tirer un lien de Client.N_clientVers Commande.N_Client
8
Ajouter les liens entre les tables
Ajouter les liens entre les tables
Client.N_clientCommande.N_Client
Commande.N_CommandeConstitution.N_Commande
Produit.N_ProduitConstitution.N_Produit
Client.N_clientCommande.N_Client
Commande.N_CommandeConstitution.N_Commande
Produit.N_ProduitConstitution.N_Produit
9
Ajouter des données aux tables
Clic-droit sur la table et choisir « Open table » puis « Return all rows »Clic-droit sur la table et choisir « Open table » puis « Return all rows »
Ajouter des données aux tables
Ajouter les données à la dernière ligne de la table, Le champs N_client se complète automatiquement car
« Identity » est mis à yes
Ajouter les données à la dernière ligne de la table, Le champs N_client se complète automatiquement car
« Identity » est mis à yes
On peut aussi modifier les données déjà entréesOn peut aussi modifier les données déjà entrées
10
Ajouter des données aux tables
Attention à la contrainte d’intégrité existentiellePour les clés étrangères
Attention à la contrainte d’intégrité existentiellePour les clés étrangères
Ajouter des données aux tablesClientClient CommandeCommande
ProduitProduit ConstitutionConstitution
11
Créer un nouveau « view »
Créer un nouveau « view »
Clic-droit et choisir « add table »Clic-droit et choisir « add table »
12
Créer un nouveau « view »
Ajouter la table clientAjouter la table client
Créer un nouveau « view »
Cliquer sur « all columns » puis sur runCliquer sur « all columns » puis sur run
13
Créer un nouveau « view »
Choisir N_client, Nom_Client et Adresse. Décocher « output » d’adresse.
Ajouter « =‘Bruxelles’ » dans criteria
Choisir N_client, Nom_Client et Adresse. Décocher « output » d’adresse.
Ajouter « =‘Bruxelles’ » dans criteria
Créer un nouveau « view »
Créer un nouveau « view »Ajouter tous les tables qu’on a créé
Créer un nouveau « view »Ajouter tous les tables qu’on a créé
14
Créer un nouveau « view »
Construire une vue qui renvoie le nom et adresse du client, les produits qu’il a acheté, la date de la
Commande et la quantité acheté
Construire une vue qui renvoie le nom et adresse du client, les produits qu’il a acheté, la date de la
Commande et la quantité acheté
Créer un nouveau « view »
15
Créer un nouveau « view »
Quels sont les clients (nom et adresse) qui n’ont pas fait d’achat en octobre
SELECT Client.NomClient, Client.AdresseFROM Client, CommandeWHERE (Client.N_Client = Commande.N_Client) And
(Commande.DateCommande NOT LIKE ‘__/10/__’);
Créer un nouveau « view »
Deux lignes identiques !Deux lignes identiques !