mysql基础

数据库软件对大小写不敏感,需要在每条命令末尾添加‘;’

一、库操作

1. 登录数据库
  • mysql -u root -p
    -u指定用户,-p指定使用密码登录
2. 创建数据库
  • create database 数据库名称;

    创建一个数据库,名称为student :

create database student
3. 查看已有数据库
  • show databases;
    这里
4. 删除数据库
  • drop database 数据库名称;
    例如:
drop database student

(友情提醒,谨慎操作)

5. 选择数据库
  • use 数据库名称;
    在确定使用某个数据库后,使用use (数据库名称)切换到目的数据库
use student

二、表操作

1. 新建表
create table 表名称 (
	字段名称 数据类型 [具体要求],
	......
);

example:
create table info (
	ID int(8) not null primary key,
	name varchar(20),
	sex varchar(4),
	birthday date
);

这里写图片描述

名称介绍
NULL数据列可包含NULL值;
NOT NULL数据列不允许包含NULL值;
DEFAULT默认值;
PRIMARY KEY主键;
AUTO_INCREMENT自动递增,适用于整数类型;
UNSIGNED是指数值类型只能为正数;
CHARACTER SET name指定一个字符集;
COMMENT对表或者字段说明;
2. 查看表
  • show tables;
    这里写图片描述
3. 查看表结构信息
  • describe 表名称;
    这里写图片描述
4. 删除表
  • drop table 表1, 表2, … ;
删除表 info

drop table info
5. 修改表
  • 修改表名 : rename table 原表名 to 新表名;
rename table info to stu;
  • 新增字段 : alter table 表名 add 字段名 数据类型 [列属性] [位置];
    位置表示此字段存储的位置,分为first(第一个位置)after + 字段名(指定的字段后,默认为最后一个位置)
alter table info add height int first;
alter table info add  weight int after height;

这里写图片描述

  • 修改字段 : alter table 表名 modify 字段名 数据类型 [列属性] [位置];
alter table info modify height int(8) first;

这里写图片描述

  • 重命名字段 : alter table 表名 change 原字段名 新字段名 数据类型 [列属性] [位置]
alter table info change height length int first;

这里写图片描述

  • 删除字段 : alter table 表名 drop 字段名;
alter table info drop length;
alter table info drop weight;

这里写图片描述


三、数据操作

1. 常用数据类型
类型说明
int整型
double浮点型
varvhar字符串型
date日期,格式为yyyy-mm-dd
2. 添加行数据
  • insert into 表名 values(值列表) [ ,(值列表) ];
insert into info values('1', 'liushall', 'boy', '2000-1-1');
insert into info values('1', 'liushall', 'boy', '2000-1-1'), ('2', 'zhangsan', 'boy', '2001-12-12');

这里写图片描述

  • 给部分字段添加数据 : insert into 表名(字段列表) values(值列表) [ ,(值列表) ];
insert into info(ID, name) values('3', 'lisi');

这里写图片描述

3. 查询数据
  • 查看全部字段信息 : select * from 表名 [ where 条件 ];
select * from info;
  • 查看部分字段信息 : select 字段名称[ ,字段名称 ] from 表名 [ where 条件 ];
select ID, name, sex from info where id != 3;

这里写图片描述

4. 更新数据
  • update 表名 set 字段 = 修改后的值 [where 条件];
update info set sex = 'girl' where name = 'lisi';

这里写图片描述

5. 删除数据
  • delete from 表名 [ where 条件 ];
delete from info where name = 'lisi';

这里写图片描述

PS

用 [] 括起来的时可选项,不需要的可以不用写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值