This is my SQLite file:
CREATE TABLE IF NOT EXISTS COTXE ( /*Table to create the cars*/
matricula VARCHAR(7) NOT NULL PRIMARY KEY, /*CarPlate*/
marca INTEGER, /*Brand*/
color INTEGER /*Color*/
);
CREATE TABLE IF NOT EXISTS PLASA ( /*Table to crate all the sites of the parking*/
pos INTEGER NOT NULL PRIMARY KEY, /*Number of the parkingsite*/
superficie INTEGER, /*How many m² it has*/
ocupada BOOL /*If its empty or not (0 = empty)*/
);
CREATE TABLE IF NOT EXISTS APARCAR ( /*Table to park a car in a site of the parking*/
pos_plaça INTEGER, /*Number of the parking site*/
matricula VARCHAR(7), /*Carplate*/
data_in DEFAULT CURRENT_TIMESTAMP, /*Date when it enters the parking*/
data_out DEFAULT NULL, /*Date when it leaves the parking*/
PRIMARY KEY (matricula, data_in),
FOREIGN KEY (pos_plaça) REFERENCES PLASA(pos),
FOREIGN KEY (matricula) REFERENCES COTXE(matricula)
);
/*Inserts to add cars and parking sites*/
INSERT INTO COTXE VALUES ('9882JPX', 0, 0);
INSERT INTO COTXE VALUES ('1111DDD', 4, 9);
INSERT INTO COTXE VALUES ('2222SSS', 8, 8);
INSERT INTO COTXE VALUES ('3333FFF', 3, 5);
INSERT INTO PLASA (superficie, ocupada) VALUES (50, 0);
INSERT INTO PLASA (superficie, ocupada) VALUES (34, 0);
INSERT INTO PLASA (superficie, ocupada) VALUES (24, 0);
I've got a little problem and its when i try to add, for example the first car two times in the same parking site without leaving in any of those it adds again the same car in the same parking site that should be impossible, i drop here an example:
sqlite> INSERT INTO APARCAR (pos_plaça, matricula) VALUES (1, '9882JPX');
sqlite> UPDATE PLASA SET ocupada = 1 WHERE pos = 1;
sqlite> INSERT INTO APARCAR (pos_plaça, matricula) VALUES (1, '9882JPX');
sqlite> SELECT * FROM APARCAR;
pos_plaça matricula data_in data_out
---------- ---------- ------------------- ----------
1 9882JPX 2022-03-17 17:41:24
1 9882JPX 2022-03-17 17:41:26
So i'm asking you guys if any of you know how to condition the INSERT INTO and before adding the car in that parking site, check if its full.
Sorry for my bad english and explanations.
Related
I have the following MARIADB code. It's supposed to demonstrate:
Constructing tables using sequences for incrementing the ID.
Using a temporary table+join to INSERT data into a table, while incrementing the ID.
Procedure:
Sequence S1 and table T1 are created. T1_ID is incremented with S1
Sequence S2 and table T2 are created. T2_ID is incremented with S2
Table T1 is filled with data. All is fine.
Temporary table TEMP_T2 is created and filled with data. No ID in this table. Column T1_NAME is a cross reference to SHORT_NAME in table T1.
The T1_ID is introduced into table TEMP_T2 with a join. The result of this SELECT is inserted into T2. Here, the sequence S2 should auto-increment T2_ID.
For some reason, at the end, T2 looks like this:
T2_ID|T1_ID|NAME|
-----+-----+----+
2| 1|y |
4| 2|x |
6| 2|z |
Why was T2_ID double-incremented?
Thanks!
USE DB1;
SET FOREIGN_KEY_CHECKS = 0;
DROP SEQUENCE IF EXISTS `S2`;
DROP SEQUENCE IF EXISTS `S1`;
DROP TABLE IF EXISTS `T2`;
DROP TABLE IF EXISTS `T1`;
-- Create sequence S1 and able T1
CREATE SEQUENCE `S1` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB;
SELECT SETVAL(`S1`, 1, 0);
CREATE TABLE `T1` (
`T1_ID` tinyint(4) NOT NULL DEFAULT nextval(`S1`),
`SHORT_NAME` varchar(10) NOT NULL,
PRIMARY KEY (`T1_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- Create sequence T2 and table T2
CREATE SEQUENCE `S2` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB;
SELECT SETVAL(`S2`, 1, 0);
CREATE TABLE `T2` (
`T2_ID` int(11) NOT NULL DEFAULT nextval(`S2`),
`T1_ID` int(11) DEFAULT NULL,
`NAME` varchar(100) DEFAULT NULL COLLATE 'utf8mb3_bin',
PRIMARY KEY (`T2_ID`),
UNIQUE KEY `T2_NAME_UN` (`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- Load data into T1
DELETE FROM T1;
INSERT INTO T1(SHORT_NAME) VALUES
('a'),
('b'),
('c');
SELECT * FROM T1;
-- Create temporary file for joining with T1
DROP TABLE IF EXISTS `TEMP_T2`;
CREATE TEMPORARY TABLE `TEMP_T2` (
`T1_NAME` varchar(10) DEFAULT NULL,
`NAME` varchar(100) DEFAULT NULL,
UNIQUE KEY `T2_NAME_UN` (`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
DELETE FROM TEMP_T2 ;
-- Insert data into the temporary table
INSERT INTO TEMP_T2(T1_NAME,NAME) VALUES
('b','x'),
('a','y'),
('b','z');
SELECT * FROM TEMP_T2;
# Do a join with TEMP_T2 x T1 and insert into T2
INSERT INTO T2(T1_ID,NAME)
SELECT
t1.T1_ID ,
t2.NAME
FROM TEMP_T2 AS t2
INNER JOIN T1 AS t1
ON t2.T1_NAME =t1.SHORT_NAME ;
SELECT * FROM T2;
Thanks for the responses.
I'm using SEQUENCE rather than AUTO_INCREMENT because I was told that it is the more modern way. It also enables retrieving the last ID of any specific table.
It's strange that this should be a bug. It seems like really basic functionality. But so it is...
I've found this as a reported existing bug MDEV-29540 in INSERT ... SELECT as it pertains to sequences in default values of columns.
Because this bug is reported and fix, this problem is/will not occur in the 10.3.37, 10.4.27, 10.5.18, 10.6.11, 10.7.7, 10.8.6, 10.9.4, 10.10.2, 10.11.1 and later versions.
I am trying to create a table in MariaDB with a virtual column, defined by a case statement
This is what I have
create table Foto (
ID int AUTO_INCREMENT not null primary key,
LigPlaatsCode varchar(10) not null,
FotoTypeID int not null check (FotoType in (0, 1, 2, 3)),
Foto varchar(1000) not null,
FotoType varchar(50) as
case FotoTypeID
when 0 then 'Bidprent'
when 1 then 'Krantartikel'
when 2 then 'Persoon'
else 'Graf'
end, `
constraint FK_Foto_LigPlaats foreign key (LigPlaatsCode) references LigPlaats (LigPlaatsCode)
)
however it always gives me this error
#42000You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'case FotoTypeID
when 0 then 'Bidprent'
when 1 then 'Krantar' at line 7
When I google on how to create virtual column and case, the links I found seem to suggest I got it right, but obviously I dont. So I am missing something.
What is wrong with this create table statement ?
EDIT
my version is 10.3.21-MariaDB
Generated column requires parantehese around case expression:
create table Foto (
ID int AUTO_INCREMENT not null primary key,
LigPlaatsCode varchar(10) not null,
FotoTypeID int not null ,
Foto varchar(1000) not null,
FotoType varchar(50) as
(case FotoTypeID
when 0 then 'Bidprent'
when 1 then 'Krantartikel'
when 2 then 'Persoon'
else 'Graf'
end), -- brackets around
-- constraint as separate entry because it is referencing FotoType
constraint fototypecheck check (FotoType in (0, 1, 2, 3))
);
db<>fiddle demo
This line does not make sense as FotoType is text:
FotoTypeID int not null check (FotoType in (0, 1, 2, 3)),
-- probably it should be
FotoTypeID int not null check (FotoTypeID in (0, 1, 2, 3)),
I have following table structure in SQLite database:
CREATE TABLE A (id integer primary key, name text, room Integer);
CREATE TABLE B (id integer primary key, idA integer not null, code blob, FOREIGN KEY(idA) REFERENCES A(id));
For each record in A there are 1 to n records in B that refers it. Desired table structure is:
CREATE TABLE A (id integer primary key, name text);
CREATE TABLE B (id integer primary key, idA integer not null, code blob, room Integer, FOREIGN KEY(idA) REFERENCES A(id));
So, I would like to transer room column from A to B without data loss: recreate table A without room column, delete duplicates from A, add room column to B, set it depends on what values were in referenced A records room columns (original A table) and reassign idA for B records.
Is it possible using only SQLite and if it is, how to do this, using only SQLite?
Thank you!
have a look at http://sqlfiddle.com/#!5/e26e2/1 .
creating the databases :
CREATE TABLE A (id integer primary key, name text, room Integer);
CREATE TABLE B (id integer primary key, idA integer not null, code blob, FOREIGN KEY(idA) REFERENCES A(id));
Fill in some test data :
insert into A (id, name, room) values (1, "azerty", 123) ;
insert into A (id, name, room) values (2, "querty", 456) ;
insert into B (id, idA, code) values (10, 1, "code 1") ;
insert into B (id, idA, code) values (15, 1, "code 1b") ;
insert into B (id, idA, code) values (20, 1, "code 1c") ;
insert into B (id, idA, code) values (25, 2, "code 2") ;
insert into B (id, idA, code) values (30, 3, "code 3") ;
copy the wrong table layouts to tables we will later drop :
alter table B rename to old_B ;
alter table A rename to old_A ;
Create tables according the correct layout :
CREATE TABLE A (id integer primary key, name text);
CREATE TABLE B (id integer primary key, idA integer not null, code blob, room Integer, FOREIGN KEY(idA) REFERENCES A(id));
Copy selected data from old_A into A :
INSERT OR REPLACE INTO A ( id, name)
SELECT id, name FROM old_A ;
fill B with an inner join to "line up" room with correct idA :
INSERT OR REPLACE INTO B ( id, idA, code, room)
SELECT old_B.*, old_A.room FROM old_B INNER JOIN old_A ON old_B.idA = old_A.id ;
drop the old tables
DROP TABLE old_A ;
DROP TABLE old_B ;
I am trying to Connect Three tables. Two of them have Primary Keys which the Third is supposed to link to. I need this inbetween as it is linked to a fourth (but this Works fine). The code I have written is as follows:
CREATE TABLE CUSTOMERS(
CUSTOMER_ID INT(10) NOT NULL,
SURNAME CHAR(50) NOT NULL,
NAME CHAR(50) NOT NULL,
PRIMARY KEY (CUSTOMER_ID)
);
CREATE TABLE WORKSHOP(
WORKSHOP_ID INT(10) NOT NULL,
NAME CHAR(100) NOT NULL,
CHAIN_NAME CHAR(100),
CHAIN_ID INT(10),
CONTRACT_WORKSHOP CHAR(5) NOT NULL,
PRIMARY KEY (WORKSHOP_ID, CHAIN_ID)
);
CREATE TABLE CAR_DAMAGE(
DAMAGE_ID INT(10) NOT NULL,
CUSTOMER_ID INT(10) NOT NULL,
DATE INT(20) NOT NULL,
PLACE CHAR(128) NOT NULL,
WORKSHOP_ID INT(10) NOT NULL,
PRIMARY KEY (DAMAGE_ID, CUSTOMER_ID, WORKSHOP_ID, DATE, PLACE),
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMERS (CUSTOMER_ID),
FOREIGN KEY (WORKSHOP_ID) REFERENCES WORKSHOP (WORKSHOP_ID)
);
INSERT INTO CUSTOMERS VALUES (1, "OLSEN", "TROND");
INSERT INTO CUSTOMERS VALUES (2, "JOHNSEN", "FELIX");
INSERT INTO CUSTOMERS VALUES (3, "SVINDAL", "AKSEL");
INSERT INTO CUSTOMERS VALUES (4, "BJORGEN", "MARIT");
INSERT INTO CUSTOMERS VALUES (5, "SVENDSON", "LISA");
INSERT INTO WORKSHOP VALUES (1, "BERTEL", "MOLLER", 1, "YES");
INSERT INTO WORKSHOP VALUES (2, "OLOF", "OLOF AUTO", 3, "NO");
INSERT INTO WORKSHOP VALUES (3, "J-AUTO", "MOLLER", 1, "YES");
INSERT INTO WORKSHOP VALUES (4, "SPEED", "BIRGER N. HAUG", 2, "YES");
INSERT INTO WORKSHOP VALUES (5, "RELAX AUTO", "MOLLER", 1, "YES");
INSERT INTO CAR_DAMAGE VALUES (1, 1, 10102008, "HELLERUD", 1);
INSERT INTO CAR_DAMAGE VALUES (2, 2, 14032015, "JAR", 2);
INSERT INTO CAR_DAMAGE VALUES (3, 3, 24052016, "LOMMEDALEN", 3);
INSERT INTO CAR_DAMAGE VALUES (4, 4, 31102017, "FLAKTVEIT", 4);
INSERT INTO CAR_DAMAGE VALUES (5, 5, 08062016, "STOCKHOLM", 5);
However, the problem occur as I get the error "foriegn key mismatch - CAR_DAMAGE referencing WORKSHOP.
I am using SQLite as I am forced to use it, given by my University.
Table WORKSHOP has a composite primary key (WORKSHOP_ID, CHAIN_ID). Any foreign key referencing that table must be a composite foreign key, consisting of the same two fields. Hence, you would need to add CHAIN_ID to table WORKSHOP and change your foreign key declaration to something like:
FOREIGN KEY (WORKSHOP_ID, CHAIN_ID) REFERENCES WORKSHOP (WORKSHOP_ID, CHAIN_ID)
[More generally, your primary keys seem, based on the information given, more complex than they need to be: why not just have WORKSHOP_ID as PK of WORKSHOP and DAMAGE_ID as PK of CAR_DAMAGE? But maybe you have good reasons.]
Thank you. This Method worked. Hence, when I proceeded, a New problem occured. The table CAR_DAMAGE is linked to a fourth table (Called DAMAGE_INFORMATION), With the code:
CREATE TABLE DAMAGE_INFORMATION(
DAMAGE_ID INT(10) NOT NULL,
DAMAGE_TYPE CHAR(100) NOT NULL,
DAMAGE_SIZE CHAR(50) NOT NULL,
SPEND INT(10) NOT NULL,
FOREIGN KEY (DAMAGE_ID) REFERENCES CAR_DAMAGE (DAMAGE_ID)
);
And I receive the same error as earlier, i.e. Foreign key mismatch "DAMAGE_INFORMATION" referencing "CAR_DAMAGE".
Is it not allowed to make a combination of 3 tables to 1, With different primary keys? The primary key of CAR_DAMAGE is:
PRIMARY KEY (DAMAGE_KEY, CUSTOMER_ID, WORKSHOP_ID)
ALTER PROCEDURE [dbo].[K_FS_InsertMrpDetails]
#date datetime,
#feedtype varchar(50),
#rateperkg float,
#rateper50kg float,
#updatedby varchar(50)
AS
BEGIN
INSERT INTO K_FS_FeedMrpDetails([date], feedtype, rateperkg, rateper50kg, updatedby, updatedon)
VALUES(#date, #feedtype, #rateperkg, #rateper50kg, #updatedby, getdate())
SELECT '1' AS status
END
With this query we insert 9 rows at a time but what I want is in one same date do not insert again different details. How can I please help me.
Add a unique constraint on the column [date]. That will prevent you from adding more than one row with the same [date] value.
Update:
To allow 9 rows for each date you can add a computed column D that removes the time part and you need to add a column that will hold the values 1 to 9 R. Use a check constraint on R to only allow 1-9. Finally you create a unique constraint on (R, D).
Sample table definition:
create table T
(
ID int identity primary key,
DT datetime not null,
R tinyint check (R in (1,2,3,4,5,6,7,8,9)) not null,
D as dateadd(day, datediff(day, 0, DT), 0),
constraint ux_RD unique (R,D)
)
Try with this:
insert into T(DT, R) values(getdate(), 1)
insert into T(DT, R) values(getdate(), 2)
insert into T(DT, R) values(getdate(), 1)
First and second insert works fine, the third raises a unique constraint exception.