MySQL command operations in Linux

Source: Internet
Author: User

Create databases/tables in batches using Linux shell

The Shell script is as follows:

  1. # Create database and table
  2. HOST ='Localhost'
  3. PORT ='123'
  4. USER ='Root'
  5. PWD =''
  6. DBNAME ='Top123'
  7. TABLENAME ='Gametop800'
  8. Mysql_login =''
  9. Mysql_create_db =''
  10. Mysql_create_table =''
  11. Function mysql_create (){
  12. Echo"Login mysql $ HOST: $ PORT ..."
  13. Mysql_login ="Sudo mysql-h $ HOST-P $ PORT-u $ USER"# Mysql-h host-P port-u root-p pwd
  14. Echo | $ {mysql_login}
  15. If[$? -Ne0]; Then
  16. Echo"Login mysql $ {HOST }:$ {PORT} failed .."
  17. Exit1
  18. Fi
  19. Echo"Create database $ DBNAME ..."
  20. Mysql_create_db ="Create database if not exists $ DBNAME"
  21. Echo $ {mysql_create_db} | $ {mysql_login}
  22. If[$? -Ne0]; Then
  23. Echo"Create db $ {DBNAME} failed .."
  24. Exit1
  25. Fi
  26. Echo"Create table $ TABLENAME ..."
  27. Mysql_create_table = "create table $ TABLENAME (
  28. Id char (50)NotNull,
  29. Top int,
  30. Name char (100),
  31. Category char (50),
  32. Rating float,
  33. Ratingcount char (20),
  34. Download char (30),
  35. Price char (20),
  36. Publishdate char (20),
  37. Version char (40),
  38. Filesize char (40),
  39. RequireAndroid char (40),
  40. Contentrating char (40),
  41. Country char (10),
  42. Dtime datetimeNotNull,
  43. Primary key (id, dtime)
  44. )"
  45. Echo $ {mysql_create_table} | $ {mysql_login }$ {DBNAME}
  46. If[$? -Ne0]; Then
  47. Echo"Create table $ {TABLENAME} fail ..."
  48. Exit1
  49. Fi
  50. Echo"Create table $ {TABLENAME} success! "
  51. }

After the script is executed, the result is as follows:



MySQL slave common operation commands:

Show Database

ShowDatabases;

Switch Database
UseTop123;

Show database tables

ShowTables;

Modify primary key

Alter table gametop800 add primary key (Id);

Delete primary key

Alter table gametop800DropPrimary key;

Add composite primary key

Alter table gametop800 add primary key (Id, dtime);


If you are interested, you can use more complex syntaxes such as awk, cut, paste, regular expressions, and database operations to complete MySQL database operations.

The following describes how to capture webpages, extract features, extract information, summarize results, and insert data into databases in batches for permanent storage.

Example:

Related Article

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.