未加星标

MySQL Quick Start and Cheat Sheet

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Although my favorite database is PostgreSQL, mysql still has its uses, especially that Google Cloud SQL is only compatible with MySQL. Let’s do a quick start, and compile a cheat sheet afterwards.## Quick Start

service mysql start # start mysql if not started mysql -u root -p # login with password prompt Setup User and Database ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; -- change root pass SHOW DATABASES; CREATE DATABASE cats; USE cats; SHOW TABLES; CREATE USER 'jump'@'localhost' IDENTIFIED BY 'secret'; -- create a new user GRANT ALL ON cats.* TO 'jump'@'localhost'; -- grant privileges FLUSH PRIVILEGES; -- refresh privileges SHOW GRANTS; -- show users and grants Create Tables CREATE TABLE cats ( id INT unsigned NOT NULL AUTO_INCREMENT, -- Unique ID name VARCHAR(150) NOT NULL, -- Name of the cat owner VARCHAR(150) NOT NULL, -- Owner of the cat birth DATE NOT NULL, -- Birthday of the cat PRIMARY KEY (id) -- make id primary key ); ALTER TABLE cats ADD gender CHAR(1) AFTER name; ALTER TABLE cats DROP gender; DESCRIBE cats; CRUD INSERT INTO cats ( name, owner, birth) VALUES ( 'Sandy', 'Lennon', '2015-01-03' ), ( 'Cookie', 'Casey', '2013-11-13' ), ( 'Charlie', 'River', '2016-05-21' ); SELECT * FROM cats; SELECT name FROM cats WHERE owner = 'Casey'; DELETE FROM cats WHERE name='Cookie'; Cheat Sheet Common Shell Commands # run the mysql client mysql -h [host] -u [user] -p mysql -h [host] -u [user] -p [database] # export data mysqldump -u [user] -p [database] > data_backup.sql Common MySQL Commands \s; -- show status SHOW VARIABLES; -- show configuration params SHOW PROCESSLIST; -- show running queries SELECT @@version; -- mysql version SELECT @@datadir; -- location of db files SELECT @@hostname; -- current hostname SELECT USER(); -- current user SELECT DATABASE(); -- current database SHOW DATABASES; USE db_name; SHOW TABLES; DESCRIBE table_name; Common Admin Commands -- list all mysql users in database SELECT host, user FROM mysql.user; -- show grant for all users SHOW GRANTS; SHOW GRANTS FOR 'user'; CREATE USER 'username'@'hostname' IDENTIFIED BY 'password'; GRANT ALL ON db_name.* TO 'username'@'hostname' IDENTIFIED BY 'password'; -- refresh privileges FLUSH PRIVILEGES; DROP USER 'username'@'hostname'; Common Data Types PURPOSE EXAMPLE NOTES integers int(5) floats float(12,3) Will round your values money decimal(10,2) Up to 10 digits. 2 points date date datetime timestamp(8) (8)YYYYMMDD, (12)YYYYMMDDHHMMSS string varchar(20) large text blob enum enum('blue','red','gray') Not recommended to use Common Functions FUNCTION NOTES NOW() datetime input strcomp(str1,str2) Compare string lower(str) upper(str) ltrim(str) substring(str,idx1,idx2) password(str) Encry password curdate() Get date curtime() Get time Common CRUD -- INSERT INSERT INTO people VALUES ('MyName', '20020831'); INSERT INTO people (name, company_id, created_at) -- copy rows from same tbl SELECT name, 50, NOW() FROM people WHERE company_id = 49; -- SELECT SELECT * FROM tbl; -- All values SELECT * FROM tbl WHERE rec_name = "value"; -- Some values SELECT * FROM tbl WHERE rec1 = "value1" -- Multiple critera AND rec2 = "val2"; SELECT column_name FROM table; -- Selecting specific columns SELECT DISTINCT column_name FROM table; -- Retrieving unique outputs SELECT col1, col2 FROM table ORDER BY col2; -- Sorting SELECT col1, col2 FROM table ORDER BY col2 DESC; -- Sorting Backward SELECT COUNT(*) FROM table; -- Counting Rows SELECT owner, COUNT(*) FROM table GROUP BY owner; -- Grouping with Counting SELECT MAX(col_name) AS label FROM table; -- Maximum value SELECT pet.name, comment FROM pet, event -- Selecting from multiple tables WHERE pet.name = event.name; -- SEARCH SELECT * FROM table WHERE rec LIKE "blah%"; -- % is wildcard SELECT * FROM table WHERE rec LIKE "_____"; -- Find 5char values: _ is 1 char SELECT * FROM table WHERE rec RLIKE "^b$"; -- regex -- JOINS SELECT * FROM table_1 INNER JOIN table_2 ON conditions; SELECT * FROM table1 LEFT JOIN table2 ON conditions; -- UPDATE UPDATE table SET column_name = "new_value" WHERE record_name = "value"; -- DELETE DELETE FROM table WHERE condition; DELETE table_1, table2 FROM table_1 INNER JOIN table_2 ON table_1.column_1 = table_2.column_2 WHERE condition; Joins

INNER JOINreturns rows when there is at least one match in both tables based on the condition given

SELECT t1.*, t2.* FROM table1 t1 INNER JOIN table2 t2 ON t1.ID = t2.ID -- adding alias to header on columns SELECT t1.ID AS t1_id, t1.Value AS t1_v, t2.ID t2_id, t2.Value AS t2_v FROM table1 t1 INNER JOIN table2 t2 ON t1.ID = t2.ID

LEFT OUTER JOINreturns all the rows from the left table with the matching rows from the right table. If no columns in right matches, it returns NULL.

RIGHT OUTER JOINreturns all the rows from the right table with the matching rows from the left table. If no columns in left matches, it returns NULL.

Common Table Manipulations CREATE TABLE table_name (field1_name TYPE(SIZE), field2_name TYPE(SIZE)); DROP TABLE table_name ALTER TABLE authors ADD name VARCHAR(255), CHANGE author_work_id wokr_id INT, DROP nickname, CHANGE `count(*)` cnt bigint(21), ### renaming ALTER is_rich SET DEFAULT FALSE; -- Adding a column to an alreadycreated table ALTER TABLE tbl ADD COLUMN [column_create syntax] AFTER col_name; -- Removing a col

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: SQLMySQLRIAPostgreSQLTIUTAUFUAUTRIM
分页:12
转载请注明
本文标题:MySQL Quick Start and Cheat Sheet
本站链接:http://www.codesec.net/view/529878.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(30)