您的位置:首页 > 数据库 > MySQL

MySQL的基本操作及SQL语句的基本命令

2017-06-29 14:44 615 查看

从所周知关系型数据库对应的SQL语句都基本相同,实现增删查改更是相同,故今天用MySQL为例,做个介绍。

准备工作

1. 下载好MySQL的安装包:

MySQL安装包的下载链接

2. 安装(我用的是Zip版的)



默认安装过程一切都是正常的,没有失败(这种可能还是有的笑哭了 笑哭)

一、 数据库基本指令:

mysqld –install :数据库的安装命令
myqld –remove:数据库卸载命令
net stop mysql:关闭mysql
net start mysql:开启mysql
mysql -u用户 -p密码 : 用户登录
--tee=c:/mysql.log:将相关数据库操作,写成文件日志
show databases; :查看所有数据库
use test:切换到test数据库
show tables; :查看所有表
desc user; :查看user表结构,字段
select * from user; :查看user表中数据、表记录
exit; :退出mysql终端


*--------有分号的地方要注意加上.*


二、 数据库操作:

show databases; //查看所有数据库
create database txt; //创建txt数据库
use txt //切入到txt数据库
drop database txt; //删除txt数据库


三、 表操作:

show tables; //查看所有表
create table user( id int, name varchar, pass varchar ); //创建user表
desc user; //查看user表字段、结构
select *from user; //查看user表中数据、表记录
rename table user to user00; //强user表名改为user00
drop table user00; //删除user00表
insert into user(name,age,content)values('ll','19','00000');  //向表中存入数据
select *from user; //显示表中所有数据
select *from user where id=2; //检索查找表中数据


四、 表设计:

1.表字段类型:

(1)数值:

int(n)(int(3)与长度无关,不够3时前面补0)、 float


(2)字符串:

char(n)(检索快捷,最多255字节,储存n)、

varchar(n)(节约空间,最多255字节,储存l+1)、

text(65535字节,储存l+2)、

longtext(42亿字节,储存l+4)

(3)日期:

date(年月日)、time(时分秒)、datetime(年月日时分秒)、year(年)、timestamp


2.字段属性:

unsigned;//无符号,即全为正数

zerofill;//0填充,即int(3)不够3位补0

auto_increment;//自增

null;//默认为null,允许null

not null;//不允许为null

default;//默认值,若不允许为null,则用默认值

五、 字符集:

//用\s查看字符集:四中字符集必须统一:utf8
show create database test//查看数据库字符集
show create table user//查看表字符集

[mysql]
default-character-set=utf8 //客户端和链接字符集

[mysqlId]
character-set-sever=utf8 //服务器、数据库、表字符集
collation-sever=utf8_general_ci //服务器、数据库和表校验字符集
java设置客户端和连接字符集
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8


六、 表字段索引:

1.主键索引。

2.普通索引。

检测sql语句:

desc select *from user where id=2\G  //加G将表颠到一下
//其中rows 1 代表查找id=2的人之检索了一行


创建带索引的表:

create table user(
id int unsigned auto_increment,
name varchar(45),
primary key(id),  //主键索引
index in_name(name)  //普通索引
);
show index from user;//查看所有索引
sql帮助:? show、   ?select等,?+关键词,查询其用法。


后期维护普通索引:

添加普通索引:

alter table user add index in_sex(sex);


删除普通索引:


alter table user drop index in_name;


后期维护数据包字段:

添加字段:

alter table user add age int;
alter table user add uid int after id;  //在id后添加uid字段


修改字段:

alter table user modify age int not null default 20;


修改列名:

alter table user change name username varchar(45);//改名要跟上类型,


若原列有索引,改名后索引依然存在。

删除字段:

alter table user drop age;


七、 sql数据库语言:

1.DDL: 数据库表定义语言(创建、删除、修改),create、drop、alter.

2.DML:数据操作语言(增删改),insert、update、delete.

3.DQL: 数据查询语言,select.

4.DCL: 数据控制语言,grant、commit、rollback.

增-insert:

insert into user(name,age,content) values('ll','19','0000');


改-update:

update user set name="baby",sex="gil" where id=3;     //将id=3的name改成baby,sex改成gil.


删-delete:

delete from user where id=3;    //删除id=3的数据.
~~~~~~~~~~~~~~~~~~~~~~~id in(1,3,5);  //删除id=1、3、5的数据.
~~~~~~~~~~~~~~~~~~~~~~~id<=3 and id>=5; //删除id小于等于3,和大于等于5的数据.
~~~~~~~~~~~~~~~~~~~~~~~id between 3 and 5;  //删除id在3~5之间的数据.


查-select:

(1)原始查发:


select name,pass from user where id=2;    //查询特定列(name,pass)id=2的数据.


(2)给字段去别名-as:


select name n,pass p from user;
select name as n,pass as p from user;//给name,pass去别名n,p.


(3)去掉列中重复值:


select distinct name from user;     //除去表中name值重复的值


(4)使用where进行查询:


select *from user where id=2;   //查找id为2的人的所有数据


(5)查询空值null:


select *from user where age is null;
select *from user where age is null;


(6)between and的使用:


select *from user where id between 3 and 5;


(7)in的使用:


select *from user where id in(1,3,5);


(8)like模糊查询的使用:


select *from user where sex like"%o%";  //查询sex中包含o的
select *from user where pass regexp".*5.*";   //正则查询


(9)使用order by对查询结果进行排序:


select *from user order by name;//根据name排序从小到大,即升序
select *from user order by name desc;//根据name排序从大到小,即降序


(10)使用limit限定输出个数:


select *from user order by age desc limit 1,3;    //第二个位置取3个
select *from user order by age desc limit 5; //第一个位置取5个即0,5;


(11)concat函数-字符串连接符:


select concat("qw","-","1233");


(12)rand函数-将排序随机:


select *from user order by rand() limit 2;


(13)count函数-统计个数:


select count(*) from user;  //统计表行数
sum(age);//求和
avg(age);//平均数
max(age)://最大值
min(age)://最小值


(14)group by分组聚合的使用:(其进行分组,若无聚合函数,只显示其第一个)


select name,count(id) from user group by name order by count(id) desc;
//统计发帖数量,并降序排列
//group by 必须在order by前面,group by的优先级高于order by
//对分组后的结果进行筛选 having:(不用where)
如:select name,count(id) from user group by name having count(id)>=3;


八、 多表查询:

1.普通查询-多表(优先选择):(只显示量表相等的部分)

需求:

user表:

mysql> create table user(
-> id int unsigned auto_increment primary key,
-> name varchar(45),
-> sex varchar(30),
-> age int
-> );


post表:

mysql> create table post(
-> id int unsigned auto_increment primary key,
-> uid int,
-> title varchar(100),
-> content txt
-> );


结果一:

mysql> select user.name,post.title,post.content from user,post where user.id=post.uid;


+-------+-------+------------------+
| name  | title | content          |
+-------+-------+------------------+
| user3 | 11    | mnbvcxz          |
| user2 | 45    | @#$%^&*()        |
| user3 | **    | qwertyuiop[]     |
| user1 | 00    | hsbcabibawib     |
| user4 | ~~    | yyyyyyyyyooooooo |
| user4 | %%    | owops op         |
| user3 | ^^    | mnbaswwwwwwwwwz  |
| user3 | $$    | 000000000000z    |
| user2 | @@3#  | @@@@@@@@@!!!!!!! |
+-------+-------+------------------+
9 rows in set (0.02 sec)


结果二:

mysql> select user.name,count(post.id) from user,post where user.id=post.uid group by post.uid;


+-------+----------------+
| name  | count(post.id) |
+-------+----------------+
| user1 |              1 |
| user2 |              2 |
| user3 |              4 |
| user4 |              2 |
+-------+----------------+
4 rows in set (0.05 sec)


2.左链接查询-多表:(全显示左边的表,右边的进行匹配,有则显示,无则为null)

mysql> select user.name,post.title from user left join post on user.id=post.uid;


+-------+-------+
| name  | title |
+-------+-------+
| user3 | 11    |
| user2 | 45    |
| user3 | **    |
| user1 | 00    |
| user4 | ~~    |
| user4 | %%    |
| user3 | ^^    |
| user3 | $$    |
| user2 | @@3#  |
| user5 | NULL  |
| user6 | NULL  |
+-------+-------+
11 rows in set (0.02 sec)


3.嵌套查询-多表(适用于小表):(只能获取前表的数据,只是与后表有一定关联,无法获取后表数据)

mysq
a02a
l> select name from user where id in(select uid from post);


+-------+
| name  |
+-------+
| user1 |
| user2 |
| user3 |
| user4 |
+-------+
4 rows in set (0.05 sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 数据库 sql