MySQL数据库
命令行连接
-
现在在工作中基本不使用终端进行连接操作数据库了,一般都使用图形化工具,如
Navicat for MySQL
,DBeaver
等等,但是操作数据库的命令操作方式,需要熟练掌握。 -
通过终端连接。 命令:
mysql -uroot / mysql -uroot -p (输入密码)
连接成功之后,如下图所示
退出登录
复制代码
1
2
3
4quit 和 exit 或 ctrl+d
数据库操作
- 查看所有数据库
复制代码
1
2show databases;
- 使用数据库
复制代码
1
2use 数据库名;
- 查看当前使用的数据库
复制代码
1
2select database();
- 创建数据库
复制代码
1
2
3
4create database 数据库名 charset=utf8; 举例: create database python charset=utf8;
- 删除数据库
复制代码
1
2
3
4drop database 数据库名; 例: drop database python;
数据表操作
- 查看当前数据库中所有表
复制代码
1
2show tables;
- 查看表结构
复制代码
1
2desc 表名;
- 创建表
- auto_increment表示自动增长
复制代码
1
2
3
4
5
6
7
8
9CREATE TABLE table_name( column1 datatype contrai, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY(one or more columns) );
复制代码
1
2
3
4
5
6
7例:创建班级表 create table classes( id int unsigned auto_increment primary key not null, name varchar(10) );
复制代码
1
2
3
4
5
6
7
8
9
10
11
12例:创建学生表 create table students( id int unsigned primary key auto_increment not null, name varchar(20) default '', age tinyint unsigned default 0, height decimal(5,2), gender enum('男','女','人妖','保密'), cls_id int unsigned default 0 )
- 修改表-添加字段
复制代码
1
2
3
4alter table 表名 add 列名 类型; 例: alter table students add birthday datetime;
- 修改表-修改字段:重命名版
复制代码
1
2
3
4alter table 表名 change 原名 新名 类型及约束; 例: alter table students change birthday birth datetime not null;
- 修改表-修改字段:不重命名版
复制代码
1
2
3
4alter table 表名 modify 列名 类型及约束; 例: alter table students modify birth date not null;
- 修改表-删除字段
复制代码
1
2
3
4alter table 表名 drop 列名; 例: alter table students drop birthday;
- 删除表
复制代码
1
2
3
4drop table 表名; 例: drop table students;
- 查看表的创建语句
复制代码
1
2
3
4show create table 表名; 例: show create table classes;
最后
以上就是无情鸵鸟最近收集整理的关于MySQL数据库_数据库与数据表操作MySQL数据库的全部内容,更多相关MySQL数据库_数据库与数据表操作MySQL数据库内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复