I have a table named quotation details with some columns
Field Name | Type
------------------------
Quotati_Id | bigint(20)
Fk_Rfq_Id | bigint(20)
Quotati_No | varchar(30)
Parent_Quotati_Id | bigint(20)
Fk_Client_Supplie_Id | int(11)
Is_Client_Supplie | bit(1)
and I want to insert data. The insert query is qiven below
INSERT INTO quotationdetails (
Fk_Rfq_Id,
Quotati_No,
Parent_Quotati_Id,Fk_Client_Supplie_Id,
Is_Client_Supplie
) VALUES (
'15847',
(SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA='qtn' AND
TABLE_NAME='quotationdetails'),
'15640', '1',
'0')
Everything is works fine , but only one problem the column named Is_Client_Supplie is inserted wrongly. ie 1 is inserted instead of 0 in the column Is_Client_Supplie .
Whats wrong with me???
It is a bit field, not a string, so remove the apostrophes from '0'. You can do the same for Fk_Client_Supplie_Id and the other integer fields.
A bit field such as bit(3) can be assigned a binary value use the notation b'101' but if assigning 0 this notation is not necessary.
use
INSERT INTO quotationdetails (
Fk_Rfq_Id,
Quotati_No,
Parent_Quotati_Id,Fk_Client_Supplie_Id,
Is_Client_Supplie
) VALUES (
'15847',
(SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA='qtn' AND
TABLE_NAME='quotationdetails'),
'15640', '1',
0)
No need of '0', use 0 instead.
Related
I want mysql to show me a table where the last string of a column has a specific letter
SELECT * FROM myTable WHERE col LIKE '%n';
nothing is going displayed (0 rows displayed).
But this one works
SELECT * FROM myTable WHERE col LIKE 'L%';
So when looking for the beginning of a string it will give me an output, but when looking for the end of a string it won't. I also tried it with other columns and it did not work.
Why?
The word it is looking for is London
The table was created like this (found this sample on a webpage):
CREATE TABLE IF NOT EXISTS `company` (
`COMPANY_ID` varchar(6) NOT NULL DEFAULT '',
`COMPANY_NAME` varchar(25) DEFAULT NULL,
`COMPANY_CITY` varchar(25) DEFAULT NULL,
PRIMARY KEY (`COMPANY_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `company`
--
INSERT INTO `company` (`COMPANY_ID`, `COMPANY_NAME`, `COMPANY_CITY`) VALUES
('18', 'Order All', 'Boston\r'),
('15', 'Jack Hill Ltd', 'London\r'),
('16', 'Akas Foods', 'Delhi\r'),
('17', 'Foodies.', 'London\r'),
('19', 'sip-n-Bite.', 'New York\r');
Your sample data shows that you have a carriage return character (\r) as the last character in the string. If we eliminate that from the search, the remaining string does match.
mysql> SELECT * FROM company where TRIM('\r' from company_city) LIKE '%n';
+------------+---------------+--------------+
| COMPANY_ID | COMPANY_NAME | COMPANY_CITY |
+------------+---------------+--------------+
| | Order All | Boston
| | Jack Hill Ltd | London
| | Foodies. | London
+------------+---------------+--------------+
I recommend not to store trailing whitespace characters in your strings. Take care of newlines and carriage returns in your application presentation, not in the data.
I created a new table with:
CREATE TABLE `test`.`tab1` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(50) NULL , `age` INT NULL , PRIMARY KEY (`id`));
When I insert a new row, but want to keep age empty, I do:
INSERT INTO `tab1` (`id`, `Name`, `Age`) VALUES (NULL, 'Peter', '');
I get this entry:
| id | name | age |
|----|-------|-----|
| 1 | Peter | 0 |
Why the age column just doesn't remain empty or NULL?
How can I set a default value, so that it just remains empty, when no value is specified while inserting?
Since the column is of type int the DB tries to convert the string '' to a number. That would be 0.
Just use null instead of '' if you don't have a value. That is what null is for.
i´m trying to insert some values in mysql but i get error code 1136, i verified and one of the values is auto-increment so i don't have to enter that one and the rest give a total of 18 which are exactly the total values i'm writting, could somebody help me?
This is the table im using:
FIELD TYPE NULL KEY DEFAULT EXTRA
id_display_detail int(11) NO PRI auto_increment
amount double NO
amount_lc double NO
exchange double NO
company varchar(10) NO
date datetime NO
description varchar(100) NO
document_number varchar(20) NO
document_type varchar(2) NO
posting_key varchar(3) NO
special_gl varchar(1) NO
status int(11) NO
voucher_number varchar(40) NO
year int(11) NO MUL
id_currency int(11) NO MUL
id_employee int(11) NO MUL
credit bit(1) YES
card_type varchar(45) NO
line_item int(11) YES
And this is my code:
INSERT INTO display_detail VALUES (300,300,0,'2001','2016-04-11',
'Downpayment ZM00080621','2000010802','ZP','29','R',0,
'GCCTEA 8062130',2016,1,1561,0,NULL,1);
Am i missing something?
and one of the values is auto-increment so i don't have to enter that one
That doesn't change the fact that the number of values in your VALUES clause has to match the number of columns.
You need to either specify NULL as the value for the auto_increment column - or specify a column list after INSERT first.
You missing the column name
(Because the id is automatic the values you provided don't math the number of column so must declare the column name)
INSERT INTO display_detail ( amount,
amount_lc ,
exchange ,
company ,
date ,
description ,
document_number,
document_type ,
posting_key ,
special_gl ,
status ,
voucher_number ,
year ,
id_currency ,
id_employee ,
credit ,
card_type ,
line_item ) VALUES (300,300,0,'2001','2016-04-11',
'Downpayment ZM00080621','2000010802','ZP','29','R',0,
'GCCTEA 8062130',2016,1,1561,0,NULL,1);
It appears that you aren't listing the columns in your INSERT statement. A MySQL query typically looks like this:
INSERT INTO table
(column1, column2, ... )
VALUES
(expression1, expression2, ... ),
(expression1, expression2, ... ),
...;
(Taken from: http://www.techonthenet.com/mysql/insert.php)
The final query would be something like this:
INSERT INTO display_detail
(amount, amount_lc, exchange, company, date, description,
document_number, document_type, posting_key,special_gl,status,voucher_number,year,
id_currency, id_employee, credit, card_type, line_item)
VALUES (300,300,0,'2001','2016-04-11',
'Downpayment ZM00080621','2000010802','ZP','29','R',0,
'GCCTEA 8062130',2016,1,1561,0,NULL,1);
I have table like this
table
id Varchar(45) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name CHAR(30) NOT NULL,
I want to increment my id field like 'LHPL001','LHPL002','LHPL003'... etc.
What should I have to do for that? Please let me know any possible way.
If you really need this you can achieve your goal with help of separate table for sequencing (if you don't mind) and a trigger.
Tables
CREATE TABLE table1_seq
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
CREATE TABLE table1
(
id VARCHAR(7) NOT NULL PRIMARY KEY DEFAULT '0', name VARCHAR(30)
);
Now the trigger
DELIMITER $$
CREATE TRIGGER tg_table1_insert
BEFORE INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO table1_seq VALUES (NULL);
SET NEW.id = CONCAT('LHPL', LPAD(LAST_INSERT_ID(), 3, '0'));
END$$
DELIMITER ;
Then you just insert rows to table1
INSERT INTO Table1 (name)
VALUES ('Jhon'), ('Mark');
And you'll have
| ID | NAME |
------------------
| LHPL001 | Jhon |
| LHPL002 | Mark |
Here is SQLFiddle demo
Create a table with a normal numeric auto_increment ID, but either define it with ZEROFILL, or use LPAD to add zeroes when selecting. Then CONCAT the values to get your intended behavior. Example #1:
create table so (
id int(3) unsigned zerofill not null auto_increment primary key,
name varchar(30) not null
);
insert into so set name = 'John';
insert into so set name = 'Mark';
select concat('LHPL', id) as id, name from so;
+---------+------+
| id | name |
+---------+------+
| LHPL001 | John |
| LHPL002 | Mark |
+---------+------+
Example #2:
create table so (
id int unsigned not null auto_increment primary key,
name varchar(30) not null
);
insert into so set name = 'John';
insert into so set name = 'Mark';
select concat('LHPL', LPAD(id, 3, 0)) as id, name from so;
+---------+------+
| id | name |
+---------+------+
| LHPL001 | John |
| LHPL002 | Mark |
+---------+------+
I know it is late but I just want to share on what I have done for this. I'm not allowed to add another table or trigger so I need to generate it in a single query upon insert. For your case, can you try this query.
CREATE TABLE YOURTABLE(
IDNUMBER VARCHAR(7) NOT NULL PRIMARY KEY,
ENAME VARCHAR(30) not null
);
Perform a select and use this select query and save to the parameter #IDNUMBER
(SELECT IFNULL
(CONCAT('LHPL',LPAD(
(SUBSTRING_INDEX
(MAX(`IDNUMBER`), 'LHPL',-1) + 1), 5, '0')), 'LHPL001')
AS 'IDNUMBER' FROM YOURTABLE ORDER BY `IDNUMBER` ASC)
And then Insert query will be :
INSERT INTO YOURTABLE(IDNUMBER, ENAME) VALUES
(#IDNUMBER, 'EMPLOYEE NAME');
The result will be the same as the other answer but the difference is, you will not need to create another table or trigger. I hope that I can help someone that have a same case as mine.
Here is PostgreSQL example without trigger if someone need it on PostgreSQL:
CREATE SEQUENCE messages_seq;
CREATE TABLE IF NOT EXISTS messages (
id CHAR(20) NOT NULL DEFAULT ('message_' || nextval('messages_seq')),
name CHAR(30) NOT NULL,
);
ALTER SEQUENCE messages_seq OWNED BY messages.id;
I'm trying to generate a sequence table in MySQL, so that I can get unique ids from last_insert_id.
The problem is that I need multiple sequences dynamically.
At the first, I created a table:
CREATE TABLE `sequence` (
`label` char(30) CHARACTER SET latin1 NOT NULL,
`id` mediumint(9) NOT NULL DEFAULT '0',
PRIMARY KEY (`label`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
And then tried to get the number, using example from http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
UPDATE sequence SET id = LAST_INSERT_ID(id + 1) WHERE label = 'test';
SELECT LAST_INSERT_ID();
After a while I realized that I also need to generate rows for new labels safely.
So I changed this schema into:
CREATE TABLE `sequence` (
`label` char(30) CHARACTER SET latin1 NOT NULL,
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`label`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
And I simply gave up using WHERE clause to update its id.
INSERT INTO sequence (label) values ( ? )
SELECT LAST_INSERT_ID()
Is this a proper way? I want to know if there is a better solution.
The MyISAM engine will do it for you -
Table definition:
CREATE TABLE `sequence` (
`label` char(30) CHARACTER SET latin1 NOT NULL,
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`label`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Populate table:
INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);
INSERT INTO sequence VALUES ('a', NULL);
INSERT INTO sequence VALUES ('b', NULL); -- add another labels 'b'
INSERT INTO sequence VALUES ('b', NULL);
INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);
Show result:
SELECT * FROM sequence;
+-------+----+
| label | id |
+-------+----+
| a | 1 |
| a | 2 |
| a | 3 |
| a | 4 |
| a | 5 |
| a | 6 |
| b | 1 |
| b | 2 |
+-------+----+