MySQL基础笔记(一)

一、window服务

1. 启动mysql
  net start mysql   --需要管理员身份打开

2. 关闭mysql
  net stop mysql  

3. 连接服务器
  mysql -h 地址 -p 端口 -u 用户名 -p 密码
    -- 例:mysql -h localhost -u root -p

4. 显示哪些线程正在运行
  show processlist;

5. 显示系统变量信息
  show variables;

二、数据库操作

1. 显示所有数据库
  show databases;

2. 查看当前数据库
  select database();

3. 显示当前时间、用户名、数据库版本
  select now() , user() , version();

4. 创建数据库
  create database 数据库名 数据库选项;
  数据库选项:
        character set charset_name
        collate collation_name

5. 修改库的选项信息
  alter database 数据库名 选项信息;

6. 删除数据库
  drop database 数据库名;

7. 修改数据库的字符编码
  alter database 数据库名 character set utf8 (字符编码,例如 utf-8);

8. 使用数据库
  use 数据库名;

三、数据类型

数据类型:int        整型
         tinyint    整型(0-256) 
	 
	 char(X)    定长字符型                例如 char(10)
	 varchar(X) 可变长度字符型            例如varchar(10)

	 text       大段文本
	 binary     二进制(存储照片等)

         float(单精度)     4字节
         double(双精度)    8字节
         decimal    浮点型(总位数,小数位数) 例如 decimal(3,1)

四、创建表的字段属性

null
not null
default 'xxx' --默认值
auto_increment --自动增长
primary key --主键

五、表的操作

1. 创建表
  create [temporary] table [数据库名.]表名 (表的结构定义) [表选项]
    --每个字段必须有数据类型
    --最后一个字段后不能有逗号
    --temporary 临时表,会话结束时表自动消失

    例: create table a(姓名 char(2) null,学号 int );  -- 注意分号

2. 查看表
  show tables;
  show tables from 数据库名;

3. 查看表的结构
  show create table 表名;

4. 显示表的结构
  describe(或 desc) 表名;

5. 对表重命名
  rename table 原表名 to 新表名;

6. 删除表
  drop table 表名;

7. 删除多张表
  drop table 表1,表2,表3;


六、数据的基本操作

1. 插入数据
  insert into 表名 (字段1,字段2...) values (值1,值2...);
  --例如:insert into b (姓名,学号) values('李林',110);

2. 查询数据
  select * from 表名;
  select * from 表名 where 字段1 = 值1 and 字段2 = 值2 and ......;

  --例如查询成绩大于90分或者成绩小于60分的学生信息 
  --select * from stu where score > 90 or score < 60;

3. 排序
  --升序 (默认是升序)
  select * from 表名 order by 字段1 asc;
  --降序
  select * from 表名 order by 字段1 desc;
  --按某一条件排序
  select * from 表名 where 字段1 = 值x oreder by 字段x asc(desc);

4. 取前n条数据
  select * from 表名 limit n;

5. 从第n条开始取m条
  select * from 表名 limit n,m;

6. 删除数据
  delete from 表名 where 字段=值;

7. 修改数据
  update 表名 set 字段1=值1 where 条件;
  update 表名 set 字段1=值1;
发布了22 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43930694/article/details/99488338