How to insert a binary value into mysql - mysql

Is there a flag to indicate that data is of binary type, or does that need to be converted to another type. For example, is there a way to do:
INSERT INTO mytable (number) VALUES (0000 0010)
Instead of:
INSERT INTO mytable (number) VALUES (2)
I know the above is a trivial example but basically I'm wondering if you can type a binary/hex value directly into a query.

You can use a bit-value literal
INSERT INTO mytable (number) VALUES (b'00000010');
or
INSERT INTO mytable (number) VALUES (0b00000010)

Related

SQL Patch that will change all INT values to JSON

I have a table called photoProcess. There is a column within this table photoProcess.storyId.
Currently, all values within the storyId column are being stored as a single integer. I am changing the column type to VARCHAR so that I can store the values as JSON. My question is how I can write a MySQL patch that will change all storyId values of these values to a JSON string.
Example:
Original photoProcess.storyId value for the first row is 6649.
I'd like to change that value to ["6649"] and do so on a large scale for all others.
Use CONCAT:
UPDATE photoProcess SET storyId = CONCAT('["', storyId , '"]')

Adding only one value to the table in sql

I have a table named student which consists of (rollno, name, sem, branch)
If I want to INSERT only one value (i.e only name has to be entered) what is the query?
To insert values into specific columns, you first have to specify which columns you want to populate. The query would look like this:
INSERT INTO your_table_name (your_column_name)
VALUES (the_value);
To insert values into more than one column, separate the column names with a comma and insert the values in the same order you added the column names:
INSERT INTO your_table_name (your_column_name_01, your_column_name_02)
VALUES (the_value_01, the_value_02);
If you are unsure, have a look at W3Schools.com. They usually have explanations with examples.
insert into student(name) values("The name you wan to insert");
Be careful not to forget to insert the primary key.
First, if the table is all empty, just wanna make the column 'name' with values, it is easy to use INSERT INTO. https://www.w3schools.com/sql/sql_insert.asp.
`INSERT INTO TABLE_NAME (COLUMN_NAME) VALUES ("the values")`
Second, if the table is already with some values inside, and only wanna insert some values for one column, use UPDATE. https://www.w3schools.com/sql/sql_update.asp
UPDATE table_name SET column1 = value1 WHERE condition;
insert into student (name)
select 'some name'
or
insert into student (name)
values ('some name')
Following works if other columns accept null or do have default value:
INSERT INTO Student (name) VALUES('Jack');
Further details can be found from the Reference Manual:: 13.2.5 INSERT Syntax.
Execute this query, if you want the rest of columns as "#", do insert # inside the single quote which is left blank in query.
Also, there is no need of defining column name in the query.
insert into student values('','your name','','');
Thanks...

mysql DOUBLE with thousands separator

Is there any way to convert string to DOUBLE format when inserting new record, I want to strip it out or ignore if it exists in value when do insert in database.
INSERT INTO table1 (measure) VALUES val1
val1 can contain 20000 or 20,000. Can I do it with mysql?
You must first remove the separators by using REPLACE.
INSERT INTO table1 (measure) VALUES (REPLACE(val1, ',', ''))
As njk mentioned, you can use REPLACE function, but to ignore if it exists in value, you use INSERT IGNORE.
INSERT IGNORE INTO table1 (measure) VALUES (REPLACE(val1, ',', ''))
The IGNORE keyword will not insert the new record if the value is found ONLY IF the column has an unique index defined or a primary key.
http://dev.mysql.com/doc/refman/5.5/en/insert.html
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

INSERT values to MySQL

If i have my SQL create statement as follows;
CREATE TABLE TABLENAME12
(
TAB_ID INT NOT NULL AUTO_INCREMENT,
NAME_FIRST NVARCHAR(200),
TYPE NVARCHAR(200),
PRIMARY KEY( TAB_ID )
);
and if i want to Insert values to it, should i enter TAB_ID too ? Since it's auto increment.
When i INSERT INTO SWM_SALES_FEEDBACK VALUES (1,'Jerry','ty'); It gets inserted if i don't specify the primary key INSERT INTO SWM_SALES_FEEDBACK VALUES ('Jerry','ty'); i get the following error :
ERROR 1136 (21S01): Column count doesn't match value count at row 1
1.) What is the point of having AUTO_INCREMENT if it doesn't get auto incremented.
2.) If i have a field called:
BIRTH_TIME DATE,
How should i INSERT value to this field since it's DATE type
1) You have to respect an order of columns in your table. You can do either:
INSERT INTO SWM_SALES_FEEDBACK VALUES (null, 'Jerry','ty');
or
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST, TYPE) VALUES ('Jerry','ty');
2) You can use '2012-07-24' format for date column.
With an auto increment, you need to specify your columns
INSERT INTO tablename12 ('name_first','type') values ('Jerry','ty')
If you aren't inserting into every column, you need to tell the query which columns you want to insert into. try INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');
Not sure if mysql has a to_date() function in the new releases:
But, you can use this : Just enclose the date time field in single quotes. You can specify the date time formats
INSERT INTO table_name (birth_date) VALUES ('2008-07-04')
For other date format questions refer this.
For your auto increment, specify the columns
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');
Point 1
If you don't want to store all fields in DB then you have to specify those column names before VALUES clause as shown below.
INSERT INTO myTable (field2,field3) VALUES ('field2','field3')
In your case, statement should be
INSERT INTO SWM_SALES_FEEDBACK (name_first, type) VALUES ('Jerry','ty')
^^^^^^^^^^^^^^^^^^
Point 2
If you have field as DATETIME, TIMESTAMP as datatype, you can enter date as
INSERT INTO myTable (myDate) VALUES ('2012-12-28 12:12:12')
Format is yyyy-mm-dd hh:mm:ss
If you want to store the current time of the system in DB, you could use NOW() as shown below.
INSERT INTO myTable (myDate) VALUES (NOW())
Try the following code
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');

REPLACE into keeping a value

I want to make a replace into in a table where cust_id is the primary key, but I do not want to modify the date field. So, a normal insert on this table would look like:
REPLACE INTO emails(cust_id, email, date)
VALUES(55, 'email#email.com', '2011-08-07 00:00');
Now, without having to modify the date field, it would be something such as:
REPLACE INTO emails(cust_id, email, date)
VALUES(55, 'email#email.com', date.value?);
But how do I exactly keep the date value?
Short answer, You can't keep the dates that way. from Mysql documentation
Values for all columns are taken from the values specified in the
REPLACE statement. Any missing columns are set to their default
values, just as happens for INSERT. You cannot refer to values from
the current row and use them in the new row
perhaps you want to use http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html instead.
insert ignore will skip insertion if any duplication
if you need to update certain fields ,
you can do
insert into some_table values (...)
on duplicate update email=?;