MySql基础操作

# 使用MySQL数据库


# 登录到MySQL
 # -p : 告诉服务器将会使用一个密码来登录, 如果所要登录的用户名密码为空, 可以忽略此选项
 # mysql -h 主机名 -u 用户名 -p
 mysql -u root -p  # mysql>

# 创建一个数据库
 # 使用 create database 语句可完成对数据库的创建: create database 数据库名 [其他选项];
 create database samp_db character set gbk;  # Query OK, 1 row affected(0.02 sec)
 # 为了便于在命令提示符下显示中文, 在创建时通过 character set gbk 将数据库字符编码指定为 gbk
 # MySQL语句以分号(;)作为语句的结束, 若在语句结尾不添加分号时, 命令提示符会以 -> 提示你继续输入
 # (有个别特例, 但加分号是一定不会错的);

# 查看已创建的数据库
 show databases;

# 选择所要操作的数据库
 # 一: 在登录数据库时指定, 命令: mysql -D 所选择的数据库名 -h 主机名 -u 用户名 -p
 mysql -D samp_db -u root -p
 # 二: 在登录后使用 use 语句指定, 命令: use 数据库名; use 语句可以不加分号
 use samp_db  # Database changed

# 创建数据库表
 # 使用 create table 语句可完成对表的创建; create table 表名称(列声明);
 # 以创建 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 "-"
 );  # Query OK, 0 rows affected
 # 对于一些较长的语句在命令提示符下可能容易输错, 因此我们可以通过任何文本编辑器将语句输入好后保存为 createtable.sql 的文件中, \
 # 通过命令提示符下的文件重定向执行执行该脚本。
 mysql -D samp_db -u root -p < 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将自动索引该列。
      #下面的 char(8) 表示存储的字符长度为8, tinyint的取值范围为 -127到128, default 属性指定当该列值为空时的默认值。

# 查看已经创建的数据表名称
 show tables;

# 查看已创建的表的详细信息;  describe 表名;
 describe students;


# 操作MySQL数据库

# 向表中插入数据,
# insert 语句可以用来将一行或多行数据插到数据库表中
 # insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);  # 其中 [] 内的内容是可选的
 insert into students values(NULL, "王刚", "男", 20, "13811371377");  # Query OK, 1 row affected
 insert into students values(NULL, "立于", "男", 22, "13811371378");  # Query OK, 1 row affected
 insert into students values(NULL, "王星", "女", 19, "13811371887");  # Query OK, 1 row affected
 insert into students values(NULL, "昭著", "男", 24, "13811389377");  # Query OK, 1 row affected
 insert into students values(NULL, "萌总", "男", 18, "13812371377");  # Query OK, 1 row affected
 insert into students values(NULL, "呜嗷", "男", 17, "13811371377");  # Query OK, 1 row affected
 # 有时我们只需要插入部分数据, 或者不按照列的顺序进行插入, 可以使用这样的形式进行插入:
 insert into students (name, sex, age) values("孙丽华", "女", 21);

# 查询表中的数据,
# select 语句常用来根据一定的查询规则到数据库中获取数据,
 # select 列名称 from 表名称 [查询条件];
 select name, age from students;
 # 也可以使用通配符 * 查询表中所有的内容, 语句: select * from students;
 select * from students;

# 按特定条件查询:
# where 关键词用于指定查询条件, 用法形式为:
 # select 列名称 from 表名称 where 条件;
 select * from students where sex="女";
  # where 子句不仅仅支持 "where 列名 = 值" 这种名等于值的查询形式, 对一般的比较运算的运算符都是支持的, \
  # 例如 =、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等。 \
  # 还可以对查询条件使用 or 和 and 进行组合查询, 以后还会学到更加高级的条件查询方式, 这里不再多做介绍。
  select * from students where age > 21;  # 查询年龄在21岁以上的所有人信息
  select * from students where name like "%王%";  # 查询名字中带有 "王" 字的所有人信息
  select * from students where id<5 and age>20;  # 查询id小于5且年龄大于20的所有人信息

# 更新表中的数据
# update 语句可用来修改表中的数据, 基本的使用形式为:
 # update 表名称 set 列名称=新值 where 更新条件;
 update students set tel=default where id=1;  # 将id为5的手机号改为默认的"-"
 update students set age=age+1;  # 将所有人的年龄增加1
 update students set name="张伟鹏", age=19 where tel="13288097888";  # 将手机号为 13288097888 的姓名改为 "张伟鹏", 年龄改为 19

# 删除表中的数据
# delete 语句用于删除表中的数据, 基本用法为:
 #delete from 表名称 where 删除条件;
 delete from students where id=2;  # 删除id为2的行
 delete from students where age<20;  # 删除所有年龄小于21岁的数据
 delete from students;  # 删除表中的所有数据


# 创建后表的修改

# alter table 语句用于创建后对表的修改, 基础用法如下:

# 添加列
 # alter table 表名 add 列名 列数据类型 [after 插入位置];
 alter table students add address char(60);  # 在表的最后追加列 address
 alter table students add birthday date after age;  # 在名为 age 的列后插入列 birthday

# 修改列
 # alter table 表名 change 列名称 列新名称 新数据类型;
 alter table students change tel telphone char(13) default "-";  # 将表 tel 列改名为 telphone
 alter table students change name name char(16) not null;  # 将 name 列的数据类型改为 char(16)

# 删除列
 # alter table 表名 drop 列名称;
 alter table students drop birthday;  # 删除 birthday 列

# 重命名表
 # alter table 表名 rename 新表名;
 alter table students rename workmates;  # 重命名 students 表为 workmates

# 删除整张表
 # drop table 表名;
 drop table workmates;  # 删除 workmates 表

# 删除整个数据库
 # drop database 数据库名;
 drop database samp_db;  # 删除 samp_db 数据库


# **附录
 # 修改 mysql root 密码
 # 使用 mysqladmin 方式:
 # mysqladmin -u root -p password 新密码
 mysqladmin -u root -p password 123456
 # 执行后提示输入旧密码完成密码修改, 当旧密码为空时直接按回车键确认即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值