• MySQL增删查改语句(入门)


    数据库定义语句:

    1. create:创建数据库及表对象
    2. drop:删除数据库及表对象
    3. alter:修改数据库及表对象

    数据库控制语句:

    1. insert:向表中插入数据
    2. delete:将表中数据删除
    3. update:更改表中数据
    4. select:检索表中数据

    数据库管理语句:

    1. grant:给用户授权
    2. revoke:回收用户权限

    create

    mysql> create table customers
        -> (
        -> cust_id int(4) primary key not null auto_increment,  #创建表格,指定了主键,不允许为空,自增张
        -> cust_name char(20) not null,  
        -> cust_sex char(5) not null,
        -> cust_add char(20) not null default 'china',  #默认值为'China'
        -> cust_city char(20) not null default 'beijing'
        -> );
    
    

    alter:

    1. MariaDB [test]> alter table test.customers
        -> change column cust_sex sex char(2) null default 'M';
        //修改表中字段名称及数据类型,change子句。
    
    2. MariaDB [test]> alter table test.customers
        -> alter column cust_city set default 'hangzhou';
        //alter子句用来修改表中字段的默认值的。
        
    3. MariaDB [test]> alter table test.customers
        -> modify column cust_name char(20) first;
        //modify子句用来修改表中字段的数据类型,first表示将这个字段设为第一列。
        
    4. MariaDB [test]> alter table test.customers
        -> drop column sex;
        //drop子句用来删除表中字段(列,一个意思,别混淆)
    
    MariaDB [test]> desc test.customers;
    +--------------+-------------+------+-----+----------+----------------+
    | Field        | Type        | Null | Key | Default  | Extra          |
    +--------------+-------------+------+-----+----------+----------------+
    | cust_name    | char(20)    | YES  |     | NULL     |                |
    | cust_id      | int(11)     | NO   | PRI | NULL     | auto_increment |
    | cust_add     | varchar(50) | NO   |     | fd       |                |
    | cust_city    | char(10)    | NO   |     | hangzhou |                |
    | cust_contact | char(50)    | NO   |     | NULL     |                |
    +--------------+-------------+------+-----+----------+----------------+
    
    5. MariaDB [test]> alter table test.customers
        -> rename to test.customers_backup;
        MariaDB [test]> rename table test.customers_backup to test.customers;
        //rename子句用来修改表的名称
        
    6. MariaDB [test]> show tables from test;
       //查看test库下表
       
    7. MariaDB [test]> show columns from test.customers;
       //查看该表的字段以及数据类型
    
    

    insert

    MariaDB [(none)]> insert into
        -> test.customers 
        -> values
        -> ('zhangsan',101,'qiao','BeiJing','Ali');
    	//向表中插入数据,千万要注意符合设定的约束即数据类型,字符要加引号。
    
    MariaDB [(none)]> insert into test.customers
        -> set cust_name='111',cust_add='WU';
    	//插入单个或多个指定字段的数据。
    
    还有一个用法就是,insert……select,将从某个表对象查询的表插入到这个表对象中,需要注意的是,要注意数据类型一致性
    

    delete

    MariaDB [(none)]> delete from test.customers 
        -> where cust_name='lisi';
        //删除满足条件的行记录
    

    update

    MariaDB [(none)]> update test.customers
        -> set cust_add='ShanTou'
        -> where cust_name='zhangsan';
    

    select

    MariaDB [(none)]> select cust_name from test.customers where cust_name='qiao1';
    	//查询指定字段,并满足where子句的条件。
    
    MariaDB [(none)]> select cust_name,cust_add as dizhi from test.customers;
    +-----------+-------+
    | cust_name | dizhi |
    +-----------+-------+
    | qiao1     | BJ    |
    | qiao2     | CD    |
    +-----------+-------+
    //as后起一个别名,并不改变表中数据
    
    MariaDB [(none)]> select cust_name, case when cust_add='BJ' then 'BeiJing' else 'other city' end as dizhi from test.customers;
    +-----------+------------+
    | cust_name | dizhi      |
    +-----------+------------+
    | qiao1     | BeiJing    |
    | qiao2     | other city |
    +-----------+------------+
    
    
    MariaDB [(none)]> select cust_name,cust_id+1000000000000000000000000000000 from test.customers;
    +-----------+-----------------------------------------+
    | cust_name | cust_id+1000000000000000000000000000000 |
    +-----------+-----------------------------------------+
    | qiao1     |         1000000000000000000000000000111 |
    | qiao2     |         1000000000000000000000000000112 |
    +-----------+-----------------------------------------+
    
    
  • 相关阅读:
    [php]php设计模式 (总结)
    MySql常用命令总结
    mysql常用命令
    搜集几个API接口
    c语言 11-7
    c语言中转换字符串函数 atoi函数
    c语言中 strncmp函数, 函数原型和头文件。
    c语言中strcmp函数,函数原型和函数头文件
    c语言中strncat函数,函数原型以头文件
    c语言中strcat函数,函数原型和函数头文件
  • 原文地址:https://www.cnblogs.com/jojoword/p/11331824.html
Copyright © 2020-2023  润新知