MySQL 库、表

1、库

    1、库的基本操作
      1、查看已有的库
        show databases;
      2、创建库(指定默认字符集)
        create database 库名 default charset=utf8;
      3、查看创建的库(字符集)(非内建)
        show create database 库名;
      4、查看当前所在库
        select database();
      5、切换库
        use 库名;
      6、查看库中已有表
        show tables;
      7、删除库
        drop database 库名;
    2、库名的命名规则
      1、可以使用数字、字母、_,但不能使用纯数字
      2、库名区分字母大小写
      3、库名具有唯一性
      4、不能使用特殊字符和MySQL关键字

 1 1、创建库AID1803db,指定字符集为utf8
 2         create database AID1803db default charset=utf8;
 3     2、切换到该库AID1803db
 4         use AID1803db;
 5     3、查看当前所在库
 6         select database();
 7     4、查看库中已有的表
 8         show tables;
 9     5、查看AID1803db的字符集(查看创建库的语句)
10         show create database AID1803db;
11     6、删除库AID1803db
12         drop database AID1803db;
View Code

3、表

1、表的基本操作  表存放在库里面
 1、创建表的字段类型  (指定字符集)
   create table 表名(
   字段名 数据类型,
   字段名 数据类型,
   ... ...
   );
 2、查看创建的表(字符集)(非内建)
   show create table 表名;

1 +-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
2 | Table | Create Table                                                                                                                                         |
3 +-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
4 | info  | CREATE TABLE `info` (
5   `id` int(11) DEFAULT NULL,
6   `name` char(10) DEFAULT NULL
7 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
8 +-------+------------------------------------------------------------------------------------------------------------------------------------------------------+
View Code

 3、查看表结构(有哪些字段、记录的类型、NULL、key、Default、Extra)
   desc 表名;

1 mysql> desc info;
2 +-------+----------+------+-----+---------+-------+
3 | Field | Type     | Null | Key | Default | Extra |
4 +-------+----------+------+-----+---------+-------+
5 | id    | int(11)  | YES  |     | NULL    |       |
6 | name  | char(10) | YES  |     | NULL    |       |
7 +-------+----------+------+-----+---------+-------+
View Code

 4、删除表
   drop table 表名;
2、表的命名规则(同库的命名规则)

 1 1、创建库 python
 2     create database python;
 3 2、在库 python 中创建表 py_mysql,指定字符集utf8
 4      表中字段有 id int 和 name char(20) 两个字段
 5     use python;
 6     create table py_mysql(
 7     id int,
 8     name char(20)
 9     )default charset=utf8;
10 3、查看表 py_mysql 的字符集以及存储引擎
11     show create table py_mysql;
12 4、查看 py_mysql 的表结构
13     desc py_mysql;
14 5、删除表 py_mysql
15     drop table py_mysql;
16 
17 练习
View Code

2、表记录管理

  1、在表中插入记录
    1、insert into 表名 values(值1),(值2),...,(值N);
    2、insert into 表名(字段名列表) values(值1),...(值N);
  2、查询表记录
    1、select * from 表名;
    2、select 字段1,字段名2,...,字段名N from 表名;

 1 1、查看所有的库
 2         show databases;
 3       2、创建新库 studb
 4         create database studb;
 5       3、在 studb 中创建表 tab1 ,指定字符集utf8,字段有 id 、name、age
 6         use studb;
 7         create table tab1(
 8     id int,
 9     name char(10),
10     age int
11     )character set utf8;
12       4、查看tab1的表结构
13         desc tab1;
14       5、在tab1中随便插入2条记录
15         insert into tab1 values
16     (1,"张三丰",100),(2,"张无忌",30);
17       6、在tab1中的name、age两个字段插入2条记录
18         insert into tab1(name,age) values
19     ("金毛狮王",88),("紫衫龙王",87);
20       7、查看tab1中所有记录
21         select * from tab1;
22       8、查看tab1表中所有人的姓名和年龄
23         select name,age from tab1;
24       9、查看tab1表中年龄大于20的信息
25         select * from tab1 where age>20;
View Code

3、字段增、删、改、修

  1、语法:alter table 表名 执行动作;
    1、添加字段
      alter table 表名 add 字段名 数据类型 first | after 字段名;
    2、删除字段
      alter table 表名 drop 字段名;
    3、修改字段数据类型
      alter table 表名 modify 字段名 新数据类型;
      # 修改数据类型时会受到表中原有数据的限制
    4、修改字段名
      alter table 表名 change 旧名 新名 数据类型;
    5、修改表名
      alter table 表名 rename 新表名;

 1     1、创建库 studb2 :create datacase studb2;
 2     2、在库中创建表 t1 ,字段有3个:name、age、phnumber
 3       use studb2;
 4       create table t1(
 5       name char(20),
 6       age tinyint unsigned,
 7       phnumber char(11)
 8       );
 9     3、查看表结构
10       desc t1;
11     4、在表中第一列添加一个 id 字段
12       alter table t1 add id int first;
13     5、把 phnumber 的数据类型改为 bigint
14       alter table t1 modify phnumber bigint;
15     6、在表中最后一列添加一个字段 address
16       alter table t1 add address varchar(50);
17     7、删除表中的 age 字段
18       alter table t1 drop age;
19     8、查看表结构
20       desc t1;
View Code

5、记录的增加、修改

  1、删除表记录
    1、delete from 表名 where 条件;
    2、注意
      delete语句后如果不加where条件,所有记录全部清空
  2、更新表记录
    1、update 表名 set 字段1=值1,字段2=值2,... where 条件;
    2、注意
      必须加where条件

 1     1、查找所有蜀国人的信息
 2       select * from hero where country="蜀国";
 3     2、查找所有女英雄的姓名、性别和国家
 4       select name,sex,country from hero 
 5       where sex="女";
 6     3、把id为2的记录改为典韦,性别男,国家魏国
 7       update hero set name="典韦",sex="男",country="魏国" where id=2;
 8     4、删除所有蜀国英雄
 9       delete from hero where country="蜀国";
10     5、把貂蝉的国籍改为魏国
11       update hero set country="魏国" 
12       where name="貂蝉";
13     6、删除所有表记录
14       delete from hero;
View Code
原文地址:https://www.cnblogs.com/LXP-Never/p/9404096.html