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
Related
I have this code:
CREATE TABLE Company ( Comp_Code INT(5) NOT NULL AUTO_INCREMENT=1000, Comp_Name VARCHAR(15) NOT NULL, PRIMARY KEY (Comp_Code) );
but when i run it in MYSQL from WAMPServer it gives an 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 '=1000,
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
)' at line 2
Why is this happening? I can't seem to find any solution to this particular issuer.
AUTO_INCREMENT is column option.
Initial AUTO_INCREMENT=1000 value is table option.
CREATE TABLE Company (
Comp_Code INT(5) NOT NULL AUTO_INCREMENT, -- specify that the column is AI
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
) AUTO_INCREMENT=1000; -- specify initial AI value
I have a php page where the user enters the following details
Name
Age
But in the database, I want to have an additional column that automatically inserts data with the combination of entered data as shown below for Code column.
Key
Name
Age
Code
1
Mike
28
MIKE128
2
Raj
32
RAJ232
So I have altered my table as follow
ALTER TABLE Employee ADD COLUMN Code;
UPDATE Employee SET Code = CONCAT(Key,Name,Age);
But this updates only the existing data for existing data,and shows NULL for newly entered data. What is the command I shall use to change the schema so that whenever I add new data automatically it updates the Code column
Below is my try, which is failed
CREATE TABLE `Employee` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(50) NOT NULL,
`Age` int(2) NOT NULL,
`Combined` varchar(10) AS (`Name` + `id` + `Age`) Code,
PRIMARY KEY (`id`)
);
ERROR 1064 (42000) at line 2: 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 'Code,
PRIMARY KEY (id)
)' at line 4
Note: I DO NOT want to concate and supply data from the front end.
Note: This is just example as I can not post real data.
You can setup trigger for dynamic default value
CREATE TRIGGER Employee_code_trigger
BEFORE INSERT ON Employee
FOR EACH ROW
SET NEW.Code = IFNULL(NEW.Code, CONCAT(NEW.Key, NEW.Name, NEW.Age));
I am trying to alter a table and set a default value for a nullable column. But i get the following error.
Here is the command:
ALTER TABLE `questiontboard`.`questions`
CHANGE COLUMN `status` `status` (11) NULL DEFAULT 1 ;
Here is the error:
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 '(11) NULL DEFAULT 1' at line 2
SQL Statement:
ALTER TABLE `questionboard`.`questions`
CHANGE COLUMN `status` `status` (11) NULL DEFAULT 1
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'question' already exists
What am i doing wrong?
You forgot the data type. Did you mean
ALTER TABLE `questiontboard`.`questions`
CHANGE COLUMN `status` `status` INT(11) NULL DEFAULT 1 ;
^^^
Your query should be this:
ALTER TABLE `questiontboard`.`questions`
CHANGE COLUMN `status` `status` int(11) NULL DEFAULT 1 ;
^^ here add int as you want the datatype
You are missing datatype of field in the query.
I got the same error when altering a table. I did the exact same thing you did (minus the code typo).
I got the error when altering a column from a SMALLINT to a varchar(n). It gives the "1050 Table already exists..." error. The error was confusing. Of course the table exists, that's why I'm trying to alter it!
In the end, I found out that the problem was that my new varchar(2) was not big enough to hold all the original smallint data. I had one row that had a 4 digit number, so varchar(2) wouldn't work. I changed it to use varchar(4), and it worked.
ALTER TABLE omiccom_wp.myTable
CHANGE COLUMN myColumn myColumn VARCHAR(2) NOT NULL DEFAULT '0' ;
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
I'm trying to create a Temp Table that has a key field that auto increments. But I keep running into a syntax error.
Here's what I'm trying:
CREATE TEMPORARY TABLE
RETURN_ARTISTS
(KEY INT(11) NOT NULL AUTO_INCREMENT,
ARTIST_1_KEY INT(11),
ARTIST_2_KEY INT(11));
INSERT INTO RETURN_ARTISTS (1,1);
DELETE TABLE RETURN_ARTISTS;
And here's the error I keep getting:
Err] 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 'INT(11) NOT NULL AUTO_INCREMENT,
ARTIST_1_KEY INT(11),
ARTIST_2_KEY INT(' at line 3
First of all, key is a reserved word, escape it with `
Secondly, when using auto_increment column, that column must be defined as a key.
CREATE TEMPORARY TABLE
`RETURN_ARTISTS`
(`KEY` INT(11) NOT NULL AUTO_INCREMENT,
ARTIST_1_KEY INT(11),
ARTIST_2_KEY INT(11),
KEY (`KEY`));
key is a reserved word, so you'll have to escape it:
(`KEY` INT(11) NOT NULL AUTO_INCREMENT,
^---^--- escapes
or, preferably, use a different field name.