1 对mysql进行操作:
进入:mysql –h host_name –u username –ppasswd
mysql –u root –p密码
host_name:对方服务器主机名或ip地址
显示所有的数据库:show databases;(sql语句)
选定要操作的数据库:use dbname
显示指定数据库中所有的数据表:show tables
退出 \q
结束当前语句的输入:\c
2 对库进行操作:
新建数据库:create database 数据库名字;
例 :create database students;
删除数据库 drop database students; 注 一次只能删除一个
想·想·
建一个库student并进入该库
对表进行操作
新建数据表
create table 表名{
列名1 列类型 【列的完整性约束】
列名2 列类型 【列的完整性约束】
。。。。
}default charset=utf8;
例 create table student(
id int,
name varchar(20),
sex varchar(10))default charset=utf8;
显示表的创建结构:desc student;
删除数据表:drop 表名例 drop student(删除当前数据库内),
drop table 库名 表名(删除其他数据库的表)
插 向数据表中插入j记录(行数据)
a 一次插入一行数据
intsert【into】表名【列名】(列名指定插入的地方)values(值列表);
注 如果表名后面没有写列名,则默认向所有的列添加值,另外字符串的值要用“”或者括起来
赋值的数据类型必须与表中相匹配
例:insert into student(id,sex)values(1,‘男’);
insert into student values(7,”王”,’男’);
查看整张表的所有信息: select *from 表名
b 一次插入多行数据:
insert [into] 表名([列名]) values(值列表),(值列表)
例: insert [into] student values(3,’dd’,’女’),(4,’vv’,’男’);
c 从其他表中引入要插入的数据
insert [into] 表名([列名])select<列名>from 源表名[where…];
create table student1(id int,name varchar(20),sex varchar(10),age int)default charset=utf8;
insert into student1 values(1,”张无忌”‘男’,26),(2,“段誉”,‘男’27);
insert into student selected,name,sex from student1 where id=2;
改:更改数据表中的记录
update 表名 set<列名=更新值>[where《更新条件》];
注 where子句是用来限定更新条件的,不带的话会更新整列
例
update student1 set sex=‘女’
update student1 set sex=‘男’where id =1;
update student1 set sex=‘男’age=22 where id =2;
删除:删除数据表中的记录:
delete from 表名[where《删除条件》]
例 delete from student1 where age<26;
注 此语句为删除表中行数据,如果不带where子句则删除整个表中记录,但表不会被删除
查: 查询表中的信息:
select<列名>from 表名[where<查询条件表达式>][order by <排序的列名>][asc或者desc]
注:asc升序排列,desc降序,不写默认升序。
例
select *from student;
select id,name from student;
select *from student where id<=3 order by id;
select *from student where id<=7 order by id desc;
更改表的结构:(列)
1 为表添加一列,如果没有指定添加的位置,那么默认在列尾添加
alter table 表名 add 列名 建表语句[after 列名];
建表语句等于列类型+列的完整性约束
例 alter table student add age int;
alter table student add age xxx int after id;
2 删除一列:
alter table 表名 drop 列名;
例
alter table student drop xxx;
3 更改指定列的默认值:
alter table 表名 alter 列名 set default 默认值;
例
alter table student alter age set default 10;
4 更改列名和类型;
alter table 表名 change 旧列名 新列名《建表语句》[after 列名]
注 当旧列名与新列名相同时,可以改变改列的类型,不同,,在改变旧列名的同时改变他的类型
例
alter table student change id num int null;
5 为表增加一个主键;
主键:即把某一列设置为主键,主键列里的每一个成员互不相同,他存在的理由是能够唯一的标识一行。
alter table 表名 add primary key(列名);
一个数据表中只能有一个主键,选为主键的列 必须是非空(not null)
例
alter table student add primary key(id);
把顺序值列设置为主键列的,默认升序排列,已经存在的值不能被插入,特别是默认值0;
6 把主键设置为自增长列
例
alter table student change num num int auto_increment;
注 有0不行,设置为自增长的列要是整形,非空,且是主键列,在在此例中int 不可不写
7 从当前位置重新设置自增长的初值,否则删除一个记录以后再添加,会出现隔断情况
alter table 表名 auto_increment=初值;
8 删除主键;
alter atble 表名 drop primary key;
删除主键之前要删除自增长;
例
alter table student change num num int 、、删除自增长
alter table student drop primary key
9 更改表名:
alter table 表名 rename as 新表名;
例
alter table student rename as student2;
例
创建一个数据表;
create table student3(
code int not null auto_increment;
name vachar (20) not null,
age int,
sex varchar(10),
score int,
grade int,
address varchar(20) default’未知’,
major varchar(20),
primary key(code))default charset=utf8;
向表中插入数据;
inset into student3(name,age,sex,socre,grade,address,major)
values(‘xxx’,13,’m’,78,1,’asdf’,’sss’),
(‘aaa’,15,’e’,76,2,’wer’,’qqq),
(‘zaa’,16,’c’,76,3,’wweer’,’wqq),
(‘caaa’,24,’ed,96,1,’wergr’,’dqq),
(‘azaa’,34,’ea,66,1,’wsder’,’cqq);
查看全体学生的全体详细信息:
select *from student3;
查看全体学生的学号和姓名:
select code,name from student3;
查看全体学生的学号和姓名,用中文显示列名;
select code as‘学号’,name as‘姓名’from student3;
、、只是在当次查看以中文显示,在表中没有改变;
给表设置别名
select s.code,s.name from student3 as s;
查询所有年级号(去掉重复的年级号)
select grade from student3;
查询年龄在20以下的学生姓名
select name from student3 where age<=20;
查询全体学生的所有信息并按年龄降序排列;
select *from student3 order by age desc 、、升序 删除 desc
查询年龄最大的前三学生所有信息:
select *from student3 order by age desc limit 3,;
查询年龄最大的第四个和第五个
select *from student3 order by age desc limit 3,2;
计算学生的总数
select count(*)from student3 ;//count(列名):该列的元素的总个数
计算一班的平均成绩:
select avg(score)from student3 where grade=1;、、avg(列名):对某一列的值计算平均值
查找sss专业的最高分和最低分
select max(score)as ‘最高分’,min(score)as‘最低分’from student3 where major=‘sss’;
查询每个专业的平均成绩:
select major,avg(score)as‘平均成绩’from student3 group by major;
查询专业的平均成绩在70 以上的专业;
select major,avg(score)as ‘平均成绩’from student3 group by major having avg(score)>70;
求总分;
select sum(score)from student3 。。sum(列名)对某一整形列的值求和
查询年龄在20-50 之间的学生的个人信息:
select *from student3 whereage between 20and50;
查询年龄不在20-50 之间的学生的个人信息:
select *from student3 where age [not] between 20and50;
查询nnn vvv 专业的学生的所有信息;
select *from student3 where major in(‘nnn’‘vvv’);
《=》select *from student3 where major =‘nnn’or major=‘vvv’;
查询学号为一的学生的信息
select *from student3 where code=1;
select *from student3 where code like 1;
查询名字以x开头的所有学生
select *from student3 where name like ‘x%’;
查询名字以x结尾的所有学生的信息
select *from student3 where name like ‘%x’;
查询名字里含有x的所有学生的所有信息:
select *from student3 where name like ‘%x%’;
查询当前数据库以stu开头的所有数据表
show tables like ‘stu%’;
查询性别为m 并且分数小于70 分的学生姓名
select name from student3 where sex=‘m’and score<70;
查询性别为m的学生的学号名字和年龄,查询结果按年龄的降序排列
select name ,age,code from student3 where sex=‘m’order by age desc;
查询全体学生的信息 ,查询结构按年级的升序排列,同一年级的学生按分数的降序排列
select *from student3 order by grade,score desc;
注 select *from 表名 order by m[desc],n[desc]…..
查询表的所有信息以m排序,在m相同的记录以n排序;
------------------------------先建两个数据表------------------------------------------
create table stu(
id int,
name varchar(20),
age int,
sex varchar(6))default charset=utf8;
create table cour(
num int,
course varchar(20),
score int)default charset=utf8;
---------------------------------------向2个表中插入数据-------------
insert into stu values(1,’张无忌’,26,’男’), (2,’乔峰’,33,’男’), (3,’徐大侠’,36,’男’), (5,’风清扬’,22,’男’);
insert into cour values(1,’语文’,78), (1,’数学’,88),(2,’语文’,88),(2,’数学’,78) (3,’英语’,78),(1,’美术’,78);
多表查询(2表)
1 内连接(inner join);最后的表为2张表连接条件的交集的合集。
select s.id,s.name,s.age,c.couse,c.scose
from stu as s
inner join cour as c
on s.id=c.num;
2 select stu.id,stu.name,stu.age,cour.
from stu,cour
where stu.id=cour.num;
----当有三张表---
1 select A.a,B.b,C.c
from ABC
where A.x=B.y=C.z;
2 左外连接(left join)以左表为主
select s.id,s.name,s.age,c.course,c.score
from stu as s
left join cour as c
on s.id=c.num;
3 右外连接(right join);已右表为主
select s.id,s.name,s.age,c.course,c.score
from stu as s
right join cour as c
on s.id=c.num;
子查询:将一个查询块嵌套在另一个查询块的where子句或having 短语中的查询称为子查询
注 一个select-from-where 语句称为一个查询块
例: select name,age from stu where id in(select num from cour where course=’语文’);
复制表:
1)复制一张已知的数据表;
create table 新表名(x1) select *from 已知表
2)在复制表示限制复制表中的内容
create table 新表名 select[列名]from 已知表;
3)创建一个已存在的表的空表;
例 create table x2 select name from stu;
create table 新表名 select*from 已知表 where 0=1;
create table x3 select*from stu where 0=1;
启用mysql日志
SET GLOBAL general_log = 'ON';
MongoDB数据库
mongo
show dbs;
use fish
var u=db.users;
u.find({nickname:'XXX'})
u.insert({});
mysql 安装
重启mysql
service mysqld restart
alias rm='rm -i'
cp /dev/null mysqld.log
查看MySQL密码
grep "temporary password" /var/log/mysqld.log
修复mysql
REPAIR TABLE 表名
mysql设置:
vi /etc/my.cn
max_connections=1000
tmp_table_size = 128M
max_heap_table_size = 128M
[client]
password = *********
在 /etc/my.cn 中添加skip-grant-tables 实现免密登录
查看MySQL密码
grep "temporary password" /var/log/mysqld.log
修改MySQL密码
先修改首先,修改validate_password_policy参数的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
set password = password('123456');
flush privileges
本地ip连接mysql
use mysql;
update user set host = '%' where user = 'root';
flush privileges;