使用
启动数据库
net start <mysql-service_name>
|
登录数据库
mysql -u <username> -p $ <password>
|
导入数据库
mysql> SET NAMES 'utf8mb4'; mysql> SET character_set_server = 'utf8mb4'; mysql> SOURCE /path/to/database.sql;
|
数据库
CREATE DATABASE yourDatabase CHARACTER SET utf8mb4;
show databases;
use yourDatabase
|
表
CREATE TABLE yourTable ( id INT AUTO_INCREMENT PRIMARY KEY, name varchar(50) NOT NULL DEFAULT 'worker', age INT , addr varchar(50) );
drop <table>;
show tables;
alter table <table> add <col> char(20) drop column <col>;
drop table <table>;
|
查询
select * from <table>\G
select <column1>, <column2>, <column3> from <table>;
select distinct <column> from <table>;
select <col> from <table> limit 5;
|
排序
ORDER BY
需要放在 WHERE
之后
select <col> from <table> order by <col> (asc);
select <col> from <table> order by <col1> desc, <col2> desc;
|
筛选
select <col> from <table> where <col> = <value>;
select <col> from <table> where <col> between 0 and 10;
select <col> from <table> where <col> is null;
|
逻辑操作符
优先级: AND
> OR
select <col> from <table> where <cond1> or (<cond2> and <cond3>);
select <col> from <table>
where <col> not in (<value1>, <value2>);
|
插入
insert into <table>(<col1>, <col2>) values(<val1>, <val2>);
insert into yourTable (name, email, age) values ('eric', 'example@email.com', 19);
|
更新
update <table> set <col1> = <val1>, <col2> = <val2> where <col3> = <val3>;
|
删除
delete from <table> where <col> = <val>;
|
联结
设计数据库时,应遵循将数据分解到不同的数据表这一原则。
然而,在使用数据时,常常需要将多个表的数据一起检索出来。
这时就需要用到联结。
select <col1>, <col2> from <table1>, <table2> where <table1>.<col> = <table2>.<col>;
select <col1>, <col2> from <table1> inner join <table2> on <table1>.<col> = <table2>.<col>;
|
安装
压缩包安装
- 初始化
mysqld --initialize --console > mysql_info
|
- 配置
mysql.ini
[mysqld]
port=3306
basedir=C:\\your\\path\\MySQL
datadir=C:\\your\\path\\MySQL\\Data
max_connections=200
max_connect_errors=10
character-set-server=utf8
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
[mysql]
default-character-set=utf8
[client]
port=3306
default-character-set=utf8
|
- 安装服务
mysqld --install MySQL8 --defaults-file="C:\your\path\MySQL\mysql.ini"
mysqld install
|
- 修改密码
mysqladmin -u root -p password (YourPassword) --port 3306
|