SQL - how do I insert into these tables? - sql-insert

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.

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

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

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?

Oracle SQL classify Subtype

CREATE TABLE Customer
(
CustomerID NUMBER NOT NULL,
AccountID NUMBER(10) NOT NULL,
CustomerType VARCHAR(10) NOT NULL,
CustomerStatus VARCHAR(10) NOT NULL,
CONSTRAINT customer_pk PRIMARY KEY (CustomerID),
CONSTRAINT check_customer_status CHECK(CustomerStatus IN ('Ineligible', 'Eligible')),
CONSTRAINT check_customer_type CHECK(CustomerType IN ('NonResident', 'Residential'))
);
Alter table CUSTOMER add CONSTRAINT res_customer_type UNIQUE (CustomerID, CustomerType)
CREATE TABLE nonresidential
(
CustomerID NUMBER NOT NULL,
CustomerType VARCHAR(10) NOT NULL,
BusinessName VARCHAR(10) NOT NULL,
ABNNumber NUMBER(10),
Email VARCHAR(35),
CONSTRAINT pk_nrtable PRIMARY KEY (CustomerID),
CONSTRAINT nonres_type CHECK (CustomerType = 'nonresident'),
CONSTRAINT res_customer_type UNIQUE (CustomerID, CustomerType) REFERENCES Customer(CustomerID, CustomerType)
);
The last line keeps coming up as missing parenthesis (around the references line along the unique constraint). I'd like to specify the unique constraint to create a subtype table under customer.
P.S I'm using oracle sql, thanks
You use the Unique keyword where you should use the Foreign key keyword.
CONSTRAINT res_customer_type UNIQUE (CustomerID, CustomerType) REFERENCES Customer(CustomerID, CustomerType)
should be
CONSTRAINT res_customer_type FOREIGN KEY (CustomerID, CustomerType) REFERENCES Customer(CustomerID, CustomerType)

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

Resources