SQL oracle Developer error - create-table

CREATE TABLE person_details
( id NOT NULL AUTO_INCREMENT,
name varchar(100),
age int,
gender varchar(8),
adult varchar(10),
contact_no int,
address varchar(255),
pnr_number int
)
Error at Command Line:2 Column:15
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:

You forgot to define the type of id field here:
( id NOT NULL AUTO_INCREMENT,
Should be something like:
( id INT NOT NULL AUTO_INCREMENT,
^^^^
And make it as a primary key like
pnr_number int,
PRIMARY KEY (id)

Related

Mysql Error 1064 when trying to create a table

I'm trying to create a table so that I can pass in a csv file to load it into the table but I'm getting this error and I'm not sure why. Here's what I have:
CREATE TABLE imdb(
-> rank int auto_increment PRIMARY key,
-> title varchar(255),
-> genre varchar(255),
-> director varchar(255),
-> actors varchar(255),
-> year int,
-> rating decimal(1,1),
-> votes int,
-> metascore int
-> );
Here is the error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rank int
auto_increment PRIMARY key,
title varchar(255),
genre varchar(255),
dir' at line 2
Those arrows shouldn't be there (maybe something that got added when copy/pasting?). Also, rank and year are reserved words (thanks #
Aloiso Gomes) so you have to add backticks to allow the name anytime you reference it (stored procs, selects, inserts, etc.)
This works:
CREATE TABLE imdb(
`rank` int auto_increment PRIMARY key,
`title` varchar(255),
`genre` varchar(255),
`director` varchar(255),
`actors` varchar(255),
`year` int,
`rating` decimal(1,1),
`votes` int,
`metascore` int
);
INSERT INTO imdb (title,genre) VALUES
('Fast Furious 1', 'racing'),
('Fast Furious 2', 'racing'),
('Fast Furious 3', 'racing?'),
('Fast Furious 4', '"racing"');
SELECT `rank`, title, genre FROM imdb;

Please, can someone help me to create a trigger in mysql that has a same function with the assertion below?

So basically I want to create a trigger for mysql database that has same/similar function with the assertion code below (since mysql does not support assertion)
Here is the assertion code that I want to replicate using trigger
CREATE ASSERTION assert
CHECK NOT EXISTS(SELECT * FROM paper P WHERE 3 <>(SELECT COUNT(*)
FROM review R
WHERE R.paperid = P.paperid)
);
CREATE ASSERTION atmostfivepapers
CHECK NOT EXISTS(SELECT * FROM pcmember P WHERE 5 <
( SELECT *
FROM review R
WHERE R.email = P.email
)
);
And here is my table
CREATE TABLE paper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
abstract VARCHAR(250),
pdf VARCHAR(100),
PRIMARY KEY(paperid)
);
CREATE TABLE author(
email VARCHAR(100) NOT NULL,
name VARCHAR(50),
affiliation VARCHAR(100),
PRIMARY KEY(email)
);
CREATE TABLE writePaper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR(100),
paper_order INT,
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES author(email)
);
CREATE TABLE pcmember(
email VARCHAR(100) NOT NULL,
name VARCHAR(20),
PRIMARY KEY(email)
);
CREATE TABLE review(
reportid INT UNSIGNED,
sdate DATE,
comment VARCHAR(250),
recommendation CHAR(1),
paperid INT UNSIGNED,
email VARCHAR(100),
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES pcmember(email)
);
Thanks in advance
First create another table replicating the columns and their features. Thereafter create a trigger.
CREATE TRIGGER afterInsert_trigger on yourMainTableName
FOR INSERT
AS
INSERT INTO yourTableCopy(column1ofYourReplicateTable, column2OFyourReplicateTable, ... etc)
SELECT column1ofYourMainTable, column2ofYourMainTable, ..., etc
FROM INSERTED
That's what I use in Microsoft SQL server

Use trigger as assertion in MySQL

I have a database and a few tables in MySql. I am trying to implement an Oracle assertion in MySQL using CREATE TRIGGER. I am not sure if I used the proper syntax.
This is what I have so far, but I'm not sure why it's wrong.
CREATE TRIGGER tg_review_before_insert
BEFORE INSERT ON review
FOR EACH ROW
SET NEW.paper = IF((SELECT * FROM paper P WHERE 3 <>(SELECT COUNT(*)
FROM review R
WHERE R.paperid = P.paperid)
);
Here are my tables:
CREATE TABLE paper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
abstract VARCHAR(250),
pdf VARCHAR(100),
PRIMARY KEY(paperid)
);
CREATE TABLE author(
email VARCHAR(100) NOT NULL,
name VARCHAR(50),
affiliation VARCHAR(100),
PRIMARY KEY(email)
);
CREATE TABLE writePaper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR(100),
paper_order INT,
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES author(email)
);
CREATE TABLE pcmember(
email VARCHAR(100) NOT NULL,
name VARCHAR(20),
PRIMARY KEY(email)
);
CREATE TABLE review(
reportid INT UNSIGNED,
sdate DATE,
comment VARCHAR(250),
recommendation CHAR(1),
paperid INT UNSIGNED,
email VARCHAR(100),
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES pcmember(email)
);
Here are the assertions I am trying to implement from Oracle SQL code to MySQL SQL code:
CREATE ASSERTION assert
CHECK NOT EXISTS(SELECT * FROM paper P WHERE 3 <>(SELECT COUNT(*)
FROM review R
WHERE R.paperid = P.paperid)
);
CREATE ASSERTION atmostfivepapers
CHECK NOT EXISTS(SELECT * FROM pcmember P WHERE 5 <
( SELECT *
FROM review R
WHERE R.email = P.email
)
);

What's wrong with this MySQL syntax?

Alright so I'm trying to tie a Spigot plugin that I'm making into MySQL, and I did it successfully up until the point where I edited the code that was creating the table. I find that MySQL stack traces are much too ambiguous to be useful, so I have no idea what I'm doing wrong here.
Code:
CREATE TABLE IF NOT EXISTS WebsiteLink_keys(id INT NOT NULL KEY AUTO_INCREMENT, key VARCHAR(36), trimmedUUID VARCHAR(36), playerUUID VARCHAR(36), date TIMESTAMP, status TEXT);
key is a reserved word in MySQL. If you absolutely must use it as a column name, you can escape it using backticks:
CREATE TABLE IF NOT EXISTS WebsiteLink_keys (
id INT NOT NULL KEY AUTO_INCREMENT,
`key` VARCHAR(36), -- Here!
trimmedUUID VARCHAR(36),
playerUUID VARCHAR(36),
date TIMESTAMP,
status TEXT
)
Or, better yet, use a name that isn't a reserved word, such as link_key:
CREATE TABLE IF NOT EXISTS WebsiteLink_keys (
id INT NOT NULL KEY AUTO_INCREMENT,
link_key VARCHAR(36), -- Here!
trimmedUUID VARCHAR(36),
playerUUID VARCHAR(36),
date TIMESTAMP,
status TEXT
)
CREATE TABLE IF NOT EXISTS WebsiteLink_keys(
id INT NOT NULL AUTO_INCREMENT,
`key` VARCHAR(36),
trimmedUUID VARCHAR(36),
playerUUID VARCHAR(36),
`date` TIMESTAMP,
status TEXT,
PRIMARY KEY (id)
);
PRIMARY KEY (id) is at the end

constraint error on INPUT command

I am doing some MySQL coding but getting a few errors when entering data.
Can you have a look at my code and just see if I am missing something?
CREATE DATABASE test1
USE test1
CREATE TABLE subject1 (
s1_id INT NOT NULL AUTO_INCREMENT,
field1 VARCHAR(20),
field2 VARCHAR(30),
field3 CHAR(10),
field4 VARCHAR(30),
PRIMARY KEY (s1_id)
);
CREATE TABLE subject2 (
s2_id INT NOT NULL AUTO_INCREMENT,
s2_s1_id INT NOT NULL,
field5 VARCHAR(20),
field6 DATE,
field7 DATE,
Field8 INT,
PRIMARY KEY (s2_id),
FOREIGN KEY (s2_s1_id) REFERENCES subject1 (s1_id)
);
I keep getting a constraint error on the s2_s1_id field.
INPUT INTO subject2 (
`s2_s1_id`,
`field5`,
`field6`,
`field7`,
`field8`
)
VALUES (
'1',
' SOLOTION 2.5',
'2015-10-10 10:00:00',
'2015-10-11 10:30:00','25'
);
Mysql error 1452 - cannot add or update a child row: a foreign constraint fails.
Any thoughts? It's a DB for my own personal use to track my numbers for dialysis.