select * from emp; #注释
#---------------------------
#----命令行连接MySql---------
#启动mysql服务器
net start mysql
#关闭
net stop mysql
#进入
mysql -h 主机地址 -u 用户名 -p 用户密码
#退出
exit
status;
显示当前mysql的version的各种信息。
#---------------------------
#----MySql用户管理---------
#修改密码:首先在DOS 下进入mysql安装路径的bin目录下,然后键入以下命令:
mysqladmin -uroot -p123 password 456;
#增加用户
#格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by '密码'
/*
如,增加一个用户user1密码为password1,让其可以在本机上登录, 并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令:
grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";
如果希望该用户能够在任何机器上登陆mysql,则将localhost改为"%"。
如果你不想user1有密码,可以再打一个命令将密码去掉。
grant select,insert,update,delete on mydb.* to user1@localhost identified by "";
*/
grant all privileges on wpj1105.* to sunxiao@localhost identified by '123'; #all privileges 所有权限
#----------------------------
#-----MySql数据库操作基础-----
#显示数据库
show databases;
#判断是否存在数据库wpj1105,有的话先删除
drop database if exists wpj1105;
#创建数据库
create database wpj1105;
#删除数据库
drop database wpj1105;
#使用该数据库
use wpj1105;
#显示数据库中的表
show tables;
#先判断表是否存在,存在先删除
drop table if exists student;
#创建表
create table student(
id int auto_increment primary key,
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
)default charset=utf8;
#删除表
drop table student;
#查看表的结构
describe student; #可以简写为desc student;
#插入数据
insert into student values(null,'aa','男','1988-10-2','......');
insert into student values(null,'bb','女','1889-03-6','......');
insert into student values(null,'cc','男','1889-08-8','......');
insert into student values(null,'dd','女','1889-12-8','......');
insert into student values(null,'ee','女','1889-09-6','......');
insert into student values(null,'ff','null','1889-09-6','......');
#查询表中的数据
select * from student;
select id,name from student;
#修改某一条数据
update student set sex='男' where id=4;
#删除数据
delete from student where id=5;
# and 且
select * from student where date>'1988-1-2' and date<'1988-12-1';
# or 或
select * from student where date<'1988-11-2' or date>'1988-12-1';
#between
select * from student where date between '1988-1-2' and '1988-12-1';
#in 查询制定集合内的数据
select * from student where id in (1,3,5);
#排序 asc 升序 desc 降序
select * from student order by id asc;
#分组查询 #聚合函数
select max(id),name,sex from student group by sex;
select min(date) from student;
select avg(id) as '求平均' from student;
select count(*) from student; #统计表中总数
select count(sex) from student; #统计表中性别总数 若有一条数据中sex为空的话,就不予以统计~
select sum(id) from student;
#查询第i条以后到第j条的数据(不包括第i条)
select * from student limit 2,5; #显示3-5条数据
-----------------------------------
#------几个简单的基本的sql语句---------
插入:
insert
into
table1(field1,field2)
values
(value1,value2)
删除:
delete
from
table1
where
范围
更新:
update
table1
set
field1=value1
where
范围
查找:
select
*
from
table1
where
field1
like
’%value1%’
---like的语法很精妙,查资料!
排序:
select
*
from
table1
order
by
field1,field2 [
desc
]
总数:
select
count
as
totalcount
from
table1
求和:
select
sum
(field1)
as
sumvalue
from
table1
平均:
select
avg
(field1)
as
avgvalue
from
table1
最大:
select
max
(field1)
as
maxvalue
from
table1
最小:
select
min
(field1)
as
minvalue
from
table1