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. 在自己网站中引入CU3ER/Flash 3D幻灯片效果和照片画框

    要求 必备知识 本文要求基本了解 JAVASCRIPT 和 XML基础语法知识. 运行环境 支持Flash Player的浏览器/Flash Player11及以上 演示地址 演示地址 下载地址   ...

  2. Chapter 3 Phenomenon——24

    My mom was in hysterics, of course. 我的母亲当时是歇斯底里的发疯了. I had to tell her I felt fine at least thirty t ...

  3. java工具类-日期工具类

    1.获得时间戳 为了统一其他语言的如php和unix系统获取的时间戳是10位长度的,精确到秒. java时间戳长度是13位,精确到毫秒 我们获取时间戳需要相应处理. //获取当前时间戳,除以1000, ...

  4. 理解Golang包导入

    Golang使用包(package)这种语法元素来组织源码,所有语法可见性均定义在package这个级别,与Java .python等语言相比,这算不上什么创新,但与C传统的include相比,则是显 ...

  5. Andrew Ng机器学习课程笔记(四)之神经网络

    Andrew Ng机器学习课程笔记(四)之神经网络 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7365730.html 前言 ...

  6. Go常量与运算符

    常量的定义 常量的值在编译时就已经确定 常量的定义格式与变量基本相同 等号右侧必须是常量或者常量表达式 常量表达式中的函数必须是内置函数 package main import ( "fmt ...

  7. ASP.NET MVC5+EF6+LayUI实战教程,通用后台管理系统框架(5)- 创建项目结构

    前言 关于理论知识,我的表达能力有限,知识水平有限,就不过多的讲解编程工作中的专用术语了,大家写的代码多了,自然就懂了 前几节课,我们看到了后台的主页面,以及一个自认为比较漂亮的登录界面,算是编程套路 ...

  8. 微信公众号开发笔记1-获取Access Token

    获取你的Access Token a)可以采用网址的形式: 用appid和appsecert获得access token,接口为https://api.weixin.qq.com/cgi-bin/to ...

  9. [总结]多项式求逆代替分治 $\text{FFT}$

    目录 问题提出 求逆代替分治 代码实现 由于我懒得不想学蠢得学不会分治 \(\text{FFT}\) ,发现可以用多项式求逆来完整地代替... 文章节选自分治 FFT 与多项式求逆,转载方便自己查看. ...

  10. Eclipse 4.3 Kepler最快汉化方法

    eclipse 4.3汉化 eclipse 4.3 ---kepler已经于2013年6月26日发布主要版本,详见:eclipse in wikipedia   1.上eclipse官网:http:/ ...