MySQL笔记之一

启动服务: net start mysql;

关闭数据库: mysqladmin shutdown -u root -p

连接数据库:mysql -h 192.168.0.25 -u root -p

 

库的操作:

创建:create database mydb;(创建数据库)

显示:show databases;(查看数据库)

调用:use mydb;

删除:drop database mydb;(删除数据库)

 

表的操作:

创建:create table mydb_table (name varchar(10),age int(4),email varchar(20));(在数据库中创建表,添加不同的字段)

      create table test (id int(8) primary key auto_increment);(在数据库中创建表,添加字段的相关属性)

描述:desc mydb_table;(描述表结构)

 

修改:

alter table mydb_table add sex varchar(4);(添加一个新的字段)

alter table mydb_table drop age;(删除一个字段)

alter table mydb_table change sex xxxx char(20);(修改字段类型)

重命名:

rename table mydb_table to mydb ;(修改表名称)

删除:drop table mydb;(删除表)

 

 

记录的操作:

添加记录:

insert into mydb_table(name,age,email)values ('mydb','33','w@126.com');(添加一条记录)

insert into mydb_table values ('w','22','www@124.com');(添加记录)

insert into mydb_table values ('a','22','a'),('aa','33','aaa');(添加两条记录)

修改记录:

update mydb_table set email='mydb@123.com' where name='w';(修改记录信息)

删除记录:

delete from mydb_table where name = 'w';

查询记录:

select * from mydb;

select n1 from mydb;

select * from mydb where n1>20;

 

添加用户:

grant usage on *.* to 'mydb'@'localhost' identified by 'mydb';

 

授予用户权限:

grant select on mydb.mydb to user@localhost identified by 'password';(将select权限,mydb(库).mydb(表)授予user用户,并设置访问主机和用户密码)

grant select on mydb.* to user1@localhost identified by 'user1';user1可以对mydb库中的所有表进行查看)

 

取消用户权限:revoke select on mydb.* from user1@localhost;

 

查看用户权限:show grants for user1@localhost;

 

异地连接,必须在添加用户时,添加其访问的服务器名称

 

grant usage on *.* to 'mydb'@'%' identified by 'mydb';

 

重新载入授权表:flush privileges;

 

删除用户

delete from user where user = '用户';

 

修改口令:

1grant select on mydb.mydb to user2@localhost identified by 'aaa';by后面的内容即为用户口令,设置用户user2的口令为aaa

2set password for user2@localhost=password('mydb');(设置用户user2口令为mydb

为自己设置口令:set password=password('mydb');(当前用户为root,为自己设置口令为mydb

 

 

数据库的备份:

mysqldump -u root -p mydb mydb >mydb.sql

数据库的恢复:

先创建出数据库库

mysql -u root -p mydb cheng < mydb.sql

 

 

 

表的备份:

select * from mydb into outfile 'mydb.back';(将数据库中的某个表存放在某个文件中)

表的恢复:

load data infile '/tmp/mydb/job.txt' into table job_admin_login fields terminated by ',' lines terminated by '/n';

 

优化表:

optimize table www;

 

select distinct name from sql_test;                         查看表中不重复的name字段.

select name,math+physics+literature from mark;               查看计算完后的结果

select count(*) from mark;                                  统计数据库中记录的个数

select avg(字段),avg(字段),avg(字段),sum(字段) from mark;        avg()函数计算平均值,sum()计算和

select min(字段),min(字段),max(字段) from mark;              最小最大数

select * from mark order by 字段;                     指定查看时的排列(默认是升序)

select * from mark order by 字段 desc;                 倒序排列

select * from mark limit 2,2;          显示查询2行下面的2条记录

select * from mark where 字段 like '%c%';                 模糊查 

select * from mark where 字段 like 'w%';                开头

select * from mark where 字段 like '%w';               结尾

 

---------------

Oracle提供了一个非常好用的关键字rownum,可以限制结果集的大小。

MySQL也提供了相同的功能,写法略有差别。

 

Usage:

Select clause

[LIMIT [offset,] rows]

 

In Oracle

select * from table1 where rownum <= 2;

 

In MySQL

select * from table1 limit 0,2;

or

select * from table1 limit 2;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值