Suggestions on storing view meta in SQL Server 2008 - sql-server-2008

I want to store the view meta info like on which tables & columns & queries its created etc,
into another mapping table,so that I can reproduce them later point of time.
Right now I have a structure like this -
CREATE TABLE [dbo].[MAPPING_VIEW]
(
[ID] [int] NOT NULL,
[OLD_VIEW] [varchar](40) NULL,
[NEW_VIEW] [varchar](40) NULL,
CONSTRAINT [PK_MAPPING_VIEW] PRIMARY KEY CLUSTERED
([ID] ASC)
)
CREATE TABLE [dbo].[MAPPING_VIEW_TBL]
(
[ID] [int] NULL,
[PARENT_ID] [int] NULL,
[OLD_TBL] [varchar](40) NULL,
[NEW_TBL] [varchar](40) NULL
)
ALTER TABLE [dbo].[MAPPING_VIEW_TBL] WITH NOCHECK
ADD CONSTRAINT [FK_MAPPING_VIEW_TBL_ID]
FOREIGN KEY([PARENT_ID]) REFERENCES [dbo].[MAPPING_VIEW] ([ID])
CREATE TABLE [dbo].[MAPPING_VIEW_TBL_COL]
(
[ID] [int] NULL,
[PARENT_ID] [int] NULL,
[VIEW_ID] [int] NULL,
[OLD_COL] [varchar](40) NULL,
[NEW_COL] [varchar](40) NULL,
[OLD_ALIAS] [varchar](40) NULL,
[NEW_ALIAS] [varchar](40) NULL
)
ALTER TABLE [dbo].[MAPPING_VIEW_TBL_COL] WITH NOCHECK
ADD CONSTRAINT [FK_MAPPING_VIEW_TBL_COL_PARENT_ID]
FOREIGN KEY([PARENT_ID]) REFERENCES [dbo].[MAPPING_TBL] ([ID])
ALTER TABLE [dbo].[MAPPING_VIEW_TBL_COL] WITH NOCHECK
ADD CONSTRAINT [FK_MAPPING_VIEW_TBL_COL_VIEW_ID]
FOREIGN KEY([VIEW_ID]) REFERENCES [dbo].[MAPPING_VIEW] ([ID])
I know, which is not enough. Any suggestions would be appreciated

Related

Why I am having a this error on phpmyadmin: Error creating foreign key on revision (check data types)?

I´m trying to create Foreign Keys in phpmyadmin, but I get this error:
Error creating foreign key on revision (check data types)
I don´t understand it because the data types are equal. So, what I want is to create a Foreign Key from 'acoustictreatment' to 'filterspecifications' which contains tag, offerid and revision. But I get the error that I mentioned.
This are my tables:
CREATE TABLE `offer` (
`projectid` varchar(20) NOT NULL,
`customer` varchar(255) NOT NULL,
`creator` varchar(255) NOT NULL,
`date` date NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `offer`
ADD PRIMARY KEY (`projectid`,`revision`);
CREATE TABLE `filterspecifications` (
`tag` varchar(100) NOT NULL,
`gasFlow` double NOT NULL,
`dustToHandle` double NOT NULL,
`offerid` varchar(20) NOT NULL,
`selectedFilter` varchar(20) NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `filterspecifications`
ADD PRIMARY KEY (`tag`,`offerid`,`revision`),
ADD KEY `offerid` (`offerid`,`revision`);
ALTER TABLE `filterspecifications`
ADD CONSTRAINT `filterspecifications_ibfk_1`
FOREIGN KEY (`offerid`,`revision`) REFERENCES `offer` (`projectid`, `revision`) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE TABLE `acoustictreatment` (
`tag` varchar(100) NOT NULL,
`offerid` varchar(20) NOT NULL,
`outputFanSilencer` tinyint(1) NOT NULL,
`fanAcousticInsulation` tinyint(1) NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `acoustictreatment`
ADD PRIMARY KEY (`tag`,`offerid`,`revision`);
The solution that I found is to delete the 'acoustictreatment' table and create it again with the foreign key from the beginning. Because what I was trying to do was to create all the tables and then create the foreign keys, but that didn´t work for me

Cannot add foreign keys to mapping tables

i cannot create foreign keys in my mapping table for a m:n-relation.
My Problem:
I work with the newest version of Shopware (Shopware 6 ) on a test machine and I want to develop a plugin which creates a custom entity. the creation proccess of my custom entity (table name: car) and the table proccess with the shopware migration works without problems. At the next step, I have created a mapping table (name: car_product) which handles the m:n-relation with the standard shopware "product"-Table.
At this point I cannot add my foreign keys which pointed to the product-table. There is no error during the execution process. The foreign Key to the car-table works fine.
I guess, my schema is not correct or a problem with the product-table is happen..
CREATE TABLE IF NOT EXISTS car (
`id_car` BINARY(16) NOT NULL,
`hsn` VARCHAR(3) NOT NULL,
`tsn` VARCHAR(4) NOT NULL,
`manufacturer` VARCHAR(45) NULL,
`model` VARCHAR(45) NULL,
`year` INT NULL,
`created_at` DATETIME(3) NOT NULL,
`updated_at` DATETIME(3) NULL,
PRIMARY KEY (`id_car`),
UNIQUE (`hsn`, `tsn`));
CREATE TABLE IF NOT EXISTS car_product(
`product_id` BINARY(16) NOT NULL,
`product_version_id` BINARY(16) NOT NULL ,
`id_car` binary(16) Not Null ,
PRIMARY KEY (`product_id`, `product_version_id`, `id_car`),
INDEX `fk_product_car_idx` (`id_car` ) ,
INDEX `fk_product_car_product_idx` (`product_id` , `product_version_id`) );
CREATE TABLE product (
id binary(16) ,
version_id binary(16),
...
cars binary(16),
)
The id, version_id attributes inside the product table are both the primaray key
SQL-Query to add the foreign Keys :
Alter Table `car_product` ADD FOREIGN KEY (`product_id`,`product_version_id`) REFERENCES `product` (`id`, `version_id`) ON DELETE CASCADE ON UPDATE CASCADE ;
Alter Table `car_product` ADD FOREIGN KEY (`id_car` ) REFERENCES `car` (`id_car`) ON DELETE CASCADE ON UPDATE CASCADE;

I keep getting the error: There is already an object named fk_driver_id and i'm not sure how to fix this error. Help :(

If exists (select * from Information_schema.TABLES where
TABLE_NAME='fd_driver_vehicles')
Drop table fd_Driver_vehicles
GO
If exists (select * from Information_schema.TABLES where
TABLE_NAME='fd_vehicles')
Drop table fd_vehicles
GO
If exists (select * from Information_schema.TABLES where
TABLE_NAME='fd_driver_territories')
Drop table fd_driver_territories
GO
If exists (select * from Information_schema.TABLES where
TABLE_NAME='fd_sizes')
Drop table fd_sizes
GO
If exists (select * from Information_schema.TABLES where
TABLE_NAME='fd_drivers')
Drop table fd_drivers
GO
create table fd_drivers
(
driver_id int not null,
driver_lastname varchar(50) not null,
driver_firstname varchar(50) not null,
driver_charge money not null,
constraint pk_drivers primary key(driver_id),
constraint ck_driver_charge check (driver_charge >0)
)
create table fd_sizes
(
size_id char(1) not null,
size_charge money not null,
constraint pk_sizes primary key(size_id),
constraint ck_size_charge check (size_charge >0)
)
create table fd_driver_territories
(
driver_id int not null,
driver_territory varchar(20) not null,
constraint pk_driver_territories
primary key(driver_id, driver_territory),
constraint fk_driver_id foreign key (driver_id)
references fd_drivers (driver_id)
)
create table fd_vehicles
(
vehicle_lp varchar(12) not null,
vehicle_make varchar(50) not null,
vehicle_model varchar(50) not null,
vehicle_size char(1) not null,
constraint pk_vehicles
primary key(vehicle_lp),
constraint fk_vehicle_size foreign key (vehicle_size)
references fd_sizes (size_id)
)
create table fd_driver_vehicles
(
driver_id int not null,
vehicle_lp varchar(12) not null,
expiration_date datetime not null,
constraint pk_driver_vehicles
primary key (driver_id, vehicle_lp),
constraint fk_driver_id2 foreign key (driver_id)
references fd_drivers (driver_id),
constraint fk_vehicle_lp foreign key (vehicle_lp)
references fd_vehicles (vehicle_lp)
)
You are adding foreign key fk_driver_id which already exists so it is throwing error, delete first entry and then add new one.
Or Change name of it to Some other name.

Mysql: using two foreign keys to the same table

I'm using MySQL workbench to design a database. Server is mysql 5.5.6
I've defined a few foreign keys linking the "candidates" table to the "countries" table. I get this error:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'customer.candidats' (errno: 150)
The thing is: i'm referencing twice the countries table: once for the "nationality" column, once for the user address's country of origin. Is that allowed? Is this the right way to do it?
Here is the generated code that seems to trigger the issue.
CREATE TABLE IF NOT EXISTS `customer`.`candidats` (
`id` INT NOT NULL AUTO_INCREMENT,
`nom` VARCHAR(40) NULL,
`prenom` VARCHAR(40) NULL,
`qualite` ENUM('0001','0002') NULL COMMENT '0001 = Madame\n0002 = Monsieur',
`sexe` SET('1','2') NULL COMMENT '1 = Femme\n2 = Homme',
`date_de_naissance` DATE NULL,
`Nationalite` INT NOT NULL,
`selor_bilinguisme` TINYINT(1) NULL,
`rue` VARCHAR(60) NULL,
`numero` VARCHAR(10) NULL,
`pays` INT NOT NULL,
`region` INT NOT NULL,
`localité` VARCHAR(40) NULL,
`code_postal` VARCHAR(10) NULL,
`email` VARCHAR(241) NULL,
`tel_domicile` VARCHAR(30) NULL,
`tel_bureau` VARCHAR(30) NULL,
`tel_mobile` VARCHAR(30) NULL,
`tel_prefere` ENUM('01','02','03') NULL DEFAULT '03',
PRIMARY KEY (`id`),
INDEX `fk_candidats_pays_idx` (`Nationalite` ASC, `pays` ASC),
INDEX `fk_candidats_régions1_idx` (`region` ASC),
CONSTRAINT `fk_candidats_pays`
FOREIGN KEY (`Nationalite` , `pays`)
REFERENCES `customer`.`pays` (`id` , `id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_régions1`
FOREIGN KEY (`region`)
REFERENCES `customer`.`régions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
The "pays" table ("countries" in French)
CREATE TABLE IF NOT EXISTS `customer`.`pays` (
`id` INT NOT NULL AUTO_INCREMENT,
`nom_fr` VARCHAR(45) NULL,
`nom_nl` VARCHAR(45) NULL,
`nationalite_fr` VARCHAR(45) NULL,
`nationalite_nl` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB
Your schema generator doesn't work correctly.
It should generate:
CONSTRAINT `fk_candidats_pays`
FOREIGN KEY (`pays`)
REFERENCES `customer`.`pays` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_Nationalite`
FOREIGN KEY (`Nationalite`)
REFERENCES `customer`.`pays` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
To your other question: This type of referencing seems strange when you see it the first time, but it's quite normal and I think there is no other way of constructing this type of relationship.

It is not letting me drop a foreign key

I am trying to remove a foreign key from a table but I am getting this error below in mysql:
1025 - Error on rename of './mobile_app/Question' to './mobile_app/#sql2-4517-15515' (errno: 152)
What does this error mean and how can it be fixed?
Below is the code where I am trying to drop the foreign key:
alter table Question drop foreign key FK_Option_Table;
Below is the Question Table details (no rows in this table):
CREATE TABLE `Question` (
`SessionId` varchar(10) NOT NULL DEFAULT '',
`QuestionId` int(5) NOT NULL,
`QuestionContent` varchar(5000) NOT NULL,
`NoofAnswers` int(2) NOT NULL,
`AnswerId` int(10) NOT NULL AUTO_INCREMENT,
`ReplyId` varchar(2) NOT NULL,
`QuestionMarks` int(4) NOT NULL,
`OptionId` varchar(3) NOT NULL,
PRIMARY KEY (`SessionId`,`QuestionId`),
KEY `FK_Option_Table` (`OptionId`),
KEY `FK_IndividualQuestion` (`QuestionId`),
KEY `FK_Reply` (`ReplyId`),
KEY `FK_AnswerId` (`AnswerId`)
) ENGINE=InnoDB AUTO_INCREMENT=76 DEFAULT CHARSET=utf8
Below is the Option_Table details:
CREATE TABLE `Option_Table` (
`OptionId` varchar(3) NOT NULL,
`OptionType` varchar(20) NOT NULL,
PRIMARY KEY (`OptionId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
There are 24 rows in this table and below is a sample:
OptionId OptionType
O1 A-C
O2 A-D
O3 A-E
O4 A-F
...
Those are index's not foreign keys you have. I see no foreign keys in your create statements.
Example to create foreign key.
ALTER TABLE `Question`
ADD CONSTRAINT `SessionId`
FOREIGN KEY (`SessionId` )
REFERENCES `Option_Table` (`OptionId` )
ON DELETE NO ACTION
ON UPDATE CASCADE
, ADD INDEX `test_idx` (`SessionId` ASC) ;
ALTER TABLE `Question` DROP FOREIGN KEY `SessionId` ;