连接数据库(数据库本地连接)
WIN + R输入cmd然后输入mysql -uroot -p123456。(root是用户名,123456是密码。需要自己提前设置。)
(不会cmd进入mysql的可以网上搜教程。当然也能使用别的软件练习mysql。)
新增用户
格式:
grant select,insert,update,delete on数据库.*to 用户名@"登录主机" identified by "密码";
select,insert,update,delete 分别代表 增删改查 四个权限。
grant select,insert,update,delete on*.*to ayue345@"%" identified by "123"; 其中”%“代表internet上的任何计算机都能连接到你的数据库并操作,是一种危险授权方式。
本地访问可以将"%"变成"loaclhost",指定ip访问可以将"%"改成指定的ip地址。
grant select,insert,update,delete on*.*to ayue345@"localhost" identified by "123"
grant select,insert,update,delete on*.*to ayue345@"10.1.2.3" identified by "123";
查看用户
use mysql;
select user,host from user;
删除用户
delete from user where user='ayue345';
修改用户密码
grant reload on*.*to 'ayue345'@'localhost';
update mysql.user set authentication_string = password('123456'),password_expired = 'n' where user = 'ayue345'and host = 'localhost'; // 密码改成123456
flush privileges; // 刷新mysql的系统权限相关表。(也可以重启mysql服务器刷新)
查看数据库
show databases;
创建数据库
create database shop;
使用数据库
use shop;
删除数据库
drop database shop;
创建表(需要连接数据库后使用,即输入use 数据库;后才能创建表)
create table 表名(
字段
);
primary key(id) 意思是 id 设置为主键。
查看表
show tables; 可以查看数据库有什么表。
(可以看到,目前只有刚创建的user表。)
查看表结构
desc user;
查看表数据
select * from 表名;
( * 代表全部的意思。 )
因为表中没插入数据,所以是空的。
复制表(3种方式)
方式一:(复制结构、主键、索引)
create table new_table like old_table;
(蓝色是新表的名字,黄色是被复制的表的名字)
可以看出,user2 的表结构、主键、索引和 user 都一样 。但 user2 的数据现在是空的,因为没有复制数据,只复制了表的格式。
给表插入数据。
insert into user values(1,'ayue1','15');
查看表数据(select * from user;)
如果想给新表插入和旧表一样的数据 :
insert table user2 select * from user;
user2 的数据就会和 user 一样。
方式二:
复制表结构、数据。
create table new_table select * from old_table;
复制表结构,不复制数据。
create table new_table select * from old_table where 0;
数据操作:
插入(insert)、查询(select)。
插入数据:
insert into 表名(字段名,字段名) values(值,值);
insert into 表名 values(值,值,值);
查询数据:
select * from 表名;
修改记录(update)
update 表名 set 字段=值,字段=值 where 条件;
把 id=3 改成 id=4 。
删除记录(delete)
delete from 表名 where 条件;
把 id=4 的记录删除。
对查询结果排序(order by)
如果多个字段排序,先排第一个字段再排第二个字段。
select * from 表名 order by age asc; // asc为升序。
select * from 表名 order by age desc; // desc为降序。
select * from 表名 order by age,id desc; // 多个字段降序
先 id 降序,再 age 升序。
对查询结果分组(group by)
select * from 表名 group by age;
select id,group_concat(age) from 表名 group by age;
group_concat(字段名) 可以作为一个输出字段来使用,表示分组后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合。
设置分组条件(having)
select id from user where age > 20 group by age having id>0;
在 age > 20 的条件下筛选,然后按 age 分组,并且查询 id>0 的 id 。
限制查询数量(limit)
检索前3条
select * from user limit 3;
从第2条到第5条
字段操作
设置主键:
创建表时用 primary key 添加主键。
id 设置为主键。若主键需要改成 name 或者 age 时,应先删除主键 id ,再设置主键。
删除主键:
alter table 表名 drop primary key;
查看表主键:
show create table 表名;
设置复合主键:
创建表时 primary key(id,name) 。id 和 name 作为复合主键。
添加字段:
alter table 表名 add phone varchar(20) not null;
删除字段
alter table 表名 drop 字段名;
改变字段类型
alter table 表名 modify id int(25) not null;
id 的 type 变成 int(25) 。
字段重命名
alter table 表名 change 字段名 新字段名 字段的类型;
id 改成 num 。
字段设置默认值:
alter table 表名 alter 字段名 set default 默认值;
删除默认值的命令:
alter table 表名 alter 字段名 drop default;
例子:
创建 dept 表。
从上面可以看出 deptName 的默认值是办公室。
插入一条数据,没有输入deptName,但已经默认为办公室。
修改deptName 的默认值:
alter table dept alter deptName set default '管理部'
插入一条新数据,仍然不输入 deptName ,但他默认为管理部。
如果没有默认值,也不输入数据的话,插入失败。
设置自增字段:
先创建一个 stu 表。
(注: 没有主键是无法设置自增字段的。)
插入一条数据 。
删除数据重新插入时,id 会往上增,不会重复
id =2删除后插入新的数据,id 从3开始。
如果想要插入 id=2 的数据,在插入时就要写上。