数据库

    xiaoxiao2022-07-13  167

    数据库

    库操作

    1. 创建数据库

    create database 数据库名 库选项;

    库选项:字符集,校对集 举例:

    create database hello01 charset utf8;

    2. 查询所有数据库

    show databases;

    3. 查看数据库创建语句

    show create database hello01;

    4. 修改数据库的字符集和校对集

    alter database hello01 charset = utf8 collate = utf8_general_ci;

    5. 删除数据库

    drop datebase hello01;

    6. 选择数据库

    use hello01;

    表操作

    1. 创建表

    create table 表名( 字段名称1 字段属性(类型)字段选项, 字段名称2 字段属性 字段选项, …… )表选项;

    表选项: 字符集和数据引擎 字符集:charset = utf8; 数据引擎:默认:innodb, 语法:engine =引擎

    字段名称:创建表中表中的数据的数据的名称,用户自定义就好了但是使用单词。

    字段类型:当前字段中数据存储类型,必须是一开始指定好的。

    整数:int ,small int , tiny int 小数:float :范围大约是-3.4E+38到-1.1E-38,0,和1.1E-38到3.4E+38。dacimal(定点型): decimal(10,2):长度十位,其中小数位两位。

    字符串 char:最大255 ,varchar:65532 _text:无限制 var和varchar的区别:定长型和变长型

    日期时间:date类型:支持的范围是‘1000-01-01’到‘9999-12-31’ ————datetime类型:支持的范围是‘1000-01-01 00:00:00’到‘9999-12-31 23:59:59’

    字段选项: 设置当前字段的一些选项—是否可以为空,是否是主键,是否是唯一键,是否加备注等 null/not null,是否可以为空 default,是否有默认值 auto_increment,是否可以自动增长,必须是数字,如需要给每条数据加一个编号原因是数据内的内容可以重复,但是 编号不可以重复。 primary key:设置主键。数据内容不能重复,在查数据主查询条件时一个表中一个主键,一般都是id. unique key:设定为唯一键,即表中所有行的数据在该字段的值不能有重复。 comment:设置备注,给当前字段设置说明。 举例:

    create table hello_stu( id int not nul auto_increment primary key comment 'id号', name varchar(10) not null default '' comment '姓名', age ine not null defaut 0 comment '年龄', sex varchar(5) not null default '女' comment '性别' )charset utf8 engine = innodb;

    2. 修改表

    添加字段:alter table 表名 add [column] 新字段名 字段类型 [字段属性列表];修改字段(井可改名):alter table 表名 change[column]旧字段名 新字段名 新字段类型[新字段类型属性表];修改字段(只改属性):alter table 表名 modify[column] 字段名 新字段类型 [新字段属性列表];修改字段名:非常抱歉没有单纯的修改字段名这个操作。删除字段:alter table 表名 drop [column] 字段名;添加普通索引:alter table 表名 add index[索引名](字段名1[,字段名2,……];添加主键索引(约束):alter table 表名 add primary key(字段名1[,字段名2,……];添加外键索引(约束):alter table 表名 add foreign key(字段名1[,字段名2,……])references 表名2 (字段名1[,字段名2,……]);添加唯一索引(约束):alter table 表名 add unique(字段名1[,字段名2,……]);添加字段默认值(约束):alter table 表名 alter[column] 字段名 set default 默认值;删除字段默认值(约束):alter table 表名 alter[column] 字段名 drop default删除主键:alter table 表名 drop primary key;#每一个表最多只能有一个主键删除外键:alter table 表名 drop fireign key 外键名;删除索引:alter table 表名 drop index 索引名;修改表名:alter table 表名 rename [to] 新表名;修改表选项:alter table 表名 选项名1=选项值1,选项名2=选项值2……;

    3. 删除表

    drop table [if exists] 表名;

    数据的操作

    1. 插入数据 语句:insert into 表名(字段1,字段2,字段3,……)values(值1,值2,值3,……); 一次性插入多条数据:insert into 表名 (字段列表)values(第一条数据),(第二条数据),……; 举例:

    insert into hello01_stu(id,name.age,sex) values (null,'王丽',12,'女'), (null,'王一',17,'男'), (null,'田甜',22,'女'), (null,'刘梦',20,'女'), (null,'周小',18,'男'), (null,'陈晨',16,'男');

    字段列表可以省略,省略后的数据必须对应上创建时的字段数据:

    insert into hello01_sttu values(null,'刘梦雨',26,'女');

    字段列表可以选择性的写:写几个后面的数据必须是一一对应。

    insert into hello01_sttu(age) value(21);

    2. 删除数据: 语句:delete from 表名[where 条件][order排序][limit限定]; where 条件必须添加否则删除所有数据,建议是id,原因是id具有唯一性,删除大范围的数据。 order:当前表示正序还是倒序,不用添加,默认就可以了。 limit:限定范围,不用添加。

    3. 修改数据:

    update 表名 set 字段名1=值表达式1,字段名2=值表达式2,……where 条件;

    4. 查询数据:

    语句:select 字段列表 [from 子句] 表名[where 子句] [group by 子句] [having 子句][order by 子句][limit 子句] ; 查询所有的数据和字段:select * from 表名; 基础查询——查询某些字段

    select name,age,sex from 表名;

    基础查询——查询别名——小名 如:查询数据的数据量——字段名 as 别名:

    select count(id) as total from hello01_stu; like模糊查询: 字段名 like ‘要查询的字符’;如果是直接写字符,配置查询字符必须一致。 字段名 like ‘%要查询的字符%’;%是匹配任何字符。 举例: set * from hello01_stu where name like '马六'; group by分组: 注意:分组只能是分组后的数量而不能是分组后的组里信息。 select字段或聚合数据(函数)from 表名 group by 分组字段。 常用聚合函数有:取平均值avg(),取个数count(),取总数sum(),取最大值max(),取最小值min(). 举例: select count(sex) from hello01_stu group by sex; select count(sex) as sexnum from hello01_stu group by sex having sexnum; order by排序: 分:正序(asc)和倒序(desc) 举例: select *from 表名 where order by asc/desc; limit:分页查询: 语句:limit0,3; limit 3,3; limit 6,3; 说明:第一个数时开始数据的下标,第二个数是每次显示的数量。 select * from hello01_stu limit 0,3;

    创建好了数据库,表,添加好了数据,我们就可以操作数据库,获得里面的数据啦,O(∩_∩)O哈哈~。

    最新回复(0)