有那么挺长段时间没有敲代码了,今敲起来竟然有些sql都想不起来了??
把以前整理sql的内容看了下,再加了点??
主要参考自
MySql文档:https://dev.mysql.com/doc/refman/8.0/en/tutorial.html
连接远程数据库:
$ mysql -h host -u -user -p
连接本地数据库:
$ mysql -u user -p
退出
$ quit
mysql所处状态的含义 。
提示 | 含义 |
---|---|
mysql> |
准备进行新查询 |
-> |
等待多行查询的下一行 |
‘> |
等待下一行,等待以单引号(‘ )开头的字符串的完成 |
"> |
等待下一行,等待以双引号(" )开头的字符串的完成 |
`> |
等待下一行,等待以反引号(` )开头的标识符的完成 |
/*> |
等待下一行,等待以开头的注释的完成 /* |
取消查询:
mysql> SELECT -> USER() -> \c mysql>
查询当前服务器下的数据库:
1 mysql> SHOW DATABASES; 2 +----------+ 3 | Database | 4 +----------+ 5 | mysql | 6 | test | 7 | tmp | 8 +----------+
使用数据库:
1 mysql> USE test 2 Database changed
创建数据库:
1 CREATE DATABASE worker;
删除数据库:
1 mysql> DROP DATABASE worker; 2 Query OK, 0 rows affected (0.15 sec)
切换到数据库下时,
创建表:
1 mysql>CREATE TABLE student ( name VARCHAR(20), 2 number INT(8), sex CHAR(1), birth DATE);
创建表时复制表:
1 CREATE TABLE new_table 2 SELECT col, col2, col3 3 FROM 4 existing_table;
创建时复制部分:
1 CREATE TABLE new_table 2 SELECT col1, col2, col3 3 FROM 4 existing_table 5 WHERE 6 conditions;
查询库下的表:
1 mysql> SHOW TABLES; 2 +-------------------+ 3 | Tables_in_student | 4 +-------------------+ 5 | student | 6 +-------------------+ 7 1 row in set (0.00 sec)
查看表结构:
1 mysql> DESCRIBE student; 2 +--------+-------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +--------+-------------+------+-----+---------+-------+ 5 | name | varchar(20) | YES | | NULL | | 6 | number | int(8) | YES | | NULL | | 7 | sex | char(1) | YES | | NULL | | 8 | birth | date | YES | | NULL | | 9 +--------+-------------+------+-----+---------+-------+
删除表:
1 mysql> DROP TABLE student;
插入数据:
(如果列的值相对应则可以忽略列名进行插入)
1 mysql> INSERT INTO student 2 -> VALUES (‘Bob‘,201803224,‘m‘,‘1999-03-30‘); 3 Query OK, 1 row affected (0.44 sec)
1 mysql> INSERT INTO student 2 -> (name,number,sex,birth) 3 -> VALUES 4 -> (‘Lily‘,201805623,‘f‘,‘1998-04-23‘);
插入多条:
1 mysql> INSERT INTO student 2 -> (name,number,sex,birth) 3 -> VALUES 4 -> (‘Li‘,201835623,‘f‘,‘1998-04-13‘), 5 -> (‘Mi‘,201835923,‘m‘,‘1994-04-03‘), 6 -> (‘Mark‘,201845723,‘m‘,‘1994-03-02‘); 7 Query OK, 3 rows affected (0.09 sec) 8 Records: 3 Duplicates: 0 Warnings: 0
改:
删:
查:
原文:https://www.cnblogs.com/flytree/p/11624624.html