• MariaDB数据表操作实例


    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.productG;  //查看表的索引
    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
                );
  • 相关阅读:
    android:ViewPager实现Tabs滑动切换效果
    android:实现退出确认对话框
    jsp初探
    struts2获取前台数据的三种方式
    struts表单验证
    SingleTon
    读取文件中内容并统计排序
    android:TabHost总结
    java i/o
    tomcat7.0连接池配置
  • 原文地址:https://www.cnblogs.com/qiyunshe-NSD1805/p/9890937.html
Copyright © 2020-2023  润新知