I am using VBA to write some values to MySQL table as rows. However, for certain columns, I want to leave them blank so that the default value of NULL can be assumed by the database.
I need to specify all column values in my SQL query. Can I write something like this (two consecutive commas represent a blank column):
SQLStr = "insert into table tablename values (12,13,19,,,,,1,,2,,5)"
You use NULL:
insert into table tablename
values (12, 13, 19, NULL, NULL, NULL, NULL, 1, NULL, 2, NULL, 5);
You can also use '' if the column is a string and by "blank" you mean "empty string".
I should add . . . you should always list the columns you are inserting, so it is better to write this as:
insert into table tablename(col1, col2, col3, col8, col10, col12)
values (12, 13, 19, 1, 2, 5);
(You don't specify what the column names are.)
By default, SQL will put NULL values in for the missing columns -- this can be prevents (using NOT NULL) or another value used (using DEFAULT).
Related
I wanted to add an entry inside a table in SQL database.
For example I have the following Database
CREATE TABLE `distributor_geneology` (
`distributor_gen_id` bigint(20) NOT NULL,
`user_id` varchar(24) NOT NULL,
`id` bigint(20) NOT NULL,
`sponsor_id` bigint(20) NOT NULL,
`rank` tinyint(4) NOT NULL
);
And I want to add an entry in sponsor_id or say id inside a database.
First, I imported the database in my SQL Workbench then In my SQL Workbench, I ran a command select * from distributor_geneology which gave me
Error Code: 1146. Table 'dba_db.distributor_genelogy' doesn't exist
[Question] How can I create/add Entry for ID (or sponsor ID or any other filed)?
One typical way which data would enter a MySQL database is via an INSERT statement:
INSERT INTO distributor_geneology (distributor_gen_id, user_id, id, sponsor_id, rank)
VALUES
(1, 1, 1, 1, 1);
I am inserting 1 everywhere, but you may alter the tuple with the values you want.
Another way to get data into a table is bulk loading via LOAD DATA.
For your first part of your question which is "Add an entry to inside table"
this operation called insertion in the database and the keyword database used to insert data is insert into
It is possible to write the INSERT INTO statement in two ways:
1- specifies both the column names and the values to be inserted
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
you can rearrange the columns orders as you want but must the values be the same order of the columns and you can let any column null if you don't want to insert any data in this column but be careful if you have not null column you must insert in you query
in your case, all the columns you have are not null.
2- if you do not need to specify the column names in the SQL query. make sure the order of the values is in the same order as the columns in the table
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
For your second part of your question which is "Error Code: 1146. Table 'dba_db.distributor_genelogy' doesn't exist"
First, ensure you imported the DB correctly and if yes > write try to use DB name in your query.
select * from DB_Name.Table_Name
Edit:
Try this query format
INSERT INTO distributor_geneology (distributor_gen_id, user_id, id, sponsor_id, rank)
VALUES
(10, '10', 10, 10, 10);
please note I put second value between 2 quotes because you are defining the user_id as varchar which means not an integer so we should put it between qouts
I have a table with something like 100 columns and now I would like to insert a row with some columns equal to some values and default values for all other columns. Is it possible?
Something like:
insert into tablename (col1, col22, col33) values (1, 2, 3)
The columns not listed in the column list will be assigned to their default values.
You can also do:
insert into tablename values (1, default, 3, default, 5, ..., 100)
But nothing I'd do with 100 columns. Too easy to make mistakes.
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'.
I am trying go select multiple values with CASE statement. I noticed we cannot do
CASE
WHEN wall.type="bk"
THEN books.id1,books.id2, // and so on
END as column_1,
Is there a way to do THEN with multiple columns or do we need to simply write a bunch of CASE THEN statements? that seems messy
No, it is just a single value. Additionally, it is contradictory to use "multiple columns" and name those multiple columns as column_1, right? :)
You can use another column to store the other id with (a similar case) and use nulls to represent the else values, just like you're doing now.
Example:
CASE
WHEN wall.type="bk"
THEN books.id1
END as column_1,
CASE
WHEN wall.type="bk"
THEN books.id2
END as column_2
Check the official documentation for more information.
No. CASE statement can only return a single value, so the only way to achieve what you want is duplicate the case ...
The database server should be optimized and perform only one time the check on the same condition ...
And everything can be done, but it always depends on what you want to do. Below I'll show you a working example right after you have to take the data as an array and do what you want.
CREATE TABLE wall (`ident` int,`type` varchar(2), `order` int);
INSERT INTO wall (`ident`, `type`, `order`) VALUES
(40,'bk', 1),
(41,'bk', 5),
(42,'rt', 2),
(43,'bk', 3),
(44,'rt', 1);
CREATE TABLE books (`ident` int,`id1` int, `time` varchar(8), `id2` int);
INSERT INTO books (`ident`, `id1`, `time`, `id2`) VALUES
(40, 10, '18:07:00', 20),
(43, 11, '05:00:00', 21),
(44, 12, '21:01:00', 22),
(41, 13, '10:00:00', 23),
(42, 14, '23:10:00', 24);
#--------------------------
SELECT
CASE
WHEN wall.type='bk'
THEN CONCAT(books.id1,'-',books.id2)
END AS column_1
FROM wall JOIN books ON books.ident = wall.ident GROUP BY wall.ident ORDER BY wall.ident ASC;
Print:
column_1
1 10-20
2 13-23
3 NULL
4 11-21
5 NULL
Solution in action via this link:
http://rextester.com/LHPI38373
According to documentation mysql_doc; you can use you can use the other syntax of case for what you wanted.
case
WHEN FIND_IN_SET(edu, "1,1st,2,2nd,3,3rd,4,4th,5,5th,pri,primary,primery") THEN
SET memEdu = "Primary";
WHEN FIND_IN_SET(edu, "intermediate,inter,f.a,fa,fac,f.a.c,f.sc,fsc,1rd year,2rd year,3rd year") THEN
SET memEdu = "Intermediate";
End case;
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);