Syntax Error when using the source command in MySql - mariadb

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)
);

Related

Reference to composite key in another table in MariaDB

I have the following script for creating three tables in MariaDB:
CREATE TABLE `booking` (
`booking_id` int(11) NOT NULL,
`booking_date` date DEFAULT NULL,
`room_no` int(11) DEFAULT NULL,
`guest_id` int(11) NOT NULL,
`occupants` int(11) NOT NULL DEFAULT '1',
`room_type_requested` varchar(6) DEFAULT NULL,
`nights` int(11) NOT NULL DEFAULT '1',
`arrival_time` varchar(5) DEFAULT NULL,
PRIMARY KEY (`booking_id`),
KEY `room_no` (`room_no`),
KEY `guest_id` (`guest_id`),
KEY `room_type_requested` (`room_type_requested`,`occupants`),
CONSTRAINT `booking_ibfk_1` FOREIGN KEY (`room_no`) REFERENCES `room` (`id`),
CONSTRAINT `booking_ibfk_2` FOREIGN KEY (`guest_id`) REFERENCES `guest` (`id`),
CONSTRAINT `booking_ibfk_3` FOREIGN KEY (`room_type_requested`) REFERENCES `room_type` (`id`),
CONSTRAINT `booking_ibfk_4` FOREIGN KEY (`room_type_requested`, `occupants`) REFERENCES `rate` (`room_type`, `occupancy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `rate` (
`room_type` varchar(6) NOT NULL DEFAULT '',
`occupancy` int(11) NOT NULL DEFAULT '0',
`amount` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`room_type`,`occupancy`),
CONSTRAINT `rate_ibfk_1` FOREIGN KEY (`room_type`) REFERENCES `room_type` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `room_type` (
`id` varchar(6) NOT NULL,
`description` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to add another table, accommodation_payments. I want it to be linked to the amount in the rate table. How to define my foreign key (lets call it room_type_booked)?
Is it better to link it to the booking table or to the room_type table similar to how the FK room_type_requested in the booking table was created (see the latter option in the ERD above)?
Why are there are two constraints for room_type_requested in booking?
CREATE TABLE `accommodation_payments` (
`acc_pmt_id` int(11) NOT NULL,
`room_type_booked` varchar(6) DEFAULT NULL,
`payment_date` date DEFAULT NULL,
`payment_method` varchar(30) NOT NULL,
`pmt_amount` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`acc_pmt_id`),
KEY `room_type_booked` (`room_type_booked`),
CONSTRAINT `acc_pmt_ibfk_1` FOREIGN KEY (`room_type_booked`) REFERENCES `booking` (`room_type_requested`));

Create MySQL schema

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

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.

#1062 - Duplicate entry '1' for key 'PRIMARY'

I am importing database and i got an error
1062 - Duplicate entry '1' for key PRIMARY.
i tried to upload table individually But it gives me error for those table which have at least one row.
i don't want to delete any record of any table.
here is one table which is giving error.
CREATE TABLE IF NOT EXISTS `wpbeta_aff_affiliates` (
`affiliate_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`email` varchar(512) DEFAULT NULL,
`from_date` date NOT NULL,
`thru_date` date DEFAULT NULL,
`status` varchar(10) NOT NULL DEFAULT 'active',
`type` varchar(10) DEFAULT NULL,
PRIMARY KEY (`affiliate_id`),
KEY `affiliates_afts` (`affiliate_id`,`from_date`,`thru_date`,`status`),
KEY `affiliates_sft` (`status`,`from_date`,`thru_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `wpbeta_aff_affiliates`
--
INSERT INTO `wpbeta_aff_affiliates` (`affiliate_id`, `name`, `email`, `from_date`, `thru_date`, `status`, `type`) VALUES
(1, 'Direct', NULL, '2013-07-03', NULL, 'active', 'direct');
You do not need to specify the record id (with the exception of some circumstances when you need to keep the connection between records in different tables). Try using this query:
INSERT INTO wpbeta_aff_affiliates (name, email, from_date, thru_date, status, type) VALUES ('Direct', NULL, '2013-07-03', NULL, 'active', 'direct');

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