MySQL 自学笔记

一、连接MySQL
1.登录MySQL
格式: mysql -h主机地址 -u用户名 -p用户密码
mysql -h localhost -uroot -p 回车后输入密码,如果SQL 服务器为本地服务器,则-h localhost 可省略
mysql -h110.110.110.110 -u root -p 123 连接到远程SQL 服务器
2.退出MySQL
格式:exit(退出)
二、修改密码
格式:mysqladmin -u用户名 -p旧密码 password 新密码。 例如
  1、 给root加个密码ab12。首先在DOS下进入目录mysql\bin,然后键入以下命令
  mysqladmin -u root -password ab12
  2、 再将root的密码改为djg345。
  mysqladmin -u root -p ab12 password **
三、数据库操作
1.创建数据库
CREATE DATABASE 数据库名;
create database test;
2.查看数据库
show databases;
3.删除数据库
drop database 数据库名;
drop database test;
4.数据库添加一个用户
grant select,insert,update,delete,create,drop,alter on 数据库.* to 用户名@登录主机 identified by “密码”
grant all privileges on test.* to testuser@localhost identified by “123456” ;  //  设置用户testuser,只能访问数据库test_db,其他数据库均不能访问 ;
grant all privileges on test.user_infor to testuser@localhost identified by “123456” ;  //  设置用户testuser,只能访问数据库test_db的表user_infor,数据库中的其他表均不能访问 ;
grant all privileges on . to testuser@“192.168.1.100” identified by “123456” ;  //设置用户testuser,只能在客户端IP为192.168.1.100上才能远程访问mysql ;
5.删除一个用户
删除账户及权限:>drop user 用户名@’%’;

        >drop user 用户名@ localhost;
 6. 使用数据库
 use 数据库名; use test;
四、数据表操作
0. 查看表结构
desc tables;
1.增加数据表
create table user(
字段名 varchar(20),
字段名 varchar(20)
); 注意最后一个字段后面没有逗号
create table user(
id int not null primary key auto_increment,
name char(20) not null,
sex int not null default ‘0’,
degree double(16,2));
not null -> 不能为空
auto_increment-> 自增 必须为整数类型的数据 并且必须制定Primary Key 属性
primary key 主键 和auto_increment 一起用
default 0 默认为0
double(16,2) 两个小数点 最多十六位长度(具体看实际长度)
2.删除数据表
drop table 表名
drop table user;
3.查看数据表
select <字段1,字段2,…> from < 表名 > where < 表达式 >
select * from user;
select id,name from user order by id limit 0,2; 查看id列和name列 数据并且限制0<= id <= 2;
4. 删除表中数据
delete from 表名 where 表达式
delete from MyClass where id=1;删除表 MyClass中编号为1 的记录
5.修改表中数据
update 表名 set 字段=新值,… where 条件
update MyClass set name=’Mary’ where id=1;
6.增加表中数据
insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )]
insert into MyClass values(1,’Tom’,96.45),(2,’Joan’,82.99), (2,’Wang’, 96.59);
7.在表中增加字段
alter table 表名 add字段 类型 其他 after 已存在字段;
在表MyClass中添加了一个字段passtest,类型为int(4),默认值为0
mysql> alter table MyClass add passtest int(4) default ‘0’
8.更改表名
rename table 原表名 to 新表名;
rename table MyClass to YouClass;
更新字段内容
update 表名 set 字段名 = 新内容
update 表名 set 字段名 = replace(字段名,’旧内容’,’新内容’);
文章前面加入4个空格
update article set content=concat(’  ‘,content);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值