Common operations for Databases in Linux

Source: Internet
Author: User

1-Connect to the database:

- - -P
 xxxxx @xxx : ~  $ mysql - h localhost - u jiangbiao -  penter password:welcome  to  the  MySQL Monitor. Commands end  with ; or   \g.your MySQL Connection ID  is  2444832  server version:  5.5 . 40  - log  Source Distribution 

2-Display the table information, including the engine used, for mysql , only the InnoDB engine supports trigger actions

CREATE TABLE tbl_buniness;

3-Display the database :

show databases;

4-Using the database:

 use DatabaseName;

5-Show data table:

Show tables;

6-Show the specific table structure:

Describe TableName;

7-Insert Operation:

INSERT  into' Root_trigger ' (' id ', ' data ')VALUES('1','test root Line 1'),         ('2','test root Line 2'),         ('3','test root Line 3');

8- DELETE CASCADE Handle the Cascade delete operation through the processing mechanism of the FOREIGN KEY constraint:

--Create a Test main table. ID is the primary key.CREATE TABLETest_main (IDINT    not NULL, ValueVARCHAR(Ten),  PRIMARY KEY(ID)); --Create a Test sub-table.CREATE TABLEtest_sub (IDINT   not NULL, main_idINT, ValueVARCHAR(Ten),  PRIMARY KEY(ID)); --Insert test Master table data.INSERT  intoTest_main (ID, value)VALUES(1,' One');INSERT  intoTest_main (ID, value)VALUES(2,' Both'); --Insert Test sub-table data.INSERT  intoTest_sub (ID, main_id, value)VALUES(1,1,'Oneone');INSERT  intoTest_sub (ID, main_id, value)VALUES(2,2,'Twotwo');-----Handle by the processing mechanism of the DELETE CASCADE foreign KEY constraintMysql> ALTER TABLEtest_sub -   ADD CONSTRAINTmain_id_cons -   FOREIGN KEY(main_id) -   REFERENCESTest_main (ID) -    on DELETE CASCADE//Query OK,2Rows Affected (0.16sec) Records:2Duplicates:0Warnings:0MySQL> DELETE  from     -Test_main - WHERE     -Id= 1;  - //Query OK,1Row affected (0.02sec) MySQL> SELECT     -   *     -  from     -test_sub;  - //+----+---------+--------+|Id|main_id|Value|+----+---------+--------+|  2 |       2 |Twotwo|+----+---------+--------+1Rowinch Set(0.00Sec

9-If you want to use a trigger to handle, here is an example:

--Create a Test main table. ID is the primary key.CREATE TABLET_test_main (IDINT    not NULL, ValueVARCHAR(Ten),  PRIMARY KEY(ID)); --Create a Test sub-table.CREATE TABLEt_test_sub (IDINT   not NULL, main_idINT, ValueVARCHAR(Ten),  PRIMARY KEY(ID)); --Insert test Master table data.INSERT  intoT_test_main (ID, value)VALUES(1,' One');INSERT  intoT_test_main (ID, value)VALUES(2,' Both'); --Insert Test sub-table data.INSERT  intoT_test_sub (ID, main_id, value)VALUES(1,1,'Oneone');INSERT  intoT_test_sub (ID, main_id, value)VALUES(2,2,'Twotwo'); DELIMITER// CREATE TRIGGERTr_t_test_main_del beforeDELETE  onT_test_main forEach ROWBEGIN  DELETE  fromT_test_subWHEREmain_id=old.id;END;//DELIMITER; MySQL> Delete  fromT_test_mainwhereId= 1; Query OK,1Row affected (0.01sec) MySQL> Select *  fromt_test_sub;+----+---------+--------+|Id|main_id|Value|+----+---------+--------+|  2 |       2 |Twotwo|+----+---------+--------+1Rowinch Set(0.00Sec
  about 8, 92 points  Original link: https://zhidao.baidu.com/question/562372469.html

10-Other:
Here is a blog link that has been organized

Common operations for Databases in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.