当前位置:网站首页>MySQL表管理(文件)
MySQL表管理(文件)
2022-06-10 02:00:00 【宝宝很可爱】
MySQL数据表的管理(文件)
1、进入数据库(进入文件夹)
- use 数据库名字;
use gx_day14;
2、查看当前数据库下的所有 表(文件)
show tables;
3、创建表(文件文件)
格式:
create table 表名称(
列名称 类型,
列名称 类型,
列名称 类型
)default charset=utf8;
create table tb1(
id int,
name varchar(16),
age int
) default charset=utf8;
每行后面还可以加更多的信息:
create table tb1(
id int,
name varchar(16) not null, -- 不允许为空
age int null, -- 允许为空(默认)
) default charset=utf8;
create table tb1(
id int,
name varchar(16),
age int default 3 -- 插入数据时,age列的值默认3
) default charset=utf8;
create table tb1(
id int primary key, -- 主键(不允许为空,不允许重复)
name varchar(16),
age int
) default charset=utf8;
主键一般用于表示当前行的数据的编号(类似于人的身份证)。
自增:id这列,从1开始,依次自动加一,系统自动添加,id这一列我们不用管,自动传数据。
create table tb1(
id int auto_increment primary key, -- 内部维护,自增
name varchar(16),
age int
) default charset=utf8;
一般情况下,我们再创建表时都会这样来写:【标准】
create table tb1(
id int not null auto_increment primary key,//非空,自增,主键
name varchar(16),
age int
) default charset=utf8;
4、查看表属性 desc 表名;
mysql> desc tb1;
5、删除表
drop table 表名称;
6、常用数据类型:
6.1、整型:tinyint,int,bigint
- tinyint
有符号,取值范围:-128 ~ 127 (有正有负)【默认】
无符号,取值范围:0 ~ 255(只有正)
create table tb2(
id int not null auto_increment primary key,
age tinyint -- 有符号:取值范围:-128 ~ 127
) default charset=utf8;
create table tb3(
id int not null auto_increment primary key,
age tinyint unsigned -- 无符号:取值范围:0 ~ 255
) default charset=utf8;
int
int 表示有符号,取值范围:-2147483648 ~ 2147483647
int unsigned 表示无符号,取值范围:0 ~ 4294967295bigint
有符号,取值范围:-9223372036854775808 ~ 9223372036854775807
无符号,取值范围:0 ~ 18446744073709551615
6.1.1、小练习:
创建表
create table tb2(
id bigint not null auto_increment primary key,
salary int,
age tinyint
) default charset=utf8;
插入数据
insert into tb2(salary,age) values(10000,18);
insert into tb2(salary,age) values(20000,28); //单行插入
insert into tb2(salary,age) values(30000,38),(40000,40); //批量插入
查看表中的数据
select * from tb2;
mysql> show tables;
+--------------------+
| Tables_in_gx_day14 |
+--------------------+
| tb1 |
+--------------------+
1 row in set (0.00 sec)
mysql> create table tb2(
-> id bigint not null auto_increment primary key,
-> salary int,
-> age tinyint
-> ) default charset=utf8;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+--------------------+
| Tables_in_gx_day14 |
+--------------------+
| tb1 |
| tb2 |
+--------------------+
2 rows in set (0.00 sec)
mysql> insert into tb2(salary,age) values(10000,18);
Query OK, 1 row affected (0.00 sec)
mysql> insert into tb2(salary,age) values(20000,28);
Query OK, 1 row affected (0.00 sec)
mysql> insert into tb2(salary,age) values(30000,38),(40000,40);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from tb2;
+----+--------+------+
| id | salary | age |
+----+--------+------+
| 1 | 10000 | 18 |
| 2 | 20000 | 28 |
| 3 | 30000 | 38 |
| 4 | 40000 | 40 |
+----+--------+------+
4 rows in set (0.00 sec)
6.2、小数:float double
decimal:准确的小数值,decimal(m,d)
m是数字总个数(负号不算),d是小数点后个数。
m最大值为65,d最大值为30。
create table tb3(
id int not null primary key auto_increment,
salary decimal(8,2) //decimal(m,d)
)default charset=utf8;
insert into tb3(salary) values(1.28);
insert into tb3(salary) values(5.289);
insert into tb3(salary) values(5.282);
insert into tb3(salary) values(122115.11);
select * from tb3;
6.3、字符串:放文本,日期等
6.3.1、char(m),速度快。
定长字符串,m代表字符串的长度,最多可容纳255个字符。
char(11),固定用11个字符串进行存储,哪怕真是没有11个字符,也会按照11存储。
create table tb4(
id int not null primary key auto_increment,
mobile char(11) //手机号 固定11位
)default charset=utf8;
insert into tb4(mobile) values("151");
insert into tb4(mobile) values("15131255555");
6.3.2、varchar(m),节省空间。动态变化
变长字符串,m代表字符的长度。 最大65535字节/3 = 最大的m (一个中文占三个字节)
varchar(11),真实数据有多少长久按照多长存储。
create table tb5(
id int not null primary key auto_increment,
name varchar(11) //用户名 长度不固定
)default charset=utf8;
insert into tb5(name) values("151");
insert into tb5(name) values("15131255555");
6.3.3、text
text数据类型用于保存 变长的大字符串,可以最多到65535 (2**16 − 1)个字符。
一般情况下,长文本会用text类型。例如:文章、新闻等。
create table tb6(
id int not null primary key auto_increment,
title varchar(128),
content text
)default charset=utf8;
6.3.4、不常用:
- mediumtext
A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters.
- longtext
A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1)
6.3.5、时间字符串
- datetime
YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59)
- date
YYYY-MM-DD(1000-01-01/9999-12-31)
6.4、练习题:用户表
create table tb7(
id int not null primary key auto_increment,
name varchar(64) not null,
password char(64) not null,
email varchar(64) not null,
age tinyint,
salary decimal(10,2),
ctime datetime
)default charset=utf8;
insert into tb7(name,password,email,age,salary,ctime) values("张三","123","[email protected]",19,1000.20,"2011-11-11 11:11:10");
insert into tb7(name,password,email,age,salary,ctime) values("张电摩","123","[email protected]",19,1000.20,"2011-11-11 11:11:10");
insert into tb7(name,password,email,age,salary,ctime) values("庞小青","123","[email protected]",19,1000.20,"2011-11-11 11:11:10");
insert into tb7(name,password,email,age,salary,ctime) values("谢涛","123","[email protected]",19,1000.20,"2011-11-11 11:11:10");
insert into tb7(name,password,email,age,salary,ctime) values("谢鹏","123","[email protected]",19,1000.20,"2011-11-11 11:11:10");
select * from tb7;
+----+-----------+----------+-------------+------+---------+---------------------+
| id | name | password | email | age | salary | ctime |
+----+-----------+----------+-------------+------+---------+---------------------+
| 1 | 张三 | 123 | xx@live.com | 19 | 10000.20 | 2011-11-11 11:11:10 |
+----+-----------+----------+-------------+------+---------+---------------------+
1 row in set (0.00 sec)
MySQL还有很多其他的数据类型,例如:set、enum、TinyBlob、Blob、MediumBlob、LongBlob 等,详细见官方文档:https://dev.mysql.com/doc/refman/5.7/en/data-types.html
开发系统时,
- 创建数据库
- 创建表结构
都需要通过上述命令创建。
边栏推荐
- [email protected] 项目实训
- Use primecache to speed up your computer!
- The transfer of production lines to Vietnam may not be a good choice. Samsung mobile phones have been affected and plan to return to South Korea
- Flutter series: foundation of material theme - materialapp
- Word 教程,如何在 Word 中应用样式、主题?
- 服务器停止响应是什么意思,该如何排查?
- Allan方差定義與計算方法簡介
- 起薪1800,三年来手头只积攒一万块,裸辞奔赴一线城市月薪达3万
- D materialize function parameters
- [FPGA] day16 FIFO implements UART protocol
猜你喜欢

在keras中使用gpu加速训练模型;安装cuda;cudnn;cudnn_cnn_infer64_8.dll 不在path中;device_lib.list_local_devices无gpu;挂掉
[email protected]和[email protected]装载5-Fu(5-氟尿"/>[email protected]和[email protected]装载5-Fu(5-氟尿

LeetCode 700:二叉搜索树中的搜索

IDEA 版 Postman问世,亲测好用
[email protected] 项目实训"/>[email protected] 项目实训

Nodejs reported an internal error typeerror: cannot read property 'destroy' of undefined

LabVIEW displays the time and date on the waveform chart or waveform chart

2022 mobile crane driver test questions simulation test platform operation

机智云轻网关服务,提升生产管理效率
[email protected] 项目实训"/>[email protected] 项目实训
随机推荐
Esp32-c3 single fire wire intelligent switch enables intelligent upgrading of traditional switch
[untitled]
DV和OV SSL证书哪个适合公司网站购买
[neural network] (22) convmixer code reproduction, network analysis, complete tensorflow code attached
A small problem in installing composer
[從零開始學習FPGA編程-16]:快速入門篇 - 操作步驟2-4- Verilog HDL語言描述語言基本語法(軟件程序員和硬件工程師都能看懂)
Webmaster Tools browser SEO plug-in - Webmaster Essential Tools SEO website ranking quick view website data
【LeetCode】461. Hamming distance
With all due respect: programmers spend most of their time not writing code, but...
iNFTnews | 元宇宙进行时:那些跑步入场的互联网大厂在如何谋篇布局?
Introduction à la définition et au calcul de la variance Allan
Mongodb learning and sorting (concept analysis)
LeetCode 700:二叉搜索树中的搜索
[从零开始学习FPGA编程-16]:快速入门篇 - 操作步骤2-4- Verilog HDL语言描述语言基本语法(软件程序员和硬件工程师都能看懂)
安装composer 的一点小问题
【FPGA】day15-串口协议uart回环工程
ZMQ communication
Metauniverse can open a new thinking window for the Internet which is falling into a bottleneck period
两道软件测试题目
OpenSUSE auto mount hard disk