mysql管理(一)

时间:2023-03-09 09:36:46
mysql管理(一)

创建数据库,并指定默认字符集和排序规则:
help create database;
create {database|schema} [if not exists] db_name [character set=] [fieldlate=];

删除数据库:
help drop database;
drop {database|schema} [if exists] db_name;

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

创建表三种方式:
help create table
    1、直接定义一张空表;
    2、以其他表中查询出数据,并以之创建新表;(字段属性将不存在)
    3、以其他表为模板创建一个空表;
    4、自动增长的字段为主键(不能为空)

1、
create    table [if not exists] tb_name (field_name field_defination, index)';

一个字段为主键:字段,字符类型,修饰符,不为空,自动增长,主键,字段,字符集,不为空
create table tb1 (id int unsigned not null auto_increment primary key, Name char(20) not null, Age tinyint not null)';
联合字段为主键:
create table tb1 (id int unsigned not null auto_increment, Name char(20) not null, Age tinyint not null, primary key(id,name))';
唯一键,索引:
create table tb2 (id int unsigned not null auto_increment, Name char(20) not null, Age tinyint not null, primary key(id),Unique key(name),Index(age))';

2、
create table testcourses select * from courses where cid <= 2;

3、
create table test2courses like courses;
如果需要相同格式,首先用desc创建相同格式空表,然后查询数据导入进去:

修改表定义:
    help alter table;
        查看、添加、删除、修改字段
            desc tb_name;
            alter table tb_name add field field_type [not null][first|after field];
            alter table tb_name drop  field;
            alter table tb_name change field new_field field_type [not null][first|after field];_
        查看、添加、删除索引
            show indexes from tb_name;
            create index index_name on tb_name (field_name...);
            field_name [(length)] [ASC | DESC]
            drop index index_name on tb_name
        更改表名
            alter table tb_name rename to new_tb_name;    |    rename table tb_name to new_tb_name;
        修改表属性
更改表引擎:
    alter table tb_name ENGINE=InnoDB;

table option:
    存储引擎:ENGINE=MyISAM|InnoDB

单独定义索引:
    单字段:
        primary key
        unique key
    单或多字段:
        primary key (field,...)
        unique key (field,...)
        index (field,...)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

DML:
    select
    insert into
    delete
    update

select

    select select-list from tb where qualification;

    查询语句类型:
        简单查询
        多表查询
        子查询

    select * from tb_name;
    select [distinct] field,field2 from tb_name;    投影
    select * from tb_name where qualification;    选择

    from子句:要查询的关系            表、多个表、其他select语句
    where子句:布尔关系表达式
        =、>、 >=、 <=、 <
        逻辑关系:
            and
            or
            not
            between ... and ...
            like ''
            %:任意长度任意字符
            _:任意单个字符
            支持正则表达式
            RLIKE,REGEXP
            in
            is null
            is not null

    order by field_name {ASC|DESC}:排序
    LIMIT子句:LIMIT[offset,]number
    聚合:SUM(),MIN(),MAX(),AVG(),COUNT()

insert into
    insert into tb_name (col1, col2, ...) values (val1, val2, ...)[,(val1, val2, ...),lll]
        字符型:单引号
        数值型:不需要引号
        日期时间型:不需要引号
        控制:NULL
replace into:
    1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。 2. 否则,直接插入新数据。