1. MariaDB 数据库操作实例

MariaDB>create database class;  //创建class数据库
MariaDB>use class;
MariaDB>create table class.student(  //创建student表
id int(4) primary key,
name varchar(4) not null,
age int(2) not null,
);
MariaDB>desc student;

MariaDB>alter table student add address varchar(48);  //添加address字段
MariaDB>alter table student add gender enum("boy","girl") after age;  //在age字段后面添加gender字段
MariaDB>alter table student change gender  
sex ("boy","girl") not null;  //将gender字段名更改为sex
MariaDB>alert table student drop sex;  //删除sex字段

2. MariaDB 索引创建与删除

MariaDB 索引有普通索引、唯一索引、主键索引、自增主键索引.

MariaDB>create database erp;  //创建erp数据库
MariaDB>create table erp.product(            //创建product表
id int(4) not null,
name varchar(8) not null,
type enum("usefull","bad") not null,
instruction char(8) not null,
index(id),index(name)
);
MariaDB>desc product;
MariaDB>drop index name on product;   //删除name字段的索引
MariaDB>create index shuoming on product(instruction);  //给指定字段创建索引
MariaDB>show inedx from erp.product\G;  //查看表的索引
MariaDB>create table price(              //建表时设置唯一索引
id int(4),
name varchar(4) not null,
TTL int(4) not null,
unique(id),unique(name),index(TTL)
);
MariaDB>desc price;
MariaDB>drop index name on price;    //删除索引
MariaDB>create unique index name on price(name);    //指定字段创建唯一索引

建表时设置主键索引

如果表内没有主键字段, 则新设置的非空唯一索引字段相当于主键索引功能.

每个表的主键字段只能有一个.

MariaDB>create table test1(        //建表时指定主键字段
id int(4) primary key,
name varchar(8)
);
MariaDB>create table test1(        
id int(4),
name varchar(8),
primary key(id)
);
MariaDB>create table test2(          //自增主键
id int(4) auto_incremnet,
name varchar(8) not null,
age int(2) not null,
primary key(id)
); MariaDB>alter table test1 drop primary key;    //删除主键 MariaDB>alter table test2 modify id int(4) not null;    //test2中id字段有自增属性,必须先去掉自增属性才能删除主键
MariaDB>alter table test2 drop primary key;
MariaDB>alter table test2 add primary key(id);    //指定字段添加主键

3. 外键同步更新与删除

MariaDB>create table water(          //创建自增主键的表
w_id int(4) auto_increment,
name varchar(8) not null,
primary key(id)
);
MariaDB>create table river(              //river表中的r_id作为外键,water表中w_id作为参考键
r_id int(4) not null,
name varchar(8) not null,
position float(7,2) not null default 0,
index(name),
foreign key(r_id) references water(w_id) on update cascade delete cascade
);

MariaDB数据表操作实例的更多相关文章

  1. MySQL 数据表操作

    MySQL 数据表操作 创建MySQL数据表需要以下信息: -表名: -表字段名: -定义每个表字段: 一.创建数据表 1)mysql> create  table  table_name (c ...

  2. mysql数据表操作&库操作

    首先登陆mysql:mysql -uroot -proot -P3306 -h127.0.0.1 查看所有的库:show databases; 进入一个库:use database; 显示所在的库:s ...

  3. mysql 数据表操作 目录

    mysql 数据表操作 存储引擎介绍 mysql 使用存储引擎 mysql 数据表的增删改查 mysql 数据类型 mysql 约束条件

  4. Mysql之数据表操作

    数据表操作: 查看当前数据库中所有的表: show tables; 查看当前使用数据库: select database(); 使用数据表: use 表名; 创建数据表: create table 数 ...

  5. 孤荷凌寒自学python第四十九天继续研究跨不同类型数据库的通用数据表操作函数

    孤荷凌寒自学python第四十九天继续研究跨不同类型数据库的通用数据表操作函数 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 不同类型 ...

  6. MySQL5.7(三)数据表操作

    概念在数据库中,数据表是数据库中最重要.最基本的操作对象,是数据存储的基本单位.数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的.每一行代表一条唯一的记录,每一列代表记录中的一个域.1.创 ...

  7. Sql Server系列:数据表操作

    表是用来存储数据和操作数据的逻辑结构,用来组织和存储数据,关系数据库中的所有数据都表现为表的形式,数据表由行和列组成.SQL Server中的数据表分为临时表和永久表,临时表存储在tempdb系统数据 ...

  8. MySQL数据表操作

    创建数据表 打开数据库 USE 数据库名称 mysql> USE D1; Database changed 使用 USE D1:表示打开数据库D1,我们可以通过SELECT DATABASE() ...

  9. MySQL(5):数据表操作

    表名的前缀: 两个student表,为了区分相同逻辑表名的不同应用,给逻辑表名,增加前缀 //学生管理 create table info_student ( name varchar(20), st ...

随机推荐

  1. Spring整合Mybatis原理简单分析

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" ...

  2. layer插件学习——询问框

    本文是自己整理的关于layer插件的询问框样式结果 一.准备工作 下载jQuery插件和layer插件,并引入插件(注意:jQuery插件必须在layer插件之前引用) 百度云资源链接: jQuery ...

  3. 百度全站 https FAQ:技术宅告诉你如何搜索更安全

    百度从 14 年开始对外开放了 https 的访问,并于 3 月初正式对全网用户进行了 https 跳转. 你也许会问,切换就切换呗,和我有啥关系?我平常用百度还不是照常顺顺当当的,没感觉到什么切换. ...

  4. Python制作回合制手游外挂简单教程(上)

    引入: 每次玩回合制游戏的时候,反反复复的日常任务让人不胜其烦 玩问道的时候,我们希望能够自动刷道,玩梦幻希望能自动做师门.捉鬼等等 说明: 该外挂只能模拟鼠标键盘操作,并不能修改游戏数据 我这里使用 ...

  5. POJ 2234 Matches Game (尼姆博弈)

    题目链接: https://cn.vjudge.net/problem/POJ-2234 题目描述: Here is a simple game. In this game, there are se ...

  6. UVa Dropping Balls

    题目链接: https://cn.vjudge.net/problem/UVA-679 /* 问题 输入完全二叉树的层数D和有几个小球滚落,计算最后一个小球落入的叶子结点的小号. 解题思路 直接模拟超 ...

  7. zoj 1109 Language of FatMouse(map映照容器的典型应用)

    题目连接: acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1109 题目描述: We all know that FatMouse doe ...

  8. 网络之XML解析-GData

    GDataXML是一套Google使用Objective-C开发的DOM方式XML解析类库,支持读取和修改XML文档,支持XPath方式查询, GDataXML 是 iOS 下的一款轻量级 XML 解 ...

  9. VS2012 生成项目报 "Lc.exe已退出,代码为-1" 错误

    解决方法:删除项目下Properties文件下的license.licx文件即可.

  10. Net Core 中间件实现修改Action的接收参数及返回值

    新一个WebApi项目(Net Core 2.1) 新建InputOutputAlterMiddleware类,修改命名空间为Microsoft.AspNetCore.Builder(不修改也没关系, ...