时间:2021-05-23
创建表
create table 表名
create table if not exists 表名
mysql> create database company;Query OK, 1 row affected (0.00 sec)mysql> use company;Database changedmysql> create table if not exists t_dept( -> deptno int, -> dname varchar(20), -> loc varchar(40));Query OK, 0 rows affected (0.20 sec)mysql> show tables;+-------------------+| Tables_in_company |+-------------------+| t_dept |+-------------------+1 row in set (0.00 sec)mysql>显示当前库下的所有表
show tables;
mysql> show tables;+-------------------+| Tables_in_company |+-------------------+| t_dept |+-------------------+1 row in set (0.00 sec)查看表的结构
describe 表名
简写
desc 表名
mysql> describe t_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+3 rows in set (0.00 sec)mysql> desc t_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+3 rows in set (0.00 sec)查看表的详细
show create table 表名
mysql> show create table t_dept;+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+| t_dept | CREATE TABLE `t_dept` ( `deptno` int(11) DEFAULT NULL, `dname` varchar(20) DEFAULT NULL, `loc` varchar(40) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 |+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)show create table t_dept \Gmysql> show create table t_dept \G*************************** 1. row *************************** Table: t_deptCreate Table: CREATE TABLE `t_dept` ( `deptno` int(11) DEFAULT NULL, `dname` varchar(20) DEFAULT NULL, `loc` varchar(40) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf81 row in set (0.00 sec)删除表
drop table 表名
drop table if exists 表名
修改表名
ALTER TABLE old_table_name RENAME [TO] new_table_name
old_table_name 原表名
new_table_name 新表名
将t_dept修改为tab_dept
为表增加一个字段默认在最后
ALTER TABLE table_name ADD 属性名 属性类型
为tab_dept增加一个字段descri varchar(20)
mysql> desc tab_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+3 rows in set (0.00 sec)mysql> alter table tab_dept add descri varchar(20);Query OK, 0 rows affected (0.33 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc tab_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | || descri | varchar(20) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)在表的第一个位置增加一个字段
ALTER TABLE table_name ADD 属性名 属性类型 first
mysql> alter table tab_dept add id int first;Query OK, 0 rows affected (0.38 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc tab_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | || descri | varchar(20) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+5 rows in set (0.00 sec)在表的指定字段之后增加字段
ALTER TABLE table_name ADD 属性名 属性类型 AFTER 属性名
mysql> alter table tab_dept add comm varchar(20) after dname;Query OK, 0 rows affected (0.31 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc tab_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || comm | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | || descri | varchar(20) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+6 rows in set (0.00 sec)删除字段
ALTER TABLE table_name DROP 属性名
mysql> alter table tab_dept drop comm;Query OK, 0 rows affected (0.32 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc tab_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | || descri | varchar(20) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+5 rows in set (0.00 sec)字段修改-修改字段数据类型
ALTER TABLE table_name MODIFY 属性名 数据类型
字段修改-修改字段名称
ALTER TABLE table_name CHANGE 旧属性名 新属性名 旧数据类型
mysql> alter table tab_dept change id deptid int;Query OK, 0 rows affected (0.07 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc tab_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| deptid | int(11) | YES | | NULL | || deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | || descri | int(11) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+5 rows in set (0.00 sec)字段修改-同时修改字段名称与数据类型
ALTER TABLE table_name CHANGE 旧属性名 新属性名 新数据类型
mysql> alter table tab_dept change deptid id varchar(32);Query OK, 0 rows affected (0.49 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc tab_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| id | varchar(32) | YES | | NULL | || deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | || descri | int(11) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+5 rows in set (0.00 sec)修改顺序
ALTER TABLE table_name MODIFY 属性名1 数据类型 FIRST|AFTER 属性名2
2个属性必须存在
将deptno调到第一个位置
将ID放在最后
mysql> alter table tab_dept modify deptno int after descri;Query OK, 0 rows affected (0.29 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc tab_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| id | varchar(32) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | || descri | int(11) | YES | | NULL | || deptno | int(11) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+5 rows in set (0.00 sec)mysql> alter table tab_dept modify deptno int first;Query OK, 0 rows affected (0.34 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table tab_dept modify id int after descri;Query OK, 0 rows affected (0.47 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc tab_dept;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| deptno | int(11) | YES | | NULL | || dname | varchar(20) | YES | | NULL | || loc | varchar(40) | YES | | NULL | || descri | int(11) | YES | | NULL | || id | int(11) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+5 rows in set (0.00 sec)声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
参考表MySQL笔记之函数查询的使用count()用来统计记录的条数复制代码代码如下:mysql>selectcount(*)fromscore;mysql>s
本文主要向大家介绍了MySQL数据库之MySQL的binary类型操作,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。示例数据表:?123C
本文实例讲述了MySQL学习笔记之数据定义表约束,分页方法。分享给大家供大家参考,具体如下:1.primarykey主键特点:主键是用于唯一标识一条记录的约束,
表复制mysql拷贝表操作我们会常常用到,下面就为您详细介绍几种mysql拷贝表的方式,希望对您学习mysql拷贝表方面能够有所帮助。假如我们有以下这样一个表:
本文实例讲述了JavaScript学习笔记之数组基本操作。分享给大家供大家参考,具体如下:一、数组定义1、定义vara=[1,2,3]vara=newArray