zoukankan      html  css  js  c++  java
  • mysql 入门练习(1)

    //登录和退出数据库

    1:mysql -u root -p//输入密码: 

    3: mysql -u root -p
    //数据库创建,删除,进入数据库

    4CREATE DATABASE `testdatabase`;
    
    5: CREATE DATABASE `testdatabase`;
    
    6DROP DATABASE `testdatabase`;
    
    7: CREATE DATABASE `testdatabase`;
    
    8: USE testdatabase;
    
    9: CREATE DATABASE `testdatabase2`;
    
    10: USE DATABASE testdatabase2;
    
    11: USE `testdatabase2`;
    
    12: USE `testdatabase`;
    
    13: DROP DATABASE `testdatabase2`;

    //表的创建和删除,创建表同时设置属性

    14: CREATE TABLE table1;15: CREATE TABLE table1(No INT);
    
    16: CREATE TABLE table2 (No INT,name VARCHAR(20),sex boolean);
    
    17: DROP TABLE table1,table2;
    
    18: CREATE TABLE tt1 (id INT(11) primary key);1
    
    9: CREATE TABLE tt2 (id int primary key, name varchar(20) unique, age int not null, sex boolean default '1');    //boolean 实际上是一个int,因此值为0或120:
    
    CREATE TABLE tt3 (id int primary key, age int not null auto_increment);
    
    21: CREATE TABLE tt3 (id int primary key auto_increment, age int not null);    //一个表只能有一个auto_increment约束,且约束的属性必须是主键或主键的一部分
    
    22:show tables;
    
    23drop table tt1,tt2,tt3;show tables;

    //key键:    //主键:

    24create table tt1 (id int primary key);
    
    25: create table tt2 (id int,name varchar(20),primary key (id));
    
    26: create table tt3 (id int,name varchar(20),age int,primary key(id,name));
    
    27: create table tt4(id int primary key,age int primary key);//只可以有一个主键
    
    28: drop table tt1,tt2,tt3,tt4;

        //外键:

    29: create table tt1(id int(4),uid int(4));
    
    30: create table tt2(id int(4),uid int(4),constraint nickname foreign key(uid) references tt1(uid));
    
    31: drop table tt1;    create table tt1(id int,uid int,primary key(id,uid));
    
    32: create table tt2(id int,uid int,constraint nickname foreign key(uid) references tt1(id));//由上四条可知外键必须依赖于父表的主键上
    
    33create table tt2(id int(4),uid int(4),constraint nickname foreign key(uid) references tt1(id));    //外键连接必须连接的是主表的主键,如果连接主表主键的一部分,必须连接主键的第一个字段
    
    34: create table tt3(id int,uid int,constraint nick name foreign key(id) references tt1(id));
    
    35: drop table tt1,tt2,tt3;//此处会报错,因为有外键依赖要先删子表才能删父表,如果输入drop table tt3,tt2,tt1;则不会报错
    
    36: create table tt1(Id int unsigned,uid int,primary key(id));
    
    37: create table tt2(id int,uid int,constraint nickname foreign key (uid) references tt1(id));
    
    38: create table tt2(id int,uid int unsigned,constraint foreign key (uid) references tt1(id));    //外键关联的主从表字段数据类型必须完全一样,否则报错“foreign key constraint is incorrectly formed”


    //查看表:

    39:describe tt1;    describe tt2;
    
    40: desc tt1;    desc tt2;
    
    41: show create table tt1;    show create table tt2;
    
    42: show create table tt1 G     show create table tt2 G

    //修改表:    //修改表名

    43alter table tt1 rename tt2;
    
    44: alter table tt1 rename temp;
    
    45: desc tt1;    desc temp;
    
    46: alter table temp rename to tt1;
    
    47: drop table tt1,tt2;

        //修改表属性数据类型

    48: create table tt1(id int,name varchar(20));
    
    49: alter table tt1 modify id varchar(20);50: insert into tt2(id,name) values(1,'string');//插入和删除操作还没有涉及,在这里只是为了加入一条数据已说明下面的问题
    
    51alter table tt1 modify name int;//有值的情况下转换字段类型失败,因为字符串类型无法自动转型为整数类型
    
    52delete from tt1;//删除表中元素
    
    53: alter table tt1 modify name int;    desc tt1;


        //修改表属性

    54alter table tt1 change name newname int;     desc tt1;
    
    55: alter table tt2 change newname name varchar(20);    desc tt2;

        //增加字段

    56: alter table tt1 add name tinytext after id;    desc tt1;
    
    57: alter table tt1 add age int not null after id;desc tt1;
    
    58: alter table tt1 add no int unique first;desc tt1;

        //删除字段

    59alter table tt1 drop age;desc tt1;desc tt1;


        //修改属性排列顺序

    60alter table tt1 modify no int after name;desc tt1;
    
    61: alter table tt1 modify no int first;desc tt1;
    
    62: drop table tt1,tt2;

    //索引:    //创建带索引的数据库表

    63create table tt1(id int,name varchar(20),age int,index index1(id));
    
    64: show create table tt1G
    
    65: explain select * from tt1 where id=1;//explain语句用来查看索引详细情况,参考博文:http://blog.csdn.net/zhuxineli/article/details/144550296666create table tt2(id int,name varchar(20),age int,unique index index2(id));//唯一索引,唯一索引指的是被索引的字段值唯一
    
    67: show create table tt2G
    
    68: create table tt3(id int,name varchar(20),age int,fulltext index index3(id));//全文索引
    
    69: create table tt3(id int,name varchar(20),age int,fulltext index index3(name));
    
    70: show create table tt3G
    
    71: create table tt4(id int,age int,info varchar(50),index index4(info(10)));
    
    72: show create table tt4G
    
    73: create table tt5(id int,name varchar(20),info varchar(50),index index5 (id,name))
    
    ;74: show create table tt5G
    
    75: explain select * from tt5 where name="nnn"G76: explain select * from tt5 where id='1'G
    
        //在已存在表上添加索引//create [unique|fulltext|spatial] index indexname on tablename (columnname [(length)] [ASC|DESC]);
    77: drop database testdatabase;    create database testdatabase;    use testdatabase;
    
    78: create table tt1(id int,name varchar(20));
    
    79: create index index1 on tt1 (id);
    
    80: create index index1 on tt1 (name);//同一表的索引别名不能相同
    
    81: create index index2 on tt1 (name);82: drop index index2 on tt1;83: create table tt2(id int,name varchar(20),age int,sex boolean,info varchar(50));
    
    84: create index index1 on tt2 (id);//不同表的索引别名可以相同
    
    85create unique index index2 on tt2 (id);//不同索引可以重复使用相同字段
    
    86: create fulltext index index3 on tt2 (name);
    
    87: create index index4 on tt2 (sex,age);//多字段索引
    
    88:show create table tt2;
    
    89: drop table tt1,tt2;
    
    90: show tables;
        //Alter语句添加索引    //Alter table tablename add [unique|fulltext|spatial] index indexname (columename [(length)] [ASC|DESC]);
    
    91: create table tt1(id int);
    
    92: alter table tt1 add index index1 (id);
    
    93: create table tt2 (id int,age int,name varchar(20),sex boolean, info varchar(50));
    
    94alter table tt2 add index index2 (id,name);
    
    95: alter table tt2 add unique index index3 (sex);
    
    96: alter table tt2 add index index4 (info(10));
    
    97: alter table tt2 add fulltext index index5 (info(10));    show create table tt2;
        //删除索引
    
    98: drop index index1 on tt2;    show create table tt2;//观察删除索引后的表信息
    
    99drop table tt1,tt2;
  • 相关阅读:
    dotNet程序保护方案
    网络数据包捕获函数库Libpcap安装与使用(非常强大)
    Objectivec 中 nil, Nil, NULL和NSNull的区别
    对象的相等和恒等
    IOS SDK介绍
    iOS内存管理编程指南
    http权威指南读书笔记(三)——http报文
    http权威指南学习笔记(二)
    http权威指南读书笔记(一)
    CentOS 设置环境变量
  • 原文地址:https://www.cnblogs.com/Koi504330/p/11901449.html
Copyright © 2011-2022 走看看