MySQL数据库基础操作命令

发布时间:2025-05-25 03:42:48 作者:益华网络 来源:undefined 浏览量(0) 点赞(0)
摘要:今天介绍的是关天Mysql数据库一些操作的基础命令 用户与权限 创建用户 mysql>createusertestidentifiedbyBaC321@#; 修改密码 5.5版本及以前的命令 mysql>setpasswordfortest=pass

今天介绍的是关天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; 

移除权限 

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

建库与表

创建库 

mysql> create database student;  mysql> show databases; 

创建表 

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%; 

匹配查询

 

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

查询排序

 

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; 

查询值

 

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);

条件查询

 

mysql> select * from T2;  mysql> select * from T1; 

增删更新

增加与删除列 

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; 

创建与删除视图 

mysql> create view t1view as select name from T1;  mysql> select * from t1view; 

 

mysql> drop view t1view;  mysql> select * from t1view;  ERROR 1146 (42S02): Table student.t1view doesnt exist  #提示此视图不存在

点击关注 民工哥技术之路 微信公众号对话框回复关键字:1024 可以获取一份最新整理的技术干货:包括系统运维、数据库、redis、MogoDB、电子书、Java基础课程、Java实战项目、架构师综合教程、架构师实战项目、大数据、Docker容器、ELK Stack、机器学习、BAT面试精讲视频等。 

二维码

扫一扫,关注我们

声明:本文由【益华网络】编辑上传发布,转载此文章须经作者同意,并请附上出处【益华网络】及本页链接。如内容、图片有任何版权问题,请联系我们进行处理。

感兴趣吗?

欢迎联系我们,我们愿意为您解答任何有关网站疑难问题!

您身边的【网站建设专家】

搜索千万次不如咨询1次

主营项目:网站建设,手机网站,响应式网站,SEO优化,小程序开发,公众号系统,软件开发等

立即咨询 15368564009
在线客服
嘿,我来帮您!