mysql日常操作整理

# 登录到MySQL
mysql -h主机名 -u用户名 -p

# 登录时指定数据库登录
mysql -D数据库 -h主机名 -u用户名 -p

# 登录之后选择数据库
use 数据库名;

# 退出
exit; 或 quit;

# 查看数据库中的数据
show databases;

# 查看所有表
show tables;

# 修改数据表名
rename table 表名 to 新表名;

# 查看表的定义
desc 表名;

# 查看创建这个表的SQL语句脚本(编码)
show create table 表名;

# 修改root用户密码
mysqladmin -uroot -p password 
  • 创建数据库
create database 数据库名 [其他选项];
eg: create database article character set utf8; //创建一个名为article的数据库,并为他设置utf-8编码
  • 查看mysql全局编码
show variables like 'character%';

修改全局编码:set 变量名 = 变量值;

eg:查看当前数据库编码:show variables like 'character_set_database';
  修改当前数据库编码为utf8:set character_set_database = 'utf8';
  • 修改数据库编码
alter database 数据库名 character set 编码;
  • 创建数据表
create table 表名称(列声明);

eg:创建 students 表, 表中将存放 学号(id)、姓名(name)、性别(sex)、年龄(age)、联系电话(tel) 这些内容:
  create table students(
    id int unsigned not null auto_increment primary key,
    name char(8) not null,
    sex char(4) not null,
    age tinyint unsigned not null,
    tel char(13) null default "-"
  );

 
  对于一些较长的语句在命令提示符下可能容易输错, 因此我们可以通过任何文本编辑器将语句输入好后保存为 createtable.sql 的文件中, 通过命令提示符下的文件重定向执行执行该脚本。

  打开命令提示符, 输入: mysql -D samp_db -u root -p < createtable.sql

  (提示: 1.如果连接远程主机请加上 -h 指令; 2. createtable.sql 文件若不在当前工作目录下需指定文件的完整路径。)
  
  语句解说:

  create table tablename(columns) 为创建数据库表的命令, 列的名称以及该列的数据类型将在括号内完成;
  括号内声明了5列内容, id、name、sex、age、tel为每列的名称, 后面跟的是数据类型描述, 列与列的描述之间用逗号(,)隔开;
  以 "id int unsigned not null auto_increment primary key" 行进行介绍:



"id" 为列的名称;
"int" 指定该列的类型为 int(取值范围为 -8388608到8388607), 在后面我们又用 "unsigned" 加以修饰, 表示该类型为无符号型, 此时该列的取值范围为 0到16777215;
"not null" 说明该列的值不能为空, 必须要填, 如果不指定该属性, 默认可为空;
"auto_increment" 需在整数列中使用, 其作用是在插入数据时若该列为 NULL, MySQL将自动产生一个比现存值更大的唯一标识符值。在每张表中仅能有一个这样的值且所在列必须为索引列。
"primary key" 表示该列是表的主键, 本列的值必须唯一, MySQL将自动索引该列。
  • 向表中插入数据
insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

eg:insert into students values(NULL, "王刚", "男", 20, "13811371377"); //1.省略列名,直接按顺序插入所有值
  insert into students (name, sex, age) values("孙丽华", "女", 21); //2.插入部分值,不能省略列名
  insert into students (name, sex, age) values("孙丽华", "女", 21),("张三", "男", 23),("李四", "男", 31); //3.一次性插入多条记录
  • 查询表中数据
select 列名称 from 表名称 [查询条件];

eg:select name, age from students;//查询表中所有学生的姓名和年龄
  select*from students;//查询表中所有字段


  where 子句不仅仅支持 "where 列名 = 值" 这种名等于值的查询形式, 对一般的比较运算的运算符都是支持的, 例如 =、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等。 还可以对查询条件使用 or 和 and 进行组合查询。
  示例:

  查询年龄在21岁以上的所有人信息: select * from students where age > 21;
  查询名字中带有 "王" 字的所有人信息: select * from students where name like "%王%";
  查询id小于5且年龄大于20的所有人信息: select * from students where id<5 and age>20;

Mysql-select查询语句详解

  • 更新表中数据
update 表名称 set 列名称=新值 [where 更新条件];

eg: 将id为5的手机号改为默认的"-": update students set tel=default where id=5;

  将所有人的年龄增加1: update students set age=age+1;

  将手机号为 13288097888 的姓名改为 "张伟鹏", 年龄改为 19: update students set name="张伟鹏", age=19 where tel="13288097888";
  • 同时更新多张表中数据
update t1,t2...tn set t1.field1=expr1,tn.fieldn=exprn... [where 条件]

eg: update num1 a,num2 b set a.four = a.four*b.four,b.four = a.four*b.four where a.id = b.id;

tips: 多表更新的语法更多地用在 : 根据 一个表的字段,动态地更新 另外一张表的字段
  • 删除表中数据
delete from 表名称 where 删除条件;

eg:删除id为2的行: delete from students where id=2;
   删除所有年龄小于21岁的数据: delete from students where age<20;
   删除表中的所有数据: delete from students;
  • 同时删除多张表数据
delete t1,t2...,tn  from t1,t2,...,tn [where 条件];
  • 查看auto_increment自增ID的值
select auto_increment from information_schema.tables where table_name = "表名" [and table_schema="数据库名"];
  • 修改auto_increment自增id的值
alter table 表名 auto_increment = 数字;
  •  创建后表的修改
添加字段
   alter table  表名  add  [column]列名  列数据类型 [first|after 列名];

    eg:在表的最后追加列 address: alter table students add address char(60);
    在名为 age 的列后插入列 birthday: alter table students add birthday date after age;

修改字段类型
  alter table  表名  modify  [column]列名  列数据类型  [first|after 列名];
  
   eg:将emp列类型改为varchar(20): alter table emp modify ename varchar(20);
  

删除字段
  alter table  表名  drop  [column]列名

   eg:删除 birthday 列: alter table students drop birthday;


字段改名
    alter table 表名  change  [column]列名  新列名 列数据类型 [first|after 列名];
    
    eg:将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default "-";
      将 name 列的数据类型改为 char(16): alter table students change name name char(16) not null;
重命名表
   alter table 表名 rename [to] 新表名;
    eg:重命名 students 表为 workmates: alter table students rename workmates;
 
 
修改表的编码
   alter table 表名 convert to character set 编码;
    eg:将workmates表编码修改为gbk:alter table workmates convert to character set gbk;
删除整张表
   drop table 表名;
    eg: 删除 workmates 表: drop table workmates;
 
删除整个数据库
   drop database 数据库名;
    eg:删除 samp_db 数据库: drop database samp_db;

猜你喜欢

转载自blog.csdn.net/u012089823/article/details/83092893