针对表的SQL语句、针对记录的SQL语句、存储引擎、数据类型、创建表的完成语法

针对表的SQL语句

  有表的前提是先有库

什么是表?

  表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段

select database();查看当前所在库

use   库名; 使用库

1.查看表

  show tables;查看那所有表

  show create table  t1;查看表结构

  describe t1;格式化之后的表结构

  desc t1;同上

2.创建表

  create table t1 (id int,name varchar(20),age int);

3.修改表

  alter table 旧表名 rename 新表名

4.删除表

  drop table  表名;

针对记录的SQL语句

select、insert、update、delete

1. 查看表中的数据

  select * from t1;

  select id from t1;

  select id ,name from t1;

  select id,name,age from t1;

2.增加数据

  insert into t1 values (1,‘kevin’,18);单条增加数据,并且是全字段增加

  insert  into t1  values (2,‘tank’,20),(3,‘oscar’,21),(4,‘jack’,22);

  insert into t1 (id ,name)values (5,‘jerry’);

3.修改数据

  update 表名 set 字段名=‘字段值’where 条件;

  update t1 set name=‘kevin’ where age=18;

  update t1 set name=‘jason1’ , age=19 where id=2;

  update t1 set name=‘jason’,age=20,k=v,k1=v1 where id=3

  update t1 set name=‘aaa’,全表改 ,工作不能使用

4.删除数据

  delete from t1 where id=1

  delete from t1 where id=1 and name=‘kevin’;

存储引擎

  存储数据的方式

MySQL 中有哪些存储方式

  show engines;或者 show engines\G

  总共支持九种存储引擎

需要掌握:

  myisam:它是MySQL5.5以前的版本默认的存储引擎,它的存取速度更快,但数据相对innodb不够安全

  innodb;它是MySQL5.6以前的版本默认的存储引擎,他的存取速度相对于myisam更慢,但数据相对于更安全

需要了解:

  memory:他的数据在内存中存储,断电数据丢失

验证以上三者存储引擎所产生文件;

create table t2 (id int)engine=MyISAM

create table t3 (id int)engine=InnoDB

create table t4 (id int)engine=MEMORY

  MyISAM:

   产生三个文件

   .frm:这个文件存表结构

   .MYD:这个文件存数据

   .MYI:这个文件存数据索引 index》》类似于书的目录》》加快查询速度

  InnoDB:

   产生两个文件

   .frm:这个文件存表结构

   .ibd :存索引和数据

  MYMORY:

   产生一个文件

   .frm:存表结构

数据类型

1.整型

  tinyint、smallint、mediumint、int、bigint

  不同类型存储的范围不一样

存储范围比较

  tinyint:1个字节----8位----2**8----256----0-255----—128-127

  smallint:2个字节----16----2**16----65536----0-65535----—32768-32767

  int:4个字节

  bigint:8个字节

默认情况下整型是否带-号

  create table 表名(id tinyint);

  create table 表名(id smallint);

  create table 表名(id int);

  create table 表名 (id bigint);

   insert into 表名 values(999);

  insert into 表名 values(-129);

ps:默认整型是带符号的

2.浮点型

  小数

float、double、decimal

语法格式:

  float(255,30);255表示存储位数,30代表的是小数位数

  double(255,30);255表示存储位数,30代表的是小数位数

  decimal(65,30);65表示存储位数,30代表的是小数位数

  decimal(8,2)表示最大范围999999.99

三者区别:

  create table 表名(id float(255,30));

  create table 表名 (id double(255,30));

  create table 表名 (id decimal(65,30));

 ps:精准度不一样,decimal》》double》》float

3.字符串

char(4)、varchar(4)

这两个都是用来存储字符串的

char(4):定长的,他就存4位,如果没有超过4位,空格填充到4位,超出报错或者最大存4位

varchar(4):可变长的,不超4位,有几位存几位,超4位报错或者最大存4位

验证

  create table 表名 (id int,name char(4));

  create table 表名(id int, name varchar(4));

想要报错,必须设置成严格模式

查看严格模式:

  1.select @@sql_mode;

  2.show variables like ‘%mode%’ 模糊查询,变量中带有mode的

设置严格模式:

  1.永久修改:需要改配置文件

  2.临时修改:

    set global

    sql_mode=‘STRICT_TRANS_TABLES,PAD_CHAR_TO_FULL_LENGTH’;

研究定长和变长

  insert into 表名 values(2,‘k’);

  insert into 表名 values(2,‘k’);

验证长度:

  select char _length(name) from 表名;

  select char_length(name) from 表名;

ps:大多数情况使用varchar(),存储数据是固定的就用char()

4.日期类型

datetime、date、time、year

  create table t14 (id int,
          reg_time datetime,
          update_time date,
          delete_time time,
          birth year
          );

  insert into 表名 values (1,‘2023-10-01 11:11:11,‘2023-10-01’,‘11:11:11’,'2023');

5.枚举类型

enum、set

enum:多选一

  create table 表名 (id int,hobby enum(‘tangtou’,‘hejiu’,‘xijiao’))

  insert into 表名values(1,‘tangtou’)

set:多选多

  create table 表名 (id int,hobby set(‘tangtou’,‘hejiu’,‘xijiao’))

  insert into 表名values(1,‘tangtou’,‘hejiu’,‘xijiao’)

整型中的数字代表什么意思

char(4)和varchar(4)中的数字表示存储长度

整型的存储范围与括号中数字没有关系,与关键字int tinyint、smallint等关键有关

  create table t11 (id int(3));
  insert into t11 values(99999);

整型中括号中的数字代表的是数据展示的位数

  create table t12 (id int(3) zerofill);
  insert into t12 values(9);

  create table t13 (id int(11) zerofill);
  insert into t13 values(9);

创建表的完成语法结构

create table 表名(

        字段名 数据类型 约束条件 约束条件 约束条件,

        字段名1 数据类型 约束条件 约束条件 约束条件,

        字段名2 数据类型 约束条件 约束条件 约束条件

        );

create table t17 (
        id int,
        name varchar(16),
        age int
        );

注意事项:

1. 字段名和数据类型是必须要写的
2. 约束条件是可选的,有就写,没有不写
3. 最后一个字段的末尾不能加逗号

 

posted @   Py玩家  阅读(86)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示