sqlref.txt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. docker search mysql
  2. docker pull mysql
  3. docker images
  4. docker run -d --name=msqdb -e MYSQL_ROOT_PASSWORD='hjkl;' -v /storage/msqdb/mysql-datadir:/var/lib/mysql mysql
  5. docker ps -a
  6. docker start msqdb
  7. docker ps
  8. docker exec -it msqdb bash
  9. cat /etc/os-release
  10. service --status-all
  11. apt-get update
  12. apt-get install openssh-server openssh-client
  13. service ssh status
  14. service ssh start
  15. useradd -m raman
  16. set password for user raman:
  17. passwd raman
  18. [hjkl;']
  19. apt-get install sudo vim
  20. vi /etc/passwd
  21. raman:x:1000:1000::/home/raman:/bin/sh
  22. raman:x:1000:1000::/home/raman:/bin/bash
  23. sysvinit - /etc/init.d/mysql
  24. systemd - /lib/systemd/sytem/mysql.service
  25. Relational Database Management System - RDBMS
  26. For help with using MySQL, please visit the MySQL Forums, where you can discuss your
  27. issues with other MySQL users.
  28. mysql -u root -p
  29. mysql -u user -p
  30. mysql -u user -p dbname
  31. select version(), current_date;
  32. # Creating a Database and Tables
  33. CREATE DATABASE bookstore;
  34. SHOW DATABASES;
  35. USE bookstore
  36. CREATE TABLE books (book_id INT,
  37. title VARCHAR(50),
  38. author VARCHAR(50) );
  39. DESCRIBE books;
  40. # Analyzing and Manipulating Data
  41. SELECT COUNT(*)
  42. FROM books
  43. JOIN authors USING(author_id)
  44. WHERE author_list = 'Tolstoy';
  45. # sqlite - use SQL without a database server
  46. # Relational Database Concepts
  47. A database server can contain many databases
  48. Databases are collections of tables
  49. Tables are two-dimensional with rows (observations) and columns (variables)
  50. Limited Mathematical and summary operations available
  51. Very good at combining information from several tables
  52. SHOW DATABASES;
  53. SHOW TABLES IN database;
  54. SHOW COLUMNS IN table;
  55. DESCRIBE table; # shows the columns and their types
  56. # Variable Types
  57. SQL supports a very large number of different formats for internal storage
  58. of information.
  59. Numeric - INTEGER, SMALLINT, BIGINT
  60. NUMERIC(w,d), DECIMAL(w,d) - numbers with width w and d decimal places
  61. show databases;
  62. use <databasename>;
  63. show tables in <databasename>;
  64. show columns in <tablename>; is equvalent to "describe <tablename>;"
  65. show rows in <tablename>; # results in error
  66. create table childrens(id char(6),
  67. race smallint,
  68. age decimal(6,3),
  69. height decimal(7,3),
  70. weight decimal(7,3),
  71. sex smallint);
  72. insert into childrens values(100011, 2, 10.346, 148.5, 38.95, 1);
  73. We can automate the process using the LOAD DATA command:
  74. load data infile 'childrens.tab'
  75. into table childrens
  76. fields terminated by '\t';
  77. This will read an entire tab-separated file into the database in one command
  78. select * from childrens; # to see all of the data in table childrens
  79. # Created Database: library and Table: booklist
  80. Reference: MYSQL_in_a_Nutshell.pdf
  81. create database library;
  82. use library;
  83. create table booklist(
  84. subject varchar(50),
  85. title varchar(75),
  86. author varchar(50));
  87. describe booklist; # similar to show columns in booklist;
  88. alter table booklist
  89. change column subject book_id int auto_increment primary key,
  90. change column title subject varchar(20),
  91. change column author title varchar(75),
  92. add column author varchar(50),
  93. add column edition varchar(20);
  94. show databases;
  95. show tables;
  96. insert into booklist
  97. (subject, title, author, edition)
  98. values('Linux', 'Introduction to Linux', 'Machtelt Garrels', '1.27 Edition');
  99. select * from booklist;
  100. select book_id, title, author, edition
  101. from booklist
  102. where subject = 'Linux';
  103. select count(*)
  104. from booklist
  105. where subject = 'Linux';
  106. update booklist
  107. set edition = '1 Edition'
  108. where book_id = '2';
  109. delete from booklist
  110. where author = ' Michael Stutz';
  111. # database schema
  112. show columns from clients like 'c%i%';
  113. alter table clients
  114. change column city client_city varchar(100) character set 'latin2',
  115. modify column client_state char(4) after client_city;
  116. # add column in a table
  117. alter table <table_name> add column <column_name> <data_type>;
  118. # set same value to all rows in a column
  119. update <tabel_name> set <column_name> = 'value' where 1 = 1;
  120. # rename a column
  121. alter table <table_name> change <old_column> <new_column> <data_type>;
  122. # drop a column from a table
  123. alter table <table_name> drop column <column_name>;
  124. # list first 10 records from a table
  125. select * from <tablename> limit 10; # select rows 1 to 10
  126. select * from <tablename> limit 0, 10; # select rows 01 to 10
  127. select * from <tablename> limit 10, 10; # select rows 11 to 20
  128. select * from <tablename> limit 10 offset 10; # select rows 11 to 20
  129. # execute sql script in MySQL
  130. mysql> source /home/raman/Scripts/datadef.sql