MySql Update table column from another table colunm

Updating a table fields from another table columns we can user following syntax

There are two tables table1 and table2. Both table have common id value. To select appropriate field from table inner join in used and updating table1 fields from table2 fields set statement is used.

For example there are two tables with following fields
user(id,fname,lname,mobileno,address,);
personal(id,fname,lname,mobile_no);

Here c=consider user and person have common id then to update user from person we can use following query

This will update user table with person tables fields.

updating user login password

SET PASSWORD FOR ‘user’@’localhost’ = PASSWORD( ‘NewPassword’ );

SHOW VARIABLES LIKE ‘validate_password%’;

# SET GLOBAL validate_password_length = 5;
# SET GLOBAL validate_password_number_count = 0;
# SET GLOBAL validate_password_mixed_case_count = 0;
# SET GLOBAL validate_password_special_char_count = 0;

Providing default value for column

We can provide default value for a column. If we don specify its value then it will automatically assign a default value.
We can specify default value on table creation.

Syntax for adding default value is

for example

this will produce following output

while inserting any rowin table if we dont provide value for column that have default value that its default value will be inserted for example

We have inserted only name of product and value of quantity,price,status and add_date is assigned as default value.

Date and Time

There are various pre defined function for data and time we will see them one by one.

If you want to show current time then we use CURTIME().It returns time in HH:MM:SS string format

below is HHMMSS in numeric format

To select current data we use CURDATE().It returns data in YYYY-MM-DD string format.

to convert string in to number we use following query

To select current data and time we use NOW() whch returns data and time in ‘YYYY:MM:DD HH:MM:SS’ in string format or YYYYMMDDHHMMSS is numeric format.

Finding all column name of table

Suppose we have a table user and we want to fetch all its column name.
For this we use INFORMATION_SCHEMA.

we will use following query to fetch all column name of table

Above query will fetch all columns of user table available in social database.

Mysql create table statement

To create table in mysql following syntax is used

For example,

if table created without any error then it will show following statement

Result:

This query will result a table with the above fields. The first column will hold number while other columns are type of varchar.

Create Table in Mysql with primary key

Above mysql create statement create a student table with primary key id,
with auto increment field value and two unique keys mobile_no and email_id,
at the time of creating user add_date is added CURRENT_TIMESTAMP as default value.

This table also uses mysql create table auto_increment

MySql create table if not exists

Syntax of create table if not exist is
CREATE TABLE IF NOT EXISTS table_name(............);

To modity the student table with CREATE TABLE IF NOT EXISTS the query is as below

Changing the default value for column

After create table if you want to change default value for any column of table the we can use alter command in following way

Syntax

Example

above query will set default value for column_name to provided default value.

Updating the column data size

To create a table

after creating table we want to increase the size of description field from varchar(20) to varchar(100).

for that we will use alter table command

Syntax

Example

we can also decrease the size of column but it may cause the data loss.
like

Setting innodb_lock_wait_timeout

What is innodb_lock_wait_timeout ?

In MySql innodb_lock_wait_timeout is an Innodb transaction wait time in seconds for a row lock.

An another transaction has to wait for specified innodb_lock_wait_timeout if a transaction is taking place. 

innodb-lock-wait-timeout in a system variable in global and session scope this variable can set dynamically to both scopes.

We can set this value as integer from 1 to 1073741824 by default its value is 50.

You can check default value of innodb-lock-wait-timeout by query

show variables like 'innodb_lock_wait_timeout';

OR

SHOW GLOBAL VARIABLES LIKE '%INNODB_LOCK_WAIT_TIMEOUT%';

OR

SELECT @@innodb_lock_wait_timeout;

innodb_lock_wait_timeout-min

All shows the default value.

Property Value for innodb_lock_wait_timeout

PropertyValue
Command-Line Format--innodb-lock-wait-timeout=#
System Variable innodb-lock-wait-timeout
ScopeGlobal, Session
DynamicYes
TypeInteger
Default Value50
Minimum Value1
Maximum Value1073741824

Show affected tables

Get affected tables you can use following query

SHOW ENGINE INNODB STATUS\G

It shows locking information

When to increase or Decrease  innodb-lock-wait-timeout value ?

You can descrese innodb_lock_wait_timeout for OLTP or High interactive systems.

If you have any high time consuming operation long queries in Data-ware house then you can increase its time

A transaction that tries to access a row that is locked by another InnoDB transaction waits at most this many seconds for write access to the row before issuing the following error:

When a lock wait timeout occurs, the current statement is rolled back (not the entire transaction).

How to update innodb-lock-wait-timeout value ?

to show default value of innodb_lock_wait_timeout we use

innodb_lock_wait_timeout can be set at run time with global and session scope

SELECT @@innodb_lock_wait_timeout;

will show innodb_lock_wait_status

innodb-lock-wait-timeout value

Show engine innodb status

InnoDB Monitors provide information about the InnoDB internal state. This information is useful for performance tuning. Each Monitor can be enabled by creating a table with a special name, which causes InnoDB to write Monitor output periodically. Also, output for the standard InnoDB Monitor is available on demand through the SHOW ENGINE INNODB STATUS SQL statement.

There are several types of InnoDB Monitors:

The standard InnoDB Monitor displays the following types of information:

  1. Table and record locks held by each active transaction
  2. Lock waits of a transactions
  3. Semaphore waits of threads
  4. Pending file I/O requests
  5. Buffer pool statistics
  6. Purge and insert buffer merge activity of the main InnoDB thread

To enable the standard InnoDB Monitor for periodic output, create a table named innodb_monitor. To obtain Monitor output on demand, use the SHOW ENGINE INNODB STATUS SQL statement to fetch the output to your client program. If you are using the mysql interactive client, the output is more readable if you replace the usual semicolon statement terminator with \G:

Refrence: Link