MySQL的知识点总结(一)

  • 前言:

  一直没有把学习笔记写成网络日志的习惯,一是觉得不大方便;二是觉得查找起来没有纸质的手感(女生的特质吧)。但是呢,近期要准备校招,为了总结大学期间专业课的学习内容和自学的一些知识,所以要开始写网络笔记了。由于个人技术有限,若有错误的地方,请大家指正。


一、关于MySQL数据库的操作

     创建、修改、删除数据库

create database if not exists test;  //创建一个名为test的数据库
use test;                                     //使用USE命令指定当前数据库
alter database test                      //修改数据库test的默认字符集和校对规则
default character set gbk
default collate gb2312_chinese_ci;
drop database if exists test;                    //删除数据库test
  • IF NOT EXISTS:在创建数据库前进行判断,只有该数据库目前尚不存在时才执行;在删除数据库的时候,避免删除不存在的数据库时出现MySQL错误信息

二、MySQL表操作

使用数据库之前先执行USE命令。

     创建、修改、删除表

show tables;    //该命令可以查看当前数据库中有哪些表

create table student   //创建一个名字为student的表
(
  学号 char(6) not null primary key,
  姓名 char(8) not null,
  性别 tinyint(1) not null default 1,
  出生日期 date not null,
  照片 blob null,
  备注 text null
);
  • auto_increment:设置自增属性,只有整形列才能设置此属性,顺序从1开始。
  • 定义外码时,数据类型和长度必须一样;参照的属性必须是主码。
  • 当一个表中有多个属性作为主码时,必须在字段定义完成后定义主码。
//alter table用于更改原有表的结构

alter table student
  add 毕业院校 varchar not null after 姓名,   //增加毕业院校这一列在姓名的后面
  drop column 照片;                                  //删除照片这一列

rename table student to stu;                    //将student表名更改为stu

drop table if exists stu;                           //删除表stu

 三、表记录的操作

       插入、修改、删除记录

  • 向表中插入全新的记录用INSERT语句
  • 替换旧记录用REPLACE语句,可以在插入数据之前将与新记录冲突(如:主键的唯一性)的旧记录删除,从而使新记录能够替换旧记录,正常插入。
  • 修改表记录用UPDATE语句,可以用来修改一个表,也可以用来修改多个表
  • 删除表记录用DELETE语句(还可以用TRANCATE TABLE语句,不过,我好像没用过~)
use test
insert into student
  values('081101','王林','山东工商学院',1,'1993-02-08');
replace into student
  values('081101','张三','武汉大学',0,'1992-01-12');
update student
  set 总学分 = 总学分 +10;             //将student表中的所有学生的总学分增加10

update user,vip                     //同时对表user和表vip记录进行修改
  set user.password='111',vip.password='222'
  where user.id=vip.id;
use test
delete from student           //删除test数据库里表student中学分小于60的学生记录
  where 总学分<60

btw:今天做数据库的题目时,有个题目是查看MySQL表结构的语句,当时只想起decribe table_name;其实总共有三种方法:

  1. describe table_name;
  2. desc table_name;         //这里的desc就是describe的简写啦
  3. show create table table_name;      //显示创建一个表的create table语句(这个方法好像真没用过,截图如下)

       

     

      

原文地址:https://www.cnblogs.com/rosestudy/p/4820234.html