• 操作MySQL数据库


                                                          操作MySQL数据库

    向表中插入数据

    insert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下:

    insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

    其中 [] 内的内容是可选的, 例如, 要给 samp_db 数据库中的 students 表插入一条记录, 执行语句:

    insert into students values(NULL, "王刚", "男", 20, "13811371377");

    按回车键确认后若提示 Query Ok, 1 row affected (0.05 sec) 表示数据插入成功。 若插入失败请检查是否已选择需要操作的数据库。


    有时我们只需要插入部分数据, 或者不按照列的顺序进行插入, 可以使用这样的形式进行插入:

    insert into students (name, sex, age) values("孙丽华", "女", 21);

    查询表中的数据

    select 语句常用来根据一定的查询规则到数据库中获取数据, 其基本的用法为:

    select 列名称 from 表名称 [查询条件];

    例如要查询 students 表中所有学生的名字和年龄, 输入语句 select name, age from students; 执行结果如下:

    	mysql> select name, age from students;
    	+--------+-----+
    	| name   | age |
    	+--------+-----+
    	| 王刚   |  20 |
    	| 孙丽华 |  21 |
    	| 王永恒 |  23 |
    	| 郑俊杰 |  19 |
    	| 陈芳   |  22 |
    	| 张伟朋 |  21 |
    	+--------+-----+
    	6 rows in set (0.00 sec)
    
    	mysql>

    也可以使用通配符 * 查询表中所有的内容, 语句: select * from students;

    按特定条件查询:

    where 关键词用于指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;

    以查询所有性别为女的信息为例, 输入查询语句: select * from students where sex="女";

    where 子句不仅仅支持 "where 列名 = 值" 这种名等于值的查询形式, 对一般的比较运算的运算符都是支持的, 例如 =、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等。 还可以对查询条件使用 or 和 and 进行组合查询, 以后还会学到更加高级的条件查询方式, 这里不再多做介绍。

    示例:

    查询年龄在21岁以上的所有人信息: 

                           select * from students where age > 21;

    查询名字中带有 "王" 字的所有人信息: 

                         select * from students where name like "%王%";

    查询id小于5且年龄大于20的所有人信息: 

                        select * from students where id<5 and age>20;

    更新表中的数据

    update 语句可用来修改表中的数据, 基本的使用形式为:

    update 表名称 set 列名称=新值 where 更新条件;

    使用示例:

    将id为5的手机号改为默认的"-": 

                 update students set tel=default where id=5;

    将所有人的年龄增加1:

                 update students set age=age+1;

    将手机号为 13288097888 的姓名改为 "张伟鹏", 年龄改为 19: 

               update students set name="张伟鹏", age=19 where tel="13288097888";

    删除表中的数据

    delete 语句用于删除表中的数据, 基本用法为:

    delete from 表名称 where 删除条件;

    使用示例:

    删除id为2的行: 

                      delete from students where id=2;

    删除所有年龄小于21岁的数据: 

                      delete from students where age<20;

    删除表中的所有数据: 

                    delete from students;

     

    创建后表的修改

    alter table 语句用于创建后对表的修改, 基础用法如下:

    添加列

    基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];

    示例:

    在表的最后追加列 address: 

                        alter table students add address char(60);

    在名为 age 的列后插入列 birthday: 

                       alter table students add birthday date after age;

    修改列

    基本形式: alter table 表名 change 列名称 列新名称 新数据类型;

    示例:

    将表 tel 列改名为 telphone: 

                               alter table students change tel telphone char(13) default "-";

    将 name 列的数据类型改为 char(16): 

                              alter table students change name name char(16) not null;

    删除列

    基本形式: alter table 表名 drop 列名称;

    示例:

    删除 birthday 列: 

                            alter table students drop birthday;

    重命名表

    基本形式: alter table 表名 rename 新表名;

    示例:

    重命名 students 表为 

                          workmates: alter table students rename workmates;

    删除整张表

    基本形式: drop table 表名;

    示例: 删除 workmates 表: 

                             drop table workmates;

    删除整个数据库

    基本形式: drop database 数据库名;

    示例: 删除 samp_db 数据库: 

                           drop database samp_db;


  • 相关阅读:
    零基础学python-2.24 一些常用函数
    零基础学python-2.23 模块
    零基础学python-2.22 回到我们的游戏 加入文件和异常
    tcp协议:三次握手四次挥手详解---总结
    centos7安装jmeter + ant
    centos7安装jenkins
    centos7安装tomcat
    centos7安装jdk (jdk-8u161-linux-x64.tar.gz 和 java-1.8.0-openjdk* 介绍)
    波浪场景jp@gc
    阶梯场景jp@gc
  • 原文地址:https://www.cnblogs.com/guanhuohuo/p/12533806.html
Copyright © 2020-2023  润新知