本文共 2542 字,大约阅读时间需要 8 分钟。
在 Windows 环境中,打开命令提示符(右键点击 WIndows 按键 + R,输入 cmd),然后执行以下命令:
mysql -u root -p
注意:如果 MySQL 安装在其他路径,请根据实际情况调整用户名和密码。
退出命令窗口:
exit;
使用以下命令查看所有数据库:
show databases;
创建数据库并设置默认字符集:
create database test default charset=utf8;
删除数据库:
drop database test;
切换到指定数据库:
use test;
tinyint:占用1字节,范围为-127到127int:占用4字节,范围为-2147483647到2147483647float:浮点数,精度约为7位decimal:以字符串形式存储小数char:固定长度字符串,最大255个字符varchar:可变长度字符串text:长文本内容timestamp:以年月日形式存储时间戳注意:除了数值类型,其他类型需用引号包裹。
unsigned:无符号整数(正数)auto_increment:自增字段(必须设置为主键)not null:字段不能为 NULLdefault:字段默认值(插入或修改时若为空则采用默认值)创建用户表:
create table user ( id int unsigned primary key auto_increment, name char(4) not null, sex char(1) not null, age tinyint not null);
删除用户表:
drop table user;
alter table user add height int unsigned not null;
alter table user drop height;
修改 age 字段为无符号:
alter table user modify age tinyint unsigned not null;
修改字段名:
alter table user change sex gender int tinyint unsigned not null;
重命名表为 users:
rename user to users;
查看用户表结构:
desc user;
插入单条记录:
insert into user (name, age) values('user1', 18); 插入多条记录:
insert into user (name, age) values('user2', 20), ('user3', 22), ('user4', 24); 删除指定记录:
delete from user where id=1;
清空数据:
truncate user;
更新用户信息:
update user set name='testUser', age=30 where id=1;
查询顺序:select → from → where → group by → having → order by → limit
select * from user;
select name, age from user;
select * from user where id=1;select * from user where id>3;select * from user where name is null;
select name, age from user where name like '%测试%';select name, age from user where name like '_测试';
按性别分组并统计人数:
select sex, count(*) from user group by sex;
按年龄升序排序:
select * from user order by age;
查看前5条记录:
select * from user limit 5;
查询用户表和信息表:
select * from user, info where user.id = info.id;
select * from user inner join info on user.id=info.id;
在 WampServer 中:
my.ini,在 [client] 加入 default-character-set=utf8[wampmysqld64] 加入 character-set-server=utf8 和 collation-server=utf8_general_cimysql -uroot -pupdate mysql.user set authentication_string=password('newPassword') where user='root';flush privileges; mysql -uroot -p123 test < 1.sql
mysqldump -uroot -p123 test user > 1.sql
在 my.ini 中修改默认存储引擎为 InnoDB:
default-storage-engine=InnoDB
本文内容转载自 CNBlog。