MySQL, given default value is not taking to the desired column - mysql

I have created a table (RJY) with default value to one of its column. When I enter the command 'Describe rjy;' its shows me default column with the value I gave,
when I insert data into my table using insert command, the column with default value is left blank and it is not taking the value which I gave. Below is my query please solve it.
mysql> insert into rjy (compy_id, reqrmnt, veh_type, go_type, weigh, l_start,l_end)
values ('123456', '111', '12 ty', 'white cemt', '1 tons', '', 'mumbai');
Here I gave default value to "l_start" field
Thank you.

Well, you give your field a value in the insert query (even it's it an empty value).
To have default value, you should change your insert as
insert into rjy (compy_id, reqrmnt, veh_type, go_type, weigh, ,l_end)
values ('123456', '111', '12 ty', 'white cemt', '1 tons', 'mumbai');

Don't include the column in your insert query. Do this:
insert into rjy (compy_id, reqrmnt, veh_type, go_type, weigh, l_end)
values ('123456', '111', '12 ty', 'white cemt', '1 tons', 'mumbai');

Related

Column count doesn't match value count in MySQL

Whenever I am trying to insert data in table:
INSERT INTO `operator`(`id`, `operator_name`, `email`, `info`)
VALUES (1,'Bangalink','This is all about Banglalink'),
(2, 'Robi', 'This is all about Robi');
MySQL said:#1136 - Column count doesn't match value count at row 1
How can i fix it?
I don't understand what need to do.
TIA
The provided number of columns in the column list specification and number of column values in each record must match.
Assuming you don't want to insert email data, remove that from the column list:
INSERT INTO `operator`(`id`, `operator_name`, `info`)
VALUES (1,'Bangalink','This is all about Banglalink'),
(2, 'Robi', 'This is all about Robi');
or pass null for email:
INSERT INTO `operator`(`id`, `operator_name`, `email`, `info`)
VALUES (1,'Bangalink',null,'This is all about Banglalink'),
(2, 'Robi', null,'This is all about Robi');
Second method is useful when you may have emails for few records.
You pass one item - info:
INSERT INTO `operator`(`id`, `operator_name`, `email`, `info`)
VALUES (1,'Bangalink','This is all about Banglalink', NULL),
(2, 'Robi', 'This is all about Robi',NULL);
or
INSERT INTO `operator`(`id`, `operator_name`, `email`)
VALUES (1,'Bangalink','This is all about Banglalink'),
(2, 'Robi', 'This is all about Robi');

Default values not working in phpmyadmin/mysql database

I can't get a table to accept "" or '' and use the default value. It is inserting NULL instead.
I am trying these commands in the direct input sql window.
INSERT INTO test01 VALUES ("", now(), "");
INSERT INTO test01 VALUES ('', now(), '');
But both just give NULL in the 3rd column. The structure is set to non-null with a default value of "yes". (Without quotation marks).
Here is a screenshot of the structure. You can see NULL is not checked.
http://garryjones.se/extras/so3.png
Default values only work if no value is inserted/updated. If you explicitly set it to an empty string (which is NOT the same as a NULL value) then it will end up with an empty string in the column. Instead of the code above you should eliminate the column from the INSERT statement at all:
INSERT INTO test01 (t1, t2) VALUES ('', now())
Other is already explain the reason here I am adding one more point you are also using current time stamp on update so do not need to use this column as well.
INSERT INTO test01 (t1) VALUES ('')
You could use the DEFAULT keyword: INSERT INTO test01 VALUES ("", now(), DEFAULT);

Making a Conditional MySQL Insert statement

So I have this table named SAKAI_REALM_RL_FN that has 3 fields
REALM_KEY
ROLE_KEY
FUNCTION_KEY
What this statement needs to do is that if a certain 2 combinations of ROLE_KEY & FUNCTION_KEY don't exist for each REALM_KEY, than do an insert.
I was already taking a look at this StackOverflow post
I also have the query I was using for the singular inserts:
INSERT INTO `sakai`.`SAKAI_REALM_RL_FN` (`REALM_KEY`, `ROLE_KEY`, `FUNCTION_KEY`) VALUES (248620, 8, 308);
Psuedo-Code:
if(ROLE_KEY equals 8 and FUNCTION_KEY=308 don't exist for REALM_KEYS)
than insert ROLE_KEY=8 & FUNCTION_KEY=308
INSERT INTO `sakai`.`SAKAI_REALM_RL_FN` (`REALM_KEY`, `ROLE_KEY`, `FUNCTION_KEY`)
SELECT *primaryKey*
FROM `sakai`.`SAKAI_REALM_RL_FN`
WHERE not exists (SELECT *primaryKey*
from `sakai`.`SAKAI_REALM_RL_FN`
where role_key = 8 and function_key = 308);
Hope that helps...
I wasn't quite sure what you wanted, but here's something that you might find useful.
Schema with few entries:
CREATE TABLE ALOHA (
REALM_KEY VARCHAR(32) NOT NULL,
ROLE_KEY VARCHAR(32) NOT NULL,
FUNCTION_KEY VARCHAR(32) NOT NULL
);
INSERT INTO ALOHA VALUES ('1', '1', '1');
INSERT INTO ALOHA VALUES ('1', '1', '2');
INSERT INTO ALOHA VALUES ('1', '2', '1');
INSERT INTO ALOHA VALUES ('1', '2', '2');
INSERT INTO ALOHA VALUES ('1', '2', '3');
INSERT INTO ALOHA VALUES ('1', '2', '4');
Try to insert 3 entries (only one gets inserted):
INSERT INTO ALOHA (REALM_KEY, ROLE_KEY, FUNCTION_KEY)
SELECT * FROM (
SELECT '1' AS REALM_KEY, '2' AS ROLE_KEY, '1' AS FUNCTION_KEY
UNION ALL
SELECT '1', '2', '3'
UNION ALL
SELECT '1', '2', '5'
) s
WHERE NOT EXISTS
(SELECT 1 FROM ALOHA a
WHERE a.ROLE_KEY = s.ROLE_KEY
AND a.REALM_KEY = s.REALM_KEY
AND a.FUNCTION_KEY = s.FUNCTION_KEY);
The RDBMS is well-equipped to handle this, if you define the correct index.
Sounds like what you need is a compound UNIQUE index across all three columns. When you perform an INSERT IGNORE, the combination will be inserted if it does not already exist.
Note that this will fail if you already have non-unique rows in your table.
ALTER TABLE SAKAI_REALM_RL_FN ADD UNIQUE KEY `idx_unique_realm_role_function` (REALM_KEY, ROLE_KEY, FUNCTION_KEY)
Then the INSERT selects all the REALM_KEY values and static values for the other 2 columns. If the values already exist, they're ignored. Otherwise they are inserted along with the REALM_KEY.
INSERT IGNORE INTO SAKAI_REALM_RL_FN (REALM_KEY, ROLE_KEY, FUNCTION_KEY)
/* SELECT within INSERT gets all REALM_KEY plus the 2 static values */
SELECT
REALM_KEY,
8,
308
FROM SAKAI_REALM_RL_FN
Here's a demo
When you have completed the INSERT IGNORE, you can drop the UNIQUE KEY since it may no longer be needed.
ALTER TABLE SAKAI_REALM_RL_FN DROP KEY `idx_unique_realm_role_function`

Storing numbers in a MySQL SET column

How should I insert numbers in a SET column? I know that for ENUM it's recommended to not store numbers in ENUM column, and numbers should be insert with ''.
Is it the same for a SET column? Should be numbers inserted with ''? Like '1','2' or is it fine to insert it like: 1, 2 ?
You insert using SET values names and enclose them in single quotes and separate them with commas.
Minimalistic example from here:
CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
INSERT INTO myset (col) VALUES ('a,d,d,s');
First of all, don't use MySQL's SET and ENUM column types.
Second, your set definition really shouldn't contain numbers. 'One' is OK, '1' will be extremely confusing.
Third, if you insert numerals like 1, 2, etc then it will actually insert the n-th member of the set instead of that number, whereas if you insert '1' it will insert that member. The difference comes up when you have a set with the following members in that order: 'foo', 'bar', '1', '2' - if you insert '1' it would be the same as inserting a 3, but if you insert a 1 it'd be the same as 'foo'.
Bottom line: don't use the SET and ENUM column types.
Number should insert like 1 ,2 ,3.
This consider as character if you put into single quote like '1','2'.

Error in SQL Statement using INSERT and AUTO-INCREMENT column

INSERT INTO `configuration` VALUES ('', 'News Box Character Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);
I run this command in phpMyAdmin, it shows
#1366 - Incorrect integer value: '' for column 'configuration_id' at row 1
configuration_id is an auto increment field beginnning of 1
Instead of this INSERT INTO configuration VALUES ('', 'News Box Character Count',
Pass the value as NULL for auto_increment or integer column or you can simply not to include that column in sql query.
INSERT INTO `configuration` VALUES (NULL, 'News Box Character Count', ...
This is because, mysql is running in the strict mode.
You can either use NULL for all the integer columns when there is nothing to enter them or turn off the MySql Strict mode.
For an autoincrement in MySQL, either insert NULL or insert nothing at all:
Easiest & Cleanest: using NULL
INSERT INTO `configuration` VALUES (NULL, 'News Box Character Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);
More work: name every column except the autoincrement one
INSERT INTO `configuration` (every,column,except,the,first) VALUES ('News Box Charac`ter Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);`
It seems pretty obvious to me? You gave an integer column and you are explicitly inserting a string (although it's empty). If the column is set to auto increment, remove the first value ('') from your values array and you should be fine. Also, maybe you wanto to specify the columns you are inserting values for, like:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
For the AUTO_INCREMENT field you can set NULL; it will generate new value automatically.
For example -
INSERT INTO table_name VALUES(NULL, 'value', ...)...
If the column is auto-increment then you can simple omit it from the INSERT statement.
Replace the '' value with NULL ('' isn't a valid integer). See here.
You should always use column names when inserting. So just don't have to insert into the column configuration_id and your sql statement doesn't break when changing the columns.
Use something like this: INSERT INTO table (column1, column2) VALUES (value1, value2);