Create MySQL schema - mariadb

I have a problem.
I created a MySQL schema with the MySQL Workbench.
But there is a error message.
A comma or closing bracket was expected. (near "VISIBLE" at position 288)
Unexpected start of statement. (near "`ShopID`" at position 330)
Unrecognised statement type. (near "ASC" at position 339)
As I said I created the scheme via the MySQL workbench. But what is wrong ?
Note: PK = Primary Key; FK = Foreign Key.
Fact table:
Contaminants (TimeID (PK/FK), ShopID (PK/FK), FoodID (PK/FK), substance, quantityPerOunce)
Dimension tables:
Time (TimeID (PK), dayNr, dayName, weekNr, monthNr, year)
Food (FoodID (PK), foodName, brand, foodType)
Place (ShopID (PK), name, street, region, country)
CREATE TABLE IF NOT EXISTS `mydb`.`Contaminants` (
`TimeID` INT NOT NULL,
`ShopID` INT NOT NULL,
`FoodID` INT NOT NULL,
`substance` VARCHAR(45) NULL,
`quantityPerOunce` INT NULL,
PRIMARY KEY (`TimeID`, `ShopID`, `FoodID`),
UNIQUE INDEX `TimeID_UNIQUE` (`TimeID` ASC) VISIBLE,
UNIQUE INDEX `ShopID_UNIQUE` (`ShopID` ASC) VISIBLE,
UNIQUE INDEX `FoodID_UNIQUE` (`FoodID` ASC) VISIBLE,
CONSTRAINT `TimeID`
FOREIGN KEY (`TimeID`)
REFERENCES `mydb`.`Time` (`TimeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ShopID`
FOREIGN KEY (`ShopID`)
REFERENCES `mydb`.`Shop` (`ShopID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FoodID`
FOREIGN KEY (`FoodID`)
REFERENCES `mydb`.`Food` (`FoodID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`Food` (
`FoodID` INT NOT NULL,
`foodName` VARCHAR(45) NULL,
`brand` VARCHAR(45) NULL,
`foodType` VARCHAR(45) NULL,
PRIMARY KEY (`FoodID`))
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`Shop` (
`ShopID` INT NOT NULL,
`INT` VARCHAR(45) NULL,
`street` VARCHAR(45) NULL,
`region` VARCHAR(45) NULL,
`country` VARCHAR(45) NULL,
PRIMARY KEY (`ShopID`))
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`Time` (
`TimeID` INT NOT NULL,
`dayNr` INT NULL,
`dayName` VARCHAR(45) NULL,
`weekNr` INT NULL,
`monthNr` VARCHAR(45) NULL,
`year` INT NULL,
PRIMARY KEY (`TimeID`))
ENGINE = InnoDB

Related

Syntax Error when using the source command in MySql

I have some tables that will not be made when using the source command and I can't seem to find out what is wrong with them
I used MariaDB [cpt238]> source E:\database\create.sql
and got this
MariaDB [cpt238]> source E:\database\create.sql
Query OK, 0 rows affected (0.028 sec)
Query OK, 0 rows affected (0.017 sec)
Query OK, 0 rows affected (0.012 sec)
Query OK, 0 rows affected (0.013 sec)
Query OK, 0 rows affected (0.014 sec)
Query OK, 0 rows affected (0.017 sec)
Query OK, 0 rows affected (0.017 sec)
Query OK, 0 rows affected (0.023 sec)
Query OK, 0 rows affected (0.015 sec)
Query OK, 0 rows affected (0.012 sec)
Query OK, 0 rows affected (0.016 sec)
Query OK, 0 rows affected (0.016 sec)
Query OK, 0 rows affected (0.014 sec)
Query OK, 0 rows affected (0.016 sec)
Query OK, 0 rows affected (0.017 sec)
Query OK, 0 rows affected (0.007 sec)
Records: 0 Duplicates: 0 Warnings: 0
ERROR 1064 (42000) at line 122 in file: 'E:\database\create.sql': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'create table Credential(
CRD_ID int not null auto_increment,
CRD_Title varcha...' at line 5
Query OK, 0 rows affected (0.014 sec)
Query OK, 0 rows affected (0.017 sec)
ERROR 1005 (HY000) at line 156 in file: 'E:\database\create.sql': Can't create table cpt238.student (errno: 150 "Foreign key constraint is incorrectly formed")
ERROR 1005 (HY000) at line 164 in file: 'E:\database\create.sql': Can't create table cpt238.graduation (errno: 150 "Foreign key constraint is incorrectly formed")
ERROR 1005 (HY000) at line 174 in file: 'E:\database\create.sql': Can't create table cpt238.requirement (errno: 150 "Foreign key constraint is incorrectly formed")
Query OK, 0 rows affected (0.021 sec)
Query OK, 0 rows affected (0.020 sec)
ERROR 1005 (HY000) at line 208 in file: 'E:\database\create.sql': Can't create table cpt238.fulfillment (errno: 150 "Foreign key constraint is incorrectly formed")
Query OK, 0 rows affected (0.018 sec)
This is the source file:
create table State(
Sta_Abbr char(2),
Sta_Name varchar(25) not null,
primary key (Sta_Abbr)
);
create table Zipcode(
Zip_Zipcode varchar(10) not null,
Zip_City varchar(25) not null,
Sta_Abbr char(2) not null,
primary key (Zip_Zipcode),
foreign key (Sta_Abbr) references State(Sta_Abbr)
);
create table Gender(
Gen_Gender char(1) not null,
Gen_Desc varchar(6) not null,
primary key (Gen_Gender)
);
create table Person(
Per_ID int not null auto_increment,
Per_FirstName varchar(15) not null,
Per_LastName varchar(15) not null,
Gen_Gender char(1) not null,
Per_DOB int not null,
primary key (Per_ID),
foreign key (Gen_Gender) references Gender(Gen_Gender)
);
create table ConType (
CT_ID int not null auto_increment,
CT_Desc varchar(20) not null,
primary key (CT_ID)
);
create table Phone(
Pho_ID int not null auto_increment,
Pho_Area int(3),
Pho_Exchange int(3) not null,
Pho_Extension int(4) not null,
CT_ID int not null,
primary key (pho_id),
foreign key (CT_ID) references ConType(CT_ID)
);
create table Email(
Mail_ID int not null auto_increment,
Mail_Domain varchar(10) not null,
Mail_UserName varchar(20) not null,
CT_ID int not null,
primary key (Mail_ID),
foreign key (CT_ID) references ConType(CT_ID)
);
create table Address(
Add_ID int not null auto_increment,
Sta_Abbr char(2) not null,
Zip_ZipCode varchar(15) not null,
CT_ID int not null,
primary key (Add_ID),
foreign key (Sta_Abbr) references State(Sta_Abbr),
foreign key (Zip_ZipCode) references Zipcode(Zip_Zipcode),
foreign key (CT_ID) references ConType(CT_ID)
);
create table DegType (
DT_ID int not null auto_increment,
DT_Title varchar(15) not null,
DT_Level int not null,
DT_Abbr char(4),
primary key (DT_ID)
);
create table Grade (
Grd_Sym char(2) not null,
Grd_Desc varchar(20) not null,
Grd_Credit boolean not null,
Grd_Pts int not null,
Grd_GPA boolean not null,
primary key (Grd_Sym)
);
create table Term(
Trm_Num int(2) not null,
Trm_Desc varchar(10) not null,
Trm_Length int not null,
primary key (Trm_Num)
);
create table Year(
Yr_Year int(4) not null,
primary key (Yr_Year)
);
create table Semester(
Sem_ID int not null auto_increment,
Trm_Num int(2) not null,
Yr_Year int(4) not null,
Sem_Start Date not null,
Sem_End Date not null,
primary key (Sem_ID),
foreign key (Yr_Year) references Year(Yr_Year),
foreign key (Trm_Num) references Term(Trm_Num)
);
create table Department(
Dpt_ID int not null auto_increment,
Dpt_Name varchar(35) not null,
primary key (Dpt_ID)
);
create table Faculty(
Per_ID int(4) not null,
Dpt_ID int(4) not null,
Primary key (Per_ID),
foreign key (Per_ID) references Person(Per_ID),
foreign key (Dpt_ID) references Department(Dpt_ID)
);
Alter table Department add Per_ID int not null;
Alter table Department add foreign key (Per_ID) references Faculty(Per_ID);
create table Credential(
CRD_ID int not null auto_increment,
CRD_Title varchar(25) not null,
DT_ID int not null,
CRD_Yr int(4) not null,
DPT_ID int not null,
primary key (CRD_ID),
foreign key (DT_ID) references DegType(DT_ID),
foreign key (Dpt_ID) references Department(Dpt_ID)
);
create table Subject(
Sub_Abbr char(3) not null,
Sub_Name varchar(25) not null,
DPT_ID int not null,
primary key (Sub_Abbr),
foreign key (DPT_ID) references Department(DPT_ID)
);
create table Course(
Crs_ID int not null auto_increment,
Crs_Num int(3) not null,
Sub_Abbr char(3) not null,
Crs_Desc varchar(200),
Crs_Title varchar(25) not null,
Crs_Hrs int(2) not null,
primary key (Crs_ID),
foreign key (Sub_Abbr) references Subject(Sub_Abbr)
);
create table Student(
Per_ID int not null,
CRD_ID int not null,
primary key (Per_ID),
foreign key (Per_ID) references Person(Per_ID),
foreign key (CRD_ID) references Credential(CRD_ID)
);
create table Graduation(
Grad_ID int not null auto_increment,
Per_ID int not null,
CRD_ID int not null,
Grad_Date Date not null,
primary key (Grad_ID),
foreign key (Per_ID) references Student(Per_ID),
foreign key (CRD_ID) references Credential(CRD_ID)
);
create table Requirement(
Req_ID int not null auto_increment,
CRD_ID int not null,
Crs_ID int not null,
primary key (Req_ID),
foreign key (CRD_ID) references Credential(CRD_ID),
foreign key (Crs_ID) references Course(Crs_ID)
);
create table Section(
Sec_ID int not null auto_increment,
Sec_Num int(3) not null,
Crs_ID int(3) not null,
Per_ID int not null,
Trm_Num int(2) not null,
Yr_Year int(4) not null,
primary key (Sec_ID),
foreign key (Crs_ID) references Course(Crs_ID),
foreign key (Per_ID) references Faculty(Per_ID),
foreign key (Trm_Num) references Term(Trm_Num),
foreign key (Yr_Year) references Year(Yr_Year)
);
create table Completion(
Cmp_ID int not null auto_increment,
Per_ID int not null,
Sec_ID int not null,
Grd_Sym char(2) not null,
primary key (Cmp_ID),
foreign key (Per_ID) references Person (Per_ID),
foreign key (Sec_ID) references Section (Sec_ID),
foreign key (Grd_Sym) references Grade (Grd_Sym)
);
create table Fulfillment(
Ful_ID int not null auto_increment,
Req_ID int not null,
Cmp_ID int not null,
primary key (Ful_ID),
foreign key (Req_ID) references Requirement(Req_ID),
foreign key (Cmp_ID) references Completion(Cmp_ID)
);
create table PreReq(
Prq_ID int not null auto_increment,
Crs_ID int(3) not null,
Prq_Crs_ID int(3) not null,
primary key (Prq_ID),
foreign key (Crs_ID) references Course(Crs_ID),
foreign key (Prq_Crs_ID) references Course(Crs_ID)
);

Cannot restart Strapi after making changes to Content Types (using SQLite)

I have followed the quickstart setup for Strapi, which as I understand sets up an SQLite Database. I created one collection type (painting) which worked without problems, but whenever I try to add a new one or make changes to the existing one the server does not restart and I receive the following error output in my console:
[2022-05-23 16:53:16.323] error: CREATE TABLE _knex_temp_alter889 (id integer not null primary key autoincrement PRIMARY KEY AUTOINCREMENT NOT NULL, title varchar(255) NULL, artist varchar(255) NULL, created_at datetime NULL, updated_at datetime NULL, published_at datetime NULL, created_by_id integer NULL, updated_by_id integer NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL) - table "_knex_temp_alter889" has more than one primary key
SqliteError: CREATE TABLE _knex_temp_alter889 (id integer not null primary key autoincrement PRIMARY KEY AUTOINCREMENT NOT NULL, title varchar(255) NULL, artist varchar(255) NULL, created_at datetime NULL, updated_at datetime NULL, published_at datetime NULL, created_by_id integer NULL, updated_by_id integer NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_created_by_id_fk FOREIGN KEY (created_by_id) REFERENCES admin_users (id) ON DELETE SET NULL, CONSTRAINT paintings_updated_by_id_fk FOREIGN KEY (updated_by_id) REFERENCES admin_users (id) ON DELETE SET NULL) - table "_knex_temp_alter889" has more than one primary key
Deleting the changes I made in my content-types files is currently the only way I have of getting the server to run again. How do I fix this?
So, I ended up creating a new project. Problem seems to have been that I added a field named 'ID', which I assume messed everything up because it conflicted with the automatically assigned ID-System.

SQL - how do I insert into these tables?

create table Employees
(
EmployeeID INT IDENTITY (1,1) PRIMARY KEY NOT NULL,
EmployeeName VARCHAR(255) NOT NULL,
EmployeeUsername VARCHAR(255) NOT NULL,
EmployeeEmail VARCHAR(255) NOT NULL,
GroupID INT FOREIGN KEY REFERENCES TypeOfGroup(GroupID),
Password VARCHAR(255) NOT NULL
);
create table TypeOfGroup
(
GroupID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
TypeGroup VARCHAR(255) NOT NULL,
Permission CHAR(1) NOT NULL
);
Question: I have a foreign key and wanted to make a connection with another table as I do this to insert into.
My foreign key is 'GROUPID'.
Try this...
insert into TypeOfGroup( TypeGroup, Permission) values ('hr', 1)
"this 1 is used for bit which is a data type in sql server 1 for true and 0 for false"
insert into Employees (EmployeeName, EmployeeUsername, EmployeeEmail,
GroupID, Password) values ('bruno', 'bruno', 'bruno#gmail.com', 1, 'urPassword')
and 1 in the 2nd query is primary of typeofgroup table.

Recursive foreign key 'on delete cascade'

CREATE TABLE IF NOT EXISTS type (
tid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (tid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_type_0 ON type (tid,uuid,name);
CREATE TABLE IF NOT EXISTS object (
oid VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
tid INTEGER NOT NULL,
CONSTRAINT FK_tid FOREIGN KEY (tid) REFERENCES type(tid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid)
);
CREATE INDEX IDX_object_0 ON object (oid,timestamp,tid);
CREATE TABLE IF NOT EXISTS object_user_owner (
uid INTEGER NOT NULL,
oid VARCHAR NOT NULL,
CONSTRAINT FK_uid FOREIGN KEY (uid) REFERENCES user(uid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT FK_oid FOREIGN KEY (oid) REFERENCES object(oid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid,uid)
);
CREATE INDEX IDX_object_user_owner_0 ON object_user_owner (uid,oid);
CREATE TABLE IF NOT EXISTS user (
uid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
password VARCHAR NOT NULL,
salt VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
lastaccess VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (uid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_user_0 ON user (uid,uuid,name);
The above three sqlite3 tables contain foreign keys. The problem is the deletion of a key in the upper table type. When I try do delete I get 'FOREIGN KEY constraint failed' Error. Deleting a from the lowest table object_user_owner before deleting a type works. I think sqlite does not check any recursive cascade constraints. Does anyone have experienced this too or is anything wrong with my design?

SimpleMembership Sql Scripts

I know that SimpleMembership will auto create the tables needed for authentication but I would much rather run the sql scripts at some time other than when the application is ran. Is there a download page for the SimpleMembership scripts?
I found a script on github
CREATE TABLE [dbo].[UserProfile] (
[UserId] INT IDENTITY (1, 1) NOT NULL,
[UserName] NVARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([UserId] ASC)
);
CREATE TABLE [dbo].[webpages_Membership] (
[UserId] INT NOT NULL,
[CreateDate] DATETIME NULL,
[ConfirmationToken] NVARCHAR (128) NULL,
[IsConfirmed] BIT DEFAULT ((0)) NULL,
[LastPasswordFailureDate] DATETIME NULL,
[PasswordFailuresSinceLastSuccess] INT DEFAULT ((0)) NOT NULL,
[Password] NVARCHAR (128) NOT NULL,
[PasswordChangedDate] DATETIME NULL,
[PasswordSalt] NVARCHAR (128) NOT NULL,
[PasswordVerificationToken] NVARCHAR (128) NULL,
[PasswordVerificationTokenExpirationDate] DATETIME NULL,
PRIMARY KEY CLUSTERED ([UserId] ASC)
);
CREATE TABLE [dbo].[webpages_OAuthMembership] (
[Provider] NVARCHAR (30) NOT NULL,
[ProviderUserId] NVARCHAR (100) NOT NULL,
[UserId] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Provider] ASC, [ProviderUserId] ASC)
);
CREATE TABLE [dbo].[webpages_Roles] (
[RoleId] INT IDENTITY (1, 1) NOT NULL,
[RoleName] NVARCHAR (256) NOT NULL,
PRIMARY KEY CLUSTERED ([RoleId] ASC),
UNIQUE NONCLUSTERED ([RoleName] ASC)
);
CREATE TABLE [dbo].[webpages_UsersInRoles] (
[UserId] INT NOT NULL,
[RoleId] INT NOT NULL,
PRIMARY KEY CLUSTERED ([UserId] ASC, [RoleId] ASC),
CONSTRAINT [fk_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[UserProfile] ([UserId]),
CONSTRAINT [fk_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [dbo].[webpages_Roles] ([RoleId])
);

Resources