新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章给大家介绍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' doesn't exist #提示此视图不存在
关于MySQL数据库中有哪些基础操作命令就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。