dev.mysql.com
- Learning MySQL and MariaDB, O'Reilly
- Commands are not case sensitive.
- Commands may span multiple lines.
- Use
;
as a command end marker, or\G
for a different printout (often with more or more readable detail).
\c
mysql.server start
mysql.server stop
mysql -u root -p
grant all privileges on *.* to 'user'@'localhost' identified by 'pass' with grant option;
mysql -h host -u user -p
mysql -u user -p
mysql -p
mysql -p <db_name>
create database database_name;
use database_name;
show databases;
use my_database;
show table;
describe my_table;
show create table <my_table>\G
ctrl-D
quit
QUIT
mysqlshow --count database_name -p
select current_date;
SELECT DATE_ADD(CURDATE(), INTERVAL 1 MONTH);
select author, author2 from myLibrary where author2 is NULL;
select author, author2 from myLibrary where author2 <=> NULL;
source my_sql_file.sql;
help;
MariaDB [(mydb)]> prompt SQL Command \d>\_
PROMPT set to 'SQL Command \d>\_'
SQL Command mydb> prompt SQL>\_
PROMPT set to 'SQL>\_'
SQL> prompt db \d>\_
PROMPT set to 'db \d>\_'
db mydb>
mysqldump --user='user_name' -p mydb my_table > my_table.sql
mysqldump --user='user_name' -p mydb > mydb.sql
mysql --user='user_name' -p mydb < mydb.sql # restore
SELECT field1, field2
FROM my_db WHERE field1 LIKE 'A string beginning%';
SHOW COLUMNS FROM my_table LIKE 'my_column'\G
ALTER TABLE my_table ALTER my_column SET DEFAULT 7;
ALTER TABLE my_table ALTER my_column DROP DEFAULT;
ALTER TABLE my_table CHANGE COLUMN col1 col2 INT DEFAULT 3;
DROP TABLE IF EXISTS my_table;
-- add a column to an existing table with enum
ALTER TABLE my_table
ADD COLUMN membership_type ENUM('basic', 'premium');
DELETE FROM my_table WHERE id_number = 101;
SHOW COLUMNS FROM my_table LIKE '%id';
SHOW FULL COLUMNS FROM my_table;
CREATE TABLE test.my_table LIKE mydb.my_table;
INSERT INTO test.my_table SELECT * FROM mydb.my_table;
-
Note, to copy into an "existing" table, a good algorithm is to first back up the table, then drop it, then follow the steps above:
$ mysqldump --user='user_name' -p mydb mytable > mydb_mytable.sql SQL> DROP TABLE mydb.mytable; SQL> CREATE TABLE mydb.mytable LIKE test.mytable; SQL> INSERT INTO mydb.mytable SELECT * FROM test.mytable;
INSERT IGNORE INTO my_db.my_table
(field1, field2)
SELECT field1, field2
FROM other_db.other_table;
REPLACE INTO my_db.my_table
(field1, field2)
SELECT field1, field2
FROM other_db.other_table;
DELAYED
is deprecated as of MySQL 5.6.6.
INSERT LOW_PRIORITY INTO ...
INSERT DELAYED INTO ...
INSERT HIGH_PRIORITY INTO ...
DELETE FROM table1, table2
USING table1 JOIN table2
WHERE field1 = 'value'
AND filed2 LIKE '%another value'
AND table1.row_id = table2.row_id;
UPDATE my_table
SET field1 = 'the value',
a_date = DATE_ADD(CURDATE(), INTERVAL 2 MONTH)
WHERE row_id = 10;
DELETE FROM table1, table2
USING table1 JOIN table2
WHERE table1.field1 = 'a value'
AND table1.field2 = 'another value'
AND table1.row_id = table2.the_row_id;
SELECT DISTINCT(field1) FROM my_table;
-
field
containsvalue1
orvalue2
anywhereSELECT field FROM my_table WHERE field REGEXP 'value1|value2';
DELETE FROM table1, table2
USING table1 LEFT JOIN table2
ON table1.field_id = table2.field_id
WHERE field1 = 'value'
AND field2 = 'another value';
LOWER()
orLCASE()
UPPER()
orUCASE()
QUOTE()
LTRIM()
,RTRIM()
, andTRIM()
LPAD()
RPAD()
SPACE()
LEFT(<field>, <chars from the left>)
RIGHT(<field>, <chars from the right>)
MID(<field>, <start point>, <# of chars; default=ALL>)
SUBSCTRING()
is the same asMID()
SUBSTRING(<field FROM <#> FOR <#>)
- wordier syntaxSUBSTRING_INDEX(<field>, <separator character>, <# to take>)
LOCATE()
POSITION(<search string> IN <containing string>)
INSERT()
REPLACE()
SELECT Count(*) FROM my_table;
SELECT my_field, COUNT(my_field)
FROM my_table
GROUP BY my_field;
[NOT]
is optiona, of course, depending on intent:
SELECT my_field FROM my_table
WHERE [NOT] BETWEEN begin_expr AND end_expr;