mysql数据库(入门)一

MySQL数据库

MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。

  • MySQL 是开源的,所以你不需要支付额外的费用。
  • MySQL 支持大型的数据库。可以处理拥有上千万条记录的大型数据库。
  • MySQL 使用标准的SQL数据语言形式。
  • MySQL 可以运行于多个系统上,并且支持多种语言。这些编程语言包括C、C++、Python、Java、Perl、PHP、Eiffel、Ruby和Tcl等。
  • MySQL 对PHP有很好的支持,PHP是目前最流行的Web开发语言。
  • MySQL 支持大型数据库,支持5000万条记录的数据仓库,32位系统表文件最大可支持4GB,64位系统支持最大的表文件为8TB。
  • MySQL 是可以定制的,采用了GPL协议,你可以修改源码来开发自己的 MySQL 系统。

数据库相关的SQL语句(基础)

1.查询所有数据库
show databases
2. 创建数据库
creste database 数据库名称
3. 查询数据库详情 查看数据库的字符集
show create database 数据库名称
4.创建数据库指定字符集
create database 数据库名称 character set 字符集(例如gbk)
5.删除数据库
drop database 数据库名称
6.选中数据库
use 数据库名称

表相关sql(一定要选中要操作的数据库)

1.创建表格式
create table 表名(字段名 类型,字段名 类型…)
2.查询所有表
show tables
3.查询表详情
show create table 表名
4.创建表指定引擎和字符集
create table 表名(字段名 类型…) engine=innodb(默认)/myisam,charset=gbk/utf-8

  • innodb(默认):支持数据库的高级操作如:事务,外键等
  • myisam:不支持高级操作,只支持基础的增删改查操作。

5.查看表结构(表字段)
desc 表名;
6.删除表
drop table 表名

修改表相关

创建一个表以此为例

修改表相关
create table student(name varchar(10),age int);
  1. 修改表名 -格式: rename table 原名 to 新名; rename table student to stu;
  2. 修改表引擎和字符集 -格式: alter table 表名 engine=myisam/innodb charset=gbk/utf8;

    alter table stu engine=myisam charset=gbk;

  3. 添加表字段
  4. 最后添加 alter table 表名 add 字段名 类型; alter table stu add chinese int;
  5. 最前面添加 alter table 表名 add 字段名 类型 first; alter table stu add math int first;
  6. 某个字段后面添加 alter table 表名 add 字段名 类型 after xxx; alter table stu add english int after name;
  7. 删除表字段 -格式:alter table 表名 drop 字段名; alter table stu drop english;
  8. 修改表字段名称和类型 -格式:alter table 表名 change 原字段名 新字段名 新类型; alter table stu change math english int;
  9. 修改字段类型和位置 -格式: alter table 表名 modify 字段名 类型 first/(after xxx); alter table stu modify english int after chinese;
  10. 练习:
  11. 创建一个hero表 有 id 整数 name 字符串 type 字符串 三个字段 create table hero(id int,name varchar(10),type varchar(10));
  12. 修改hero表的属性引擎为myisam 字符集为gbk alter table hero engine=myisam charset=gbk;
  13. 给hero表添加money字段 整数类型,添加在name的后面 alter table hero add money int after name;
  14. 修改type字段 名称改为 herotype varchar(20) alter table hero change type herotype varchar(20);
  15. 修改表名hero为 heros rename table hero to heros;
  16. 修改name字段到最后面 alter table heros modify name varchar(10) after hero_type;
  17. 删除money字段 alter table heros drop money;
  18. 删除hero表 drop table heros;

数据相关SQL

create table hero(name varchar(10),age int);
  1. 插入数据 -全表插入格式:insert into 表名 values(值1,值2...); insert into hero values('李白',30); -指定字段格式:insert into 表名 (字段1名,字段2名) values(值1,值2); insert into hero (name) values('关羽');

    • 批量插入数据: insert into hero values('刘备',20),('关羽',19),('张飞',30); insert into hero (name) values('悟空'),('八戒'),('沙僧');
  2. 查询数据 *代表所有字段 select * from hero; select name from hero; select name,age from hero; select * from hero where age<20; select * from hero where name='关羽';

  3. 删除数据 -格式:delete from 表名 where age=10; -删除表中所有数据: delete from 表名; delete from hero where age=20; delete from hero where name='悟空';
  4. 修改数据 -格式:update 表名 set name='Tom' where age=20; update hero set age=100 where name='八戒'; update hero set age=50;

中文问题

在命令行中执行 set names gbk; (需要在数据库里面执行)

显示?的是因为数据库或表的字符编码为gbk 修改为utf8

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值