day 34 数据库 表 记录

操作表

语法:

create table 表名(
    字段名 列类型 【约束条件】,#记住加逗号
    字段名 列类型 【约束条件】,#记住加逗号
    字段名 列类型 【约束条件】 #最后一行不加逗号
    )charset=utf8;#加分号

列约束:

auto_increment:自增1
primary key: 主键索引,加快查询速度,列的值不能重复
not null:标识该字段不能为空
default :为该字段设置默认值
create table t1(
    id int unsigned auto_increment primary key,
    name char(10) not null default 'xxx',
    age int not null default 0
)charset=utf8;

列类型:

-数字

​ -整形

​ tinyint

​ smallint

​ int (********************************************************)推荐使用

​ mediumint

​ bigint

​ a.整数类型

​ b.取值范围

​ c. unsigned 加上代表不能取负数,只适用于整形

​ 应用场景:

​ 根据公司业务,来选取合适的类型

​ -浮点型(*****************)

​ float: 不一定精准

​ decimal:非常的精准的数字 例如:decimal(6,2) 6表示数字总个数,2表示小数点后个数。

create table t5(
    id int auto_increment primary key,
    salary decimal(16,10),
    num float
)charset=utf8;

字符串

-char(长度):定长

create table t6(
    id int auto_increment primary key,
    name char(10) not null default 'xxx'
 )charset=utf8;

-varchar(长度):变长

create table t6(
    id int auto_increment primary key,
    name varchar(10) not null default 'xxx'
 )charset=utf8;

区别:

​ char:定长,无论插入的字符是多少个,永远固定占规定的长度

​ 场景:

​ 1.身份证

​ 2.手机号 char(11)

​ 3.md5加密之后的值

​ varchar:变长,根据插入的字符串的长度来计算所占的字节数,但是有一个字节是用来保存字符串的大小的

​ 注意:如果,不能确定插入的数据的大小 ,一般建议使用varchar(255)

时间日期类型

year

​ YYYY(1901)

date

​ YYYY-MM-DD(1000-01-01)

time

​ HH:MM:SS(12:12:12)

datatime(*************************************************************)

​ YYYY-MM-DD HH:MM:SS(1970-01-01 00:00:00)

create table t8(
    d date,
    t time,
    dt datatime
);
insert into t8 values(now(),now(),now());

枚举

​ 列出所有的选项

create table t9(
    id int auto_increment primary key,
    gender enum('male','female')
)charset=utf8;

drop table 表名;# 线上禁用
drop table t9;

1.修改表名

alter table 旧表名 rename 新表名

alter table t8 rename t88;

2.增加字段

#添加的列永远是添加在最后一列之后
alter table 表名
add 字段名 列类型 【约束条件】,
add 字段名 列类型 【约束条件】;

alter table t88 
add name varchar(32) not null default '';

#添加的列添加在第一列
alter table 表名
add 字段名 列类型 【约束条件】 first;

#添加的列添加在某一列下面
alter table 表名
add 字段名 列类型 【约束条件】 after 字段名;

3.删除字段

alter table 表名 drop 字段名;

alter table t88 drop name4;

4.修改字段

alter table 表名 modify 字段名 数据类型 【约束条件】;

alter table t88 modify name2 char(20);

alter table 表名 change 旧字段名 新字段名 新数据类型 【约束条件】;

alter table t88 change name2 name22 varchar(32) not null default '';

show tables;
show create table t88;

复制表结构

create table t89 like t88;

操作表数据行

语法:
    insert into 表名(列1,列2) values (值1,值2),(值1,值2);
 
insert into t1 (id,name) values (1,'nick');

delete from 表名 where 条件;

delete from t5 where id=1;
delete from t5 where id>1;
delete from t5 where id>=1;
delete from t5 where id!=1;


truncate 表名;
truncate t5;

区别:

1.delete之后,插入数据从上一次主键自增加1开始,truncate则是从1开始

2.delete删除,是一行一行的删除。truncate是全选删除,truncate删除的速度是高于delete的

update 表名 set 列名=新值1,列名=新值2 where 条件;

update t66 set name='xxxx' where id=30;
update t66 set name='xxxx' where id=>30;
update t66 set name='xxxx' where id=>30 and  id<=35;

语法:
    select 列1,列2 from 表名;#*代表查询所有的列
    select * from t66;
    select * from t66 where id>20;
 
between..and.. :取值范围是闭区间
    select * from t66 where id between 30 and 40;
    
避免重复 distinct
    select distinct name from t66;
   
通过四则运算查询(不要用)
    select name ,age*10 from t3;
    select name ,age*10 as age from t3;
    
in(80,90,100)
    select * from t66 where id in (23,34,11);
    
 like:模糊查询(不让用)
    select * from t66 where name like 'x%';
    select * from t66 where name like '%x';
    select * from t66 where name like '%x%';

猜你喜欢

转载自www.cnblogs.com/zqfzqf/p/11760876.html