Mysql数据表的操作(一)
来源:互联网 发布:淘宝 企业店铺 编辑:程序博客网 时间:2024/04/30 02:57
一、数据库及数据表的创建与删除
//创建数据库test2
mysql> create database test2;
Query OK, 1 row affected
//遍历显示所有的数据库
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| book |
| mysql |
| performance_schema |
| sys |
| test |
| test2 |
+——————–+
7 rows in set
//删除test2数据库
mysql> drop database test2;
Query OK, 0 rows affected
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| book |
| mysql |
| performance_schema |
| sys |
| test |
+——————–+
6 rows in set
//使用test数据库
mysql> use test;
Database changed
//在数据库下创建数据表
mysql> create table tb_emp2
(
id int(11),
name varchar(25),
deptid int(11),
salary float
);
Query OK, 0 rows affected
//遍历显示这个数据库下所有的数据表
mysql> show tables;
+—————-+
| Tables_in_test |
+—————-+
| tb_emp1 |
| tb_emp2 |
+—————-+
2 rows in set
二、对表的操作
1.查看表结构
mysql> desc tb_emp1;
+——–+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——–+————-+——+—–+———+——-+
| id | int(11) | YES | | NULL | |
| name | varchar(25) | YES | | NULL | |
| deptid | int(11) | YES | | NULL | |
| salary | float | YES | | NULL | |
+——–+————-+——+—–+———+——-+
4 rows in set
2.查看表详细结构
show create table tb_emp1或者show create table tb_emp1\G后者显示结果便于直观
mysql> show create table tb_emp1;
+———+——————————————————————————————————————————————————————————————-+
| Table | Create Table |
+———+——————————————————————————————————————————————————————————————-+
| tb_emp1 | CREATE TABLE tb_emp1
(
id
int(11) DEFAULT NULL,
name
varchar(25) DEFAULT NULL,
deptid
int(11) DEFAULT NULL,
salary
float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+———+——————————————————————————————————————————————————————————————-+
1 row in set
3.修改数据表名
mysql> alter table tb_emp1 rename tb_emp3;
Query OK, 0 rows affected
mysql> show tables;
+—————-+
| Tables_in_test |
+—————-+
| tb_emp2 |
| tb_emp3 |
+—————-+
2 rows in set
4.修改字段数据类型
mysql> desc tb_emp3;
+——–+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——–+————-+——+—–+———+——-+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| deptid | int(11) | YES | | NULL | |
| salary | float | YES | | NULL | |
+——–+————-+——+—–+———+——-+
4 rows in set
5.修改字段名
mysql> alter table tb_emp3 change deptid loc varchar(50);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
6.添加字段
mysql> alter table tb_emp3 add managerID int(10);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
给表的第一列添加一个字段
mysql> alter table tb_emp3 add column2 int(11) first;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
给表的指定行添加字段
mysql> alter table tb_emp3 add column3 int (20) after name;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
删除字段
mysql> alter table tb_emp3 drop name;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
修改字段在表中的排列位置
mysql> alter table tb_emp3 modify column3 int(20) first;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
修改字段到指定列之后
mysql> alter table tb_emp3 modify column3 int(20) after salary;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
7.删除数据表
删除没有被关联的表
mysql> drop table if exists tb_emp3;
Query OK, 0 rows affected
- Mysql数据表的操作(一)
- MySQL数据表的操作
- mysql的数据表操作
- MySQL数据表的操作
- 数据表操作(一)
- MySQL(二)--数据表的基本操作
- 数据表的基本操作(一)
- mysql 数据表的基本操作
- MySQL:数据表的基本操作
- mysql:数据表的基本操作
- MySQL数据表的常用操作
- MySQL 数据表的基本操作
- Mysql修改数据表的操作
- mysql 数据表的基本操作
- MySQL操作数据表的记录
- MySQL学习7:操作数据表(一)
- MYSQL基本操作(2)-数据表的操作
- mysql(五)操作数据表
- 笔记本联想(Lenovo)G40-70M加装内存和SSD固态硬盘
- 如何扩展一个类
- 关系数据库中关系表间的连接(左连接,右连接,内连接,全连接,外连接)
- hud 6069 Counting Divisors(数学题 约数定理)
- JDBC及简单封装
- Mysql数据表的操作(一)
- C++模板(二)
- 51nod1072 威佐夫游戏
- 说说你对servlet 的理解或者 servlet 是什么?
- 初见openssl
- 数据库编程题
- 使用call方法自定义接受者来调用方法
- Camera---基础属性及注意点
- 【程序笔记】第一期-深究bool类型