I have a 2-table schema which can be specified as follows:
CREATE TABLE MEMBERS
( membershipnumber int NOT NULL PRIMARY KEY,
firstname varchar(20) NOT NULL,
lastname varchar(20) NOT NULL,
email varchar(30) NOT NULL,
status varchar(15) NOT NULL DEFAULT 'unapproved',
);
CREATE TABLE TELEPHONENUMBERS
( telephone varchar(15) NOT NULL PRIMARY KEY,
membershipnumber INT NOT NULL REFERENCES MEMBERS(membershipnumber),
isprimary enum('0','1') NOT NULL DEFAULT '1'
);
I am trying to specify an integrity constraint to indicate that any individual member may have only one primary telephone number (i.e. telephonenumbers.isprimary='1') and any number of secondary telephone numbers.
Here is my current attempt:
ALTER TABLE MEMBERS
ADD CONSTRAINT oneprimary_ck CHECK (SELECT COUNT(isprimary)=1 FROM TELEPHONENUMBERS WHERE TELEPHONENUMBERS.membershipnumber = membershipnumber) IN '1';
However this yields the following syntax 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 'SELECT COUNT(isprimary)=0 FROM TELEPHONENUMBERS WHERE TELEPHONENUMBERS.membershipnumber=1 IN '1') IN ''
I'm assuming my approach is totally wrong, but am unsure how to proceed. Would it be better to do this using a trigger or is there a small alteration ot my approach that would work?
I am using server version: 5.5.30.
With many thanks,
Froskoy.
Does it work any better if you swap ...
ALTER TABLE MEMBERS ADD CONSTRAINT oneprimary_ck CHECK
(SELECT COUNT(isprimary)=1 FROM TELEPHONENUMBERS
WHERE TELEPHONENUMBERS.membershipnumber = membershipnumber) IN '1';
... with ...
ALTER TABLE MEMBERS ADD CONSTRAINT oneprimary_ck CHECK
(SELECT COUNT(isprimary) FROM TELEPHONENUMBERS
WHERE isprimary=1 AND TELEPHONENUMBERS.membershipnumber = membershipnumber) > 1;
... ?
Two things are wrong in your approach:
A check constraint cannot contain a subquery
Check constraints are not enforced by MySQL (not in 5.5 anyway)
What you can do, is create a trigger that raises an error. Here's an example:
https://stackoverflow.com/a/7189396/417194
Related
I am new to SQL databases. I am using MySQL for my current project
Here is my query
CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID VARCHAR(50) NOT NULL FOREIGN KEY REFERENCES users(U_ID),
USER2_ID VARCHAR(50) NOT NULL FOREIGN KEY REFERENCES users(U_ID),
TIME_OF_FRIENDSHIP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
When I run this query I get this error.
>[Error] Script lines: 1-22 -------------------------
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 'CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID V' at line 17
Warnings: --->
W (1): 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 'CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID V' at line 17
<---
[Executed: 18/11/2020 8:14:46 AM] [Execution: 0ms]
Can someone help me out with this problem.
I guess you are using MS SQL Server syntax and you try to run it on MySQL. Here is MySQL syntax for creating table with FK.
CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID VARCHAR(50) NOT NULL,
USER2_ID VARCHAR(50) NOT NULL,
TIME_OF_FRIENDSHIP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (USER2_ID) REFERENCES USERS(U_ID),
FOREIGN KEY (USER1_ID) REFERENCES USERS(U_ID)
);
Reference https://www.w3schools.com/sql/sql_foreignkey.asp
I am getting a SQL query syntax error while creating a table. Here is the SQL query:
CREATE TABLE ACCOUNT (
ACCNO NUMBER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BA L NUMBER(8,2) NOT NULL,
CREATION-DT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
Here is the error:
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 'NUMBER(5) NOT NULL.
What is wrong with my syntax?
Using SQLfiddle you can see this is not valid.
You have multiple problems with your syntax; invalid data types, invalid column names, etc. You can encapsulate the invalid names in backticks but then you will have to remember to encapsulate them later.
Instead of Number you probably meant numeric but i would suggest trying Integer instead.
Or just try this:
CREATE TABLE ACCOUNT (
ACCNO INTEGER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BAL INTEGER(8) NOT NULL,
CREATIONDT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
NUMBER is not a valid datatype in MySQL, you should choose the one from the list (maybe you've meant NUMERIC).
You should also escape some identifiers (in your case, column names BA L, which includes a space within it, and CREATION-DT, which includes a dash) with backticks ` in MySQL:
CREATE TABLE `ACCOUNT` (
`ACCNO` NUMERIC(5) NOT NULL,
`NAME` VARCHAR(20) NOT NULL,
`BA L` NUMERIC(8,2) NOT NULL,
`CREATION-DT` DATE NOT NULL,
PRIMARY KEY ( `ACCNO` )
);
SQLFiddle
I try to create a table but I keep getting an SQL Error but I can't figure out why.
This is the error:
check the manual that corresponds to your MySQL server version for the
right syntax to use near '+491634170770 (Id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT,Type VARCHAR(20),Conte' at line 1 ERROR 1064 (42000):
You have an error in your SQL syntax;
This is my statement:
CREATE TABLE +491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
I already tried to find a solution here, but my syntax seems to be correctly. I think its because of the name of the table. But using backticks like this ´+491234175789´ didn't work.
This is the backtick `:
CREATE TABLE `+491234175789` (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
However, don't create a table name that requires backticks. It is just bad form and makes queries harder to read and write -- you are creating problems for the future. Call it something like:
CREATE TABLE t_491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
I have created the customer table that has a trigger that not allow null value on the customer_id column. it looks like:
create table customer(
customer_id varchar(20) UNIQUE,
customer_name varchar(15) NOT NULL,
password varchar(10) NOT NULL,
social_number varchar(14) not null,
phone_number varchar(13) NOT NULL,
email varchar(30) NOT NULL,
address varchar(50) NOT NULL,
primary key ( social_number )
);
But, creating the following trigger causes this error.
create trigger null_checker
on customer
after insert
as
delete from customer
where customer_id is null;
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 'on customer after insert as delete from customer where customer_id is null' at line 1
What's wrong with? I could add not-null constraint using not null specifier. but I am not allowed to do it because creating the trigger is a part of my assignment. Any help would be appreciated.
You need to use right syntax:
create trigger null_checker
after insert on customer
for each row
delete from customer
where customer_id is null;
But you cannot modify the same table from trigger.
A solution, without triggers, may be is change the sql_mode to avoid null values defined in your columns.
set sql_mode = 'TRADITIONAL';
That command says to MySQL “give an error instead of a warning” when inserting an incorrect value into a column, like inserting NULL to a 'NOT NULL column.
More info: http://dev.mysql.com/doc/refman/5.0/en/sql-mode.html
Creating a database for a class in PHP and MYSQL. Running into an error when creating the database, phpMyAdmin gives me a useless error message. (Software that gives a better error message would be awesome BTW)
phpMyAdmin error:
#1064 - 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 'CREATE TABLE customers ( customer_id INT UNSIGNED NOT NULL auto increment, fir' at line 1
USE isys288_gottfrk
CREATE TABLE customers ( customer_id INT UNSIGNED NOT NULL
auto increment, first_name carchar(20) NOT NULL, last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id), INDEX full_name (last_name, first_name) ) engine =
innodb;
There were several errors in your code.
USE isys288_gottfrk;
CREATE TABLE customers
(
customer_id INT UNSIGNED NOT NULL auto_increment,
first_name varchar(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id),
INDEX full_name (last_name, first_name)
) engine = innodb;
I suggest you use a DB tool like MySQL Workbench to generate your tables. Then you can see the problems right away.
Juergen d edited your question for formatting, this might have helped you as well: Your initial statement was all on one line, so the error was flagged in that one line. If you edited it on multi lines (like in your question, or better in his answer) you might have gotten a better message, e.g. one that flags that the line containing carchar(20) contains a problem (it's even better the way he has it in his answer, but it correctly uses varchar(20), not flagging a problem)
Yes, it would be nice if error messages would immediately flag the problem, but sometimes it's already better when the input is formatted in a more human-readable way. I've tested the same with a multiline statement, mysql stated
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 'carchar(20)
Try this:
CREATE TABLE customers (
customer_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, first_name VARCHAR (20) NOT NULL
, last_name VARCHAR (40) NOT NULL
, PRIMARY KEY (customer_id)
, INDEX full_name (last_name, first_name)
) ENGINE = INNODB ;
You can use some GUI clients like SQLyog for this.