MySQL的基础操作命令_Mysql数据库基础操作命令

今天介绍的是关天Mysql数据库一些操作的基础命令

用户与权限

创建用户

mysql>create user test identified by 'BaC321@#';

修改密码

5.5版本及以前的命令

mysql>set password for test=passowrd('!1A@2#3');

5.6及以上命令

mysql>update mysql.user set authentication_string=password('A1b2c3#!@') where user='test';

创建用户并授权

mysql>grant select,insert,update on student.* to test@localhost identified by 'A1b2c3#!@';

查看授权

mysql> show grants for test@localhost;

d809445e0f71aec1c0e2ac962f4e5610.png

移除权限

mysql> revoke insert,update on student.* from test@localhost;

建库与表

创建库

mysql> create database student;

mysql> show databases;

e15dd6a61851b5d99e197023d31ad925.png

创建表

mysql> use student;

mysql> create table T1 (name varchar(10) not null,sex varchar(10) not null);

通过现有的表创建新表

mysql> create table T2 as select * from T1;

插入数据

mysql> insert into T1 values('zhang','man');

Query OK, 1 row affected (0.03 sec)

mysql> insert into T1 values('li','man');

Query OK, 1 row affected (0.03 sec)

mysql> insert into T1 values('wang','man');

Query OK, 1 row affected (0.02 sec)

mysql> insert into T1 values('zhao','women');

Query OK, 1 row affected (0.05 sec)

#需要注意的是如果列超过两列,就需要指定列字段名如下

mysql> insert into T1(name,sex) values('gege','man');

查询数据

查询数据

mysql> select user,host from mysql.user;

#查看用户

mysql> select * from T1 where name like '%an%';

mysql> select * from T1 where age like '2%';

匹配查询

27e3d6554084ea017ea6ec3941e01164.png

mysql> select * from T1 order by name,age;

查询排序

eee64c4d175bc734ddbdea0a78c5e41d.png

mysql> select count(*) as toaolcount from T1;

mysql> select sum(age) as sumvalue from T1;

mysql> select avg(age) as avgvalue from T1;

mysql> select max(age) from T1;

查询值

2c18b1165ab9528b7c68fc1f4384787c.png

mysql> select score from T1 where score <91;

mysql> select score from T1 where score >=91;

mysql> select * from T1 where score in (96,100);

条件查询

3a83f0a38edf1a015af8b2c252bb3f35.png

mysql> select * from T2;

mysql> select * from T1;

94ca1350d3ce442aa314d121595ac944.png

增删更新

增加与删除列

mysql> alter table T1 add age int(4) not null;

mysql> alter table T1 drop age

更新表里的数据

mysql> update T1 set age=25 where name='zhang';

mysql> update T1 set age=23 where name='li';

删除数据

mysql> delete from T1 where age='22';

建索引与删除

mysql> create index indexT1 on T1(name(10));

mysql> drop index indexT1 on T1;

主键与视图

创建主键

mysql> alter table T1 add primary key(name);

mysql> desc T1;

bbd24e367d66871ef38f4e9548d5c97b.png

创建与删除视图

mysql> create view t1view as select name from T1;

mysql> select * from t1view;

e21de368103fa033c063b620c7e95190.png

mysql> drop view t1view;

mysql> select * from t1view;

ERROR 1146 (42S02): Table 'student.t1view' doesn't exist

#提示此视图不存在

点击关注

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值