MYSQL 表的操作<三>

2018-09-12  本文已影响16人  那是一阵清风_徐来

数据类型

1.数值类型
2.日期和时间类型
类型                 格式                   用途
date            YYYY-MM-DD                日期值
time            HH:MM:SS                时间值或持续时间
datetime        YYYY-MM-DD HH:MM:SS     混合日期和时间值,默认值null
timestamp       YYYY-MM-DD HH:MM:SS     混合日期和时间值,时间戳,默认使用当前时间
year            YYYY                       年份值
3.字符串类型
char                         固定长度
varchar                      可变长度
text                         长文本数据
...

笔记:
1、一个汉字占多少长度与编码有关:
      UTF-8:一个汉字=3个字节
      GBK:一个汉字=2个字节
2、varchar(n) 表示 n 个字符,无论汉字和英文,Mysql 都能存入 n 个字符,仅是    实际字节长度有所区别
3、MySQL 检查长度,可用 SQL 语言来查看:
      select LENGTH(fieldname) from tablename

创建表

通用语法:

CREATE TABLE table_name (column_name column_type);

示例:

创建表:
1、分析实体: 学生
2、学生Id
3、姓名
4、性别
5、年龄

create table Student(
sid int primary key,
sname varchar(31),
sex int,
age int
);

规范写的话一般如下:
CREATE TABLE `student` (
  `sid` int(11) NOT NULL,
  `sname` varchar(31) DEFAULT NULL,
  `sex` int(11) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8

备注:
创建 MySql 的表时,表名和字段名外面的符号 ` 不是单引号,而是英文输入法状态下的反单引号,也就是键盘左上角 esc 按键下面的那一个 ~ 按键,坑惨了。
反引号是为了区分 MySql 关键字与普通字符而引入的符号,一般的,表名与字段名都使用反引号。

注意:
1、MySQL命令终止符为分号 (;) 。
2、AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1。
3、PRIMARY KEY关键字用于定义列为主键。 您可以使用多列来定义主键,列间以逗号分隔。

比如iOS创建表:
create table if not exists table_apiVersion(ID integer primary key autoincrement, apiVersion text,oldApiVersion text)

创建表打印如下:

create table Student(
-> sid int primary key,
-> sname varchar(31),
-> sex int,
-> age int
-> );

Query OK, 0 rows affected (0.01 sec)

查看表

--  查看所有的表
    show tables;

--  查看表的创建过程
    show create table student;

--  查看表结构
    desc student;

查看表打印如下:

mysql> show tables;
+-----------------+
| Tables_in_day06 |
+-----------------+
| Student |
+-----------------+
1 row in set (0.00 sec)

查看表的创建过程打印如下:

+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE student (
sid int(11) NOT NULL,
sname varchar(31) DEFAULT NULL,
sex int(11) DEFAULT NULL,
age int(11) DEFAULT NULL,
PRIMARY KEY (sid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

查看表结构打印如下:

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid | int(11) | NO | PRI | NULL | |
| sname | varchar(31) | YES | | NULL | |
| sex | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

修改表

--  添加列(add)
    alter table 表名 add 列名 列的类型 列的约束
    alter table student add weight double not null;

--  修改列(modify)
    alter table student modify weight float not null;

--  修改列名(change)
    alter table student change sex gender varchar(2);

--  删除列(drop)
    alter table student  drop weight;

--  修改表名(rename)
    rename table student to person

--  修改字符集
    alter table person character set gbk;

添加列 打印如下:

alter table student add weight double not null;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sid | int(11) | NO | PRI | NULL | |
| sname | varchar(31) | YES | | NULL | |
| sex | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| weight | double | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

修改列 打印如下:

mysql> alter table student modify weight float not null;
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sid | int(11) | NO | PRI | NULL | |
| sname | varchar(31) | YES | | NULL | |
| sex | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| weight | float | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

修改类名打印如下:

mysql> alter table student change sex gender varchar(2);
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sid | int(11) | NO | PRI | NULL | |
| sname | varchar(31) | YES | | NULL | |
| gender | varchar(2) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| weight | float | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

删除列打印如下;

mysql> alter table student drop weight;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sid | int(11) | NO | PRI | NULL | |
| sname | varchar(31) | YES | | NULL | |
| gender | varchar(2) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

修改表名打印如下:

mysql> rename table student to person;
Query OK, 0 rows affected (0.11 sec)

mysql> show tables;
+-----------------+
| Tables_in_day06 |
+-----------------+
| person |
+-----------------+
1 row in set (0.00 sec)

mysql> show create table person;
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| person | CREATE TABLE person (
sid int(11) NOT NULL,
sname varchar(31) DEFAULT NULL,
gender varchar(2) DEFAULT NULL,
age int(11) DEFAULT NULL,
PRIMARY KEY (sid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

修改表的字符集打印如下:

mysql> alter table person character set gbk;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> show create table person;
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| person | CREATE TABLE person (
sid int(11) NOT NULL,
sname varchar(31) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
sex int(11) DEFAULT NULL,
age int(11) DEFAULT NULL,
PRIMARY KEY (sid)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

删除表

drop table person;

mysql> drop table person;
Query OK, 0 rows affected (0.09 sec)

mysql> show tables;
Empty set (0.00 sec)

上一篇 下一篇

猜你喜欢

热点阅读