时间:2021-07-01 10:21:17 帮助过:7人阅读
创建数据库testdb用作实验
MariaDB [(none)]> create database testdb character set utf8; #支持中文 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | testdb | +--------------------+
使用数据库testdb
MariaDB [(none)]> use testdb; MariaDB [testdb]> show tables; #查看表 Empty set (0.000 sec) #空表
创建一个classes表,有ip、name两个字段(列),ip为无符号的整型数,设置有主键,name为VARCHAR类型的字符串
MariaDB [testdb]> create table classes (id tinyint unsigned primary key,name varchar(20)); Query OK, 0 rows affected (0.005 sec) MariaDB [testdb]> show tables; +-------------------+ | Tables_in_testdb | +-------------------+ | classes | +-------------------+
查看表的字段
MariaDB [testdb]> desc classes; #describe,描述 +-------+---------------------+------+-----+---------+------+ | Field | Type | Null | Key | Default | Extra| +-------+---------------------+------+-----+---------+------+ | id | tinyint(3) unsigned | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+---------------------+------+-----+---------+------+ #定义 类型 是否为空 是否主键 默认值 扩展
linux当中的注释用#,而数据库中的注释用--
创建一个复杂的students表,包含字段id、name、age、high、gender、cls_id
MariaDB [testdb]> create table students (id tinyint unsigned primary key auto_increment, -> name varchar(20), -> age tinyint unsigned, -> gender enum(‘男‘, ‘女‘, ‘人妖‘,‘保密‘)not null default ‘人妖‘, #枚举,默认为‘人妖’ -> cls_id tinyint unsigned -> ); MariaDB [testdb]> show tables; +------------------+ | Tables_in_testdb | +------------------+ | classes | | students | +------------------+ MariaDB [testdb]> desc students; +--------+-----------------------------+------+-----+----------+---------------+ | Field | Type | Null | Key | Default | Extra | +--------+-----------------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | gender | enum(‘男‘,‘女‘,‘人妖‘,‘保密‘) | NO | | 人妖 | | | cls_id | tinyint(3) unsigned | YES | | NULL | | +--------+-----------------------------+------+-----+---------+----------------+
查看表的创建
MariaDB [testdb]> show create table students; …….. #列出语法 | students | CREATE TABLE `students` ( `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `age` tinyint(3) unsigned DEFAULT NULL, `gender` enum(‘男‘,‘女‘,‘人妖‘,‘保密‘) NOT NULL DEFAULT ‘人妖‘, `cls_id` tinyint(3) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 #默认引擎为InnoDB ………
MariaDB处理引擎有InnoDB、MyISAM、blackhole等,最主要的区别在于InnoDB支持事务处理与外键和行级锁。
2> 改字段
修改表:修改用命令alter
添加一个brithday字段,类型为date
MariaDB [testdb]> alter table students add brithday date; MariaDB [testdb]> desc students; +----------+--------------------------------------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra | +----------+--------------------------------------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | gender | enum(‘男‘,‘女‘,‘人妖‘,‘保密‘) | NO |人妖 | | | | cls_id | tinyint(3) unsigned YES | |NULL | | | | brithday | date | YES | | NULL | |
更改字段名
MariaDB [testdb]> alter table students change brithday birthday date;
更改表的类型
MariaDB [testdb]> alter table students modify birthday datetime;
删除字段
MariaDB [testdb]> alter table students drop birthday;
删除表
MariaDB [testdb]> drop table students;
3> 给字段插入数据
向students表插入数据,value接值,ip为1,name为zxj,字符串类型用单引号引起,age为250,high为250.253,gender为人妖id_cls为1
MariaDB [testdb]> insert into students values (1,‘zxj‘,250.253,‘人妖‘,1);
查看插入的字段
MariaDB [testdb]> select * from students; +----+------+------+--------+--------+ | id | name | age | gender | cls_id | +----+------+------+--------+--------+ | 1 | zxj | 250 | 人妖 | 1 | +----+------+------+--------+--------+ 1 row in set (0.000 sec)
由于id设置的是自增,因此每次插入时id都是增加的,为体现效果,手动插入id为4的一条数据
MariaDB [testdb]> select * from students; +----+------+------+--------+--------+ | id | name | age | gender | cls_id | +----+------+------+--------+--------+ | 1 | zxj | 250 | 人妖 | 1 | | 4 | zxj | 250 | 人妖 | 1 | +----+------+------+--------+--------+
MariaDB [testdb]> insert into students values (0,‘zxj‘,250.253,‘人妖‘,1); Query OK, 1 row affected (0.001 sec) MariaDB [testdb]> select * from students; +----+------+------+--------+--------+ | id | name | age | gender | cls_id | +----+------+------+--------+--------+ | 1 | zxj | 250 | 人妖 | 1 | | 4 | zxj | 250 | 人妖 | 1 | | 5 | zxj | 250 | 人妖 | 1 | +----+------+------+--------+--------+ 3 rows in set (0.000 sec)
MariaDB [testdb]> show create table students; …. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 #当前默认值为为6,因此下一次默认添加数据时自增为6
对部分插入,比如仅对name、age添加数据
MariaDB [testdb]> insert into students (name,age) values (‘wrl‘ ,24); MariaDB [testdb]> select * from students; +----+------+------+--------+--------+ | id | name | age | gender | cls_id | +----+------+------+--------+--------+ | 1 | zxj | 250 | 人妖 | 1 | | 4 | zxj | 250 | 人妖 | 1 | | 5 | zxj | 250 | 人妖 | 1 | | 6 | wrl | 24 | 人妖 | NULL | #gender设置了默认值为‘人妖’ +----+------+------+-------+---------+
删除一条数据
MariaDB [testdb]> delete from students where id=6; #条件表达式where MariaDB [testdb]> select * from students; +----+------+------+--------+--------+ | id | name | age | gender | cls_id | +----+------+------+--------+--------+ | 1 | zxj | 250 | 人妖 | 1 | | 4 | zxj | 250 | 人妖 | 1 | | 5 | zxj | 250 | 人妖 | 1 | +----+------+------+--------+--------+
插入多条数据
MariaDB [testdb]> insert into students (name,age) values (‘wrl‘ ,24),(‘my‘,23); #多条数据以逗号隔开,字符串加单引号 MariaDB [testdb]> select * from students; +----+------+------+--------+--------+ | id | name | age | gender | cls_id | +----+------+------+--------+--------+ | 1 | zxj | 250 | 人妖 | 1 | | 4 | zxj | 250 | 人妖 | 1 | | 5 | zxj | 250 | 人妖 | 1 | | 7 | wrl | 24 | 人妖 | NULL | | 8 | my | 23 | 人妖 | NULL |
4> 修改字段数据,用命令update
修改id>=7的数据的gender为‘保密’
MariaDB [testdb]> update students set gender=‘保密‘ where id>=7; MariaDB [testdb]> select * from students; +----+------+------+----------+-------+ | id | name | age | gender |cls_id | +----+------+------+--------- +-------+ | 1 | zxj | 250 | 人妖 | 1 | | 4 | zxj | 250 | 人妖 | 1 | | 5 | zxj | 250 | 人妖 | 1 | | 7 | wrl | 24 | 保密 | NULL | | 8 | my | 23 | 保密 | NULL | +----+------+------+---------+--------+
由于gender设置是枚举,选项有四个,因此可以用1、2、3、4来代替男、女、人妖、保密 四个选项。
MariaDB [testdb]> update students set gender=1 where id=7; MariaDB [testdb]> update students set gender=2 where id=8; MariaDB [testdb]> update students set gender=4 where name=‘zxj‘; MariaDB [testdb]> select * from students; +----+------+------+---------+----------+ | id | name | age | gender | cls_id | +----+------+------+---------+----------+ | 1 | zxj | 250 | 保密 | 1 | | 4 | zxj | 250 | 保密 | 1 | | 5 | zxj | 250 | 保密 | 1 | | 7 | wrl | 24 | 男 | NULL | | 8 | my | 23 | 女 | NULL | +----+------+------+--------+-----------+
还可以修改多条数据的多个字段(列),如
MariaDB [testdb]> update students set gender=2,age=23 where name=‘zxj‘; Query OK, 3 rows affected (0.001 sec) Rows matched: 3 Changed: 3 Warnings: 0 MariaDB [testdb]> select * from students; +----+------+------+--------+--------+ | id | name | age | gender | cls_id | +----+------+------+--------+--------+ | 1 | zxj | 23 | 女 | 1 | | 4 | zxj | 23 | 女 | 1 | | 5 | zxj | 23 | 女 | 1 | | 7 | wrl | 24 | 男 |NULL | | 8 | my | 23 | 女 |NULL | +----+------+------+--------+--------+ 5 rows in set (0.000 sec)
5> 删除
删除students表中id为4、5的数据
MariaDB [testdb]> delete from students where id=4; MariaDB [testdb]> delete from students where id=5; MariaDB [testdb]> select * from students; +----+------+------+--------+--------+ | id | name | age | gender | cls_id | +----+------+------+--------+--------+ | 1 | zxj | 23 | 女 | 1 | | 7 | wrl | 24 | 男 | NULL | | 8 | my | 23 | 女 | NULL | +----+------+------+--------+--------+
清空表。清空表的内容不要随便用,会清除所有数据。
MariaDB [testdb]> delete from students;
面试:用delete并不会清除自增AUTO_UNCREMENT的值,若要清空表后重置自增值为1,用truncate命令,truncate会清空表的所有内容且不可恢复(不能回滚)。
MariaDB [testdb]> truncate table students;
6> 逻辑删除
用一条字段来表示这条信息是否已经不能在使用了,比如游戏企业通过设置一个字段来表示哪些用户活跃,哪些用户不活跃等。一般企业都将逻辑删除标记为bit(0和1)。逻辑删除并不会删除磁盘数据。
先在students表中添加一个逻辑删除标记ls_delete,bit类型,默认为0
MariaDB [testdb]> alter table students add is_delete bit default 0; MariaDB [testdb]> desc students; ……… | is_delete | bit(1) | YES | | b‘0‘ | MariaDB [testdb]> insert into students (name,age,is_delete) values (‘jony‘,24,1); MariaDB [testdb]> select * from students; +----+------+------+--------+--------+-----------+ | id | name | age | gender | cls_id | is_delete | +----+------+------+--------+--------+-----------+ | 1 | zxj | 23 | 女 | 1 | | #bit=0数值较小,不显示不代表没有 | 7 | wrl | 24 | 男 | NULL | || 8 | my | 23 | 女 | NULL | | | 9 | jony | 24 | 人妖 | NULL | 口 | #bit=1 +----+------+------+--------+--------+-----------+ 4 rows in set (0.000 sec)
用is_delete来表明删除项,如筛选bit不为0的用户
MariaDB [testdb]> select * from students where is_delete=0; +----+------+------+--------+--------+-----------+ | id | name | age | gender | cls_id | is_delete | +----+------+------+--------+--------+-----------+ | 1 | zxj | 23 | 女 | 1 | | | 7 | wrl | 24 | 男 | NULL | | | 8 | my | 23 | 女 | NULL | | +----+------+------+--------+--------+-----------+
MariaDB数据库(二)
标签:引擎 back 更改 增删改 抛出异常 字段名 round std data