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. Vue源码翻译之渲染逻辑链

    本篇文章主要要记录说明的是,Vue在Vdom的创建上的相关细节.这也是描绘了Vue在界面的创建上的一个逻辑顺序,同时我也非常拜服作者编码的逻辑性,当然或许这么庞大复杂的编码不是一次性铸就的,我想应该也 ...

  2. 使用ssh-add命令添加ssh私钥时报错

    当使用ssh-add命令添加ssh私钥时,报如下错误: Could not open a connection to your authentication agent. 其实需要先执行如下命令: e ...

  3. mysql数据库中实现内连接、左连接、右连接

    原文:http://www.cnblogs.com/xwdreamer/archive/2010/12/15/2297058.html 内连接:把两个表中数据对应的数据查出来 外连接:以某个表为基础把 ...

  4. MySQL和Mariadb二进制日志binlog详解

    Mariadb/mysql提供了4中不同的日志,分别是错误日志(error.log).普通日志(general log).慢日志(slow log)以及二进制日志(binlog).错误日志记录了系统启 ...

  5. Netty 核心组件 Pipeline 源码分析(二)一个请求的 pipeline 之旅

    目录大纲: 前言 针对 Netty 例子源码做了哪些修改? 看 pipeline 是如何将数据送到自定义 handler 的 看 pipeline 是如何将数据从自定义 handler 送出的 总结 ...

  6. Linux创建用户等操作

    转自: https://www.linuxidc.com/Linux/2017-06/144916.htm 与大家分享下Linux系统中创建用户.设置密码.修改用户.删除用户的命令,希望对你有所帮助. ...

  7. Alice and Bob(博弈)

    Alice and Bob Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users ...

  8. JAVA设计模式详解(二)----------观察者模式

    有一个模式可以帮助你的对象知悉现况,不会错过该对象感兴趣的事,对象甚至在运行时可以决定是否要继续被通知,如果一个对象状态的改变需要通知很多对这个对象关注的一系列对象,就可以使用观察者模式 .观察者模式 ...

  9. Spring、Springmvc整合web的web.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  10. jQuery的$.getScript方法去加载javaScript文档解析

    1.两个文件的代码如下: <script> function Ajax(){ //将9-4.html中的Ajax()函数进行修改 $.getScript('9-8.js',function ...