1 连接数据库,找到msysql
C:\Windows\system32>cd C:\Program Files\MySQL\MySQL Server 5.7\bin
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -p
Enter password: ****
2 查询数据库
mysql> create databases;
3新建数据库
mysql> create database sjk;
4切换数据库
mysql> use sjk;
5显示数据库里的表
mysql> show tables;
6新建表
use sjk;
create table 表名(字段设定列表)//ENGINE:引擎 默认字符类型是utf8
create table if not exists stuTable (id int,name varchar(20),gender varchar(2))ENGINE='innoDB' default charset='utf8';
7显示数据表的结构
describe 表名;
describe stutable;
8插入数据
插入数据:insert into 表名(字段名1,字段2) values(值1,值2);
mysql> insert into stutable(name,gender)values('张三','M');
9修改数据
修改:update 表名 set 字段1=值1,字段2=值2 where 条件;
mysql> update stutable set name='李四'where name='张三';
10显示表中的记录
select * from 表名
mysql> select *from stutable;
11修改字段名或者儿编码类型
11_1增加字段
mysql> alter table stuTable add( beizhu char(8));
11-2重命名
mysql> alter table stuTable rename to student;
11-3修改属性
mysql> alter table student change beizhu opp char not null;
11-4删除
mysql> alter table student drop oop;
12查询
select *from student;//查询所有
select * from student id = 4;// 根据条件查询
select name,phone from stuTable where phone = '119' or phone = '120' // ||
select * from stuTable where name = 'xiaohan' and phone = '120 // &&
// 模糊查找
select count(name) from stuTable where name like '%xiao%' // like % 模糊查找
13排序
select score from scoreTable order by score // 升序
select * from scoreTable order by score desc // 降序
select score from scoreTable where score > 70 order by score limit 0,1
// limit begin,pageCount
14函数
count() 统计个数
max() 最大值
min() 最小值
avg() 平均值
网友评论