ASIX/M10/UF1/A04/E1

De Lordwektabyte Wiki
Salta a la navegació Salta a la cerca

ASIX M10 UF1

Pt41. Pràctica d'Identificació de variables, constants i operadors sobre un codi, correcció d'errades al codi

Guillem Solà i Boeck (host: PC05)

Enunciat

SET db="world2";
SELECT CONCAT("BASE DE DADES ",@db) "";
 
SET ins=CONCAT("CREATE OR REPLACE DATABASE ",@db);
PREPARE @ins FROM ins;
EXECUTE ins;
 
USE world2;
 
SET @taula="City";
CREATE TABLE @taula (
  ID int(11) NOT NULL AUTO_INCREMENT,
  Name char(35) NOT NULL DEFAULT '',
  CountryCode char(3) NOT NULL DEFAULT '',
  District char(20) NOT NULL DEFAULT '',
  Population int(11) NOT NULL DEFAUL '0',
  PRIMARY KEY (ID),
  KEY CountryCode (CountryCode),
  CONSTRAINT city_ibfk_1 FOREIGN (CountryCode) REFERENCES Country (Code)
);
SELECT CONCAT("Taula ",taula," creada") AS "";
INSERT INTO City (1,'Kabul','AFG','Kabol',1780000);
SELECT COMCAT("Dades de ",@taula," importades") as "";
 
SELECT "Fi de la importació" as '';

Solució

Variables: @db, @ins, @taula Literals: "world2", "City"

SET @db="world2";
SELECT CONCAT("BASE DE DADES ",@db);
 
SET @ins=CONCAT("CREATE OR REPLACE DATABASE ",@db);
PREPARE ins FROM @ins;
EXECUTE ins;
 
USE world2;
 
SET @taula="City";
CREATE TABLE City (
  ID int(11) NOT NULL AUTO_INCREMENT,
  Name char(35) NOT NULL DEFAULT '',
  CountryCode char(3) NOT NULL DEFAULT '',
  District char(20) NOT NULL DEFAULT '',
  Population int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (ID),
  KEY CountryCode (CountryCode)
);
SELECT CONCAT("Taula ",@taula," creada") AS '';
INSERT INTO City VALUES (1,'Kabul','AFG','Kabol',1780000);
SELECT CONCAT("Dades de ",@taula," importades") as '';
 
SELECT "Fi de la importació" as '';