在命令行在操作数据库:(注意 加了 ; 是SQL语句结束标识 )
(1)连接数据库: mysql -h localhost(主机地址) -u root(用户名) -p 回车 在输入数据库密码
(2)修改密码:mysqladmin -u root(用户名) -p 123456(旧密码) password 12345(新密码);
(3)显示数据库列表: show databases;
(4)显示某个数据库中的数据表: use test(数据库名称); show tables;
(5)显示数据表的结构: describe user(表名);
(6)创建数据库: create database thinkphp(数据库名);
(7)删除数据库: drop database thinkphp(数据库名);
(8)创建表:use thinkphp(数据库名); create table user(表名)
( id int(3) auto_increment(自动增长) not null primary key(设置为主键),
name varchar(10) not null,
age int(3) not null ) engine = myisam partition by key(id) partition 5 ;
注意分区:key hash 是去余操作 list rang 是条件分区 list条件是可列举的 rang条件是可以范围的。
(9) 删除表:drop table user(表名);
(10) 向表中插入数据: insert into user(表名) values ( 1 , `php`, 1);
(11) 删除表中的数据: delete from user(表名) where age =3;
(12) 显示表中的数据: select * from user(表名);
(13) 清空表中的数据: delete from user(表名) ;
(14) 更新表中的数据: update user(表名) set name = 'linux' where id =2;
(15) 查询表中结果排序: select * from user order by id desc(倒序排列 ase顺序排列);
(16)查询表中某个数据的总数: select count(id) from user where name = 'linux';//id、name 字段名
(17)查询表中的最大值: select max(id) from user; //sum 求和 avg 平均数 min 最小数 max 最大数
(18)组合查询: select goods.id goods.name user.age from goods, user where goods.id = user.id;
(19)建立索引: create index user_index(索引表名字任意取) on user(id);
(20)连接查询: select goods.id goods.name user.age from goods inner join user on goods.id = user.id order by goods.name; //inner join 是查找两个人相等的数据 left join 查找左表中所有的数据右表中相等的数据
right join 查找右表中的所有数据左表中想等的数据
(21)向表中添加字段: alter table user(表名) add sex bool(要添加的字段名和类型);
(22)向数据库中导入sql文件:先定位到数据库中在使用 source d:thinkphp.sql
(23) 退出数据库: exit;