mysql基础(2)_sql基本操作之表操作(创建、查看、修改、删除数据表)

创建数据表
基本语法:
create table 表名(
字段名 字段类型 [字段属性],
字段名 字段类型 [字段属性],
...
字段名 字段类型 [字段属性]
)[表选项];

-- 创建一个表
mysql> create table class1(
    -> name varchar(10),
    -> age int,
    -> addr varchar(40)
    -> );
Query OK, 0 rows affected (1.87 sec)

name varchar(10)  --(不能超过10个字符,数值必须写否则报错)

说明:
   一、表必须放到对应的数据库下,有两种方式可以将表挂入到指定的数据库下;
         1、在数据表名字前面加上数据库名字,用"."连接即可:数据库.数据表

-- 将数据表挂到某数据库下
mysql> create table mydatabase2.class(
    -> name varchar(10),
    -> age int,
    -> addr varchar(40)
    -> );
Query OK, 0 rows affected (1.85 sec)

        2、在创建数据表之前先进入到某个具体的数据库即可:use 数据库名字

-- 进入数据库创建表
mysql> use mydatabase2;
Database changed
mysql> create table teacher(
    -> name varchar(10)
    -> );
Query OK, 0 rows affected (1.76 sec)

   二、表选项:与数据库选项类似
         Engine:存储引擎,mysql提供的具体存储数据的方式,默认有一个innodb(5.5以前默认是myisam)
         Charset:字符集,只对当前自己表有效(级别比数据库高)
         Collate:校对集,只对当前自己表有效

-- 使用表选项
mysql> create table student(
    -> name varchar(10)
    -> )charset utf8;
Query OK, 0 rows affected (1.78 sec)

创建表的另一种方式:复制已有表结构
即从已经存在的表复制一份(只复制结构,如果表中有数据不复制)
基本语法:
create table 新表名 like 表名; 
说明:只要使用数据库.表名,就可以在任何在数据库下访问其他数据库的表名

-- 复制表,查看表
mysql> use test;
Database changed
mysql> create table teacher like mydatabase2.teacher;
Query OK, 0 rows affected (1.75 sec)
mysql> desc teacher;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.02 sec)

显示数据表
    1、显示所有表
    基本语法:show tables;

    2、匹配显示表
    基本语法:show tables like '匹配模式';

-- 显示表、显示匹配表
mysql> show tables;
+-----------------------+
| Tables_in_mydatabase2 |
+-----------------------+
| class                 |
| student               |
| teacher               |
+-----------------------+
3 rows in set (0.00 sec)

mysql> show tables like 'c%';
+----------------------------+
| Tables_in_mydatabase2 (c%) |
+----------------------------+
| class                      |
+----------------------------+
1 row in set (0.00 sec)

显示表结构
本质含义:显示表中所包含的字段信息(名字、类型、属性等)
基本语法:Describe 表名;
               Desc 表名(常用);
               show columns from 表名;

    其中表结构中内容含义:
    Field 字段名字
    Type 字段类型
    Null 值是否允许为空
    Defoult 默认值
    Extra 额外的属性

显示表详细结构
基本语法:show create table 表名;

mysql中有多种语句结束符:
;与\g所表示的效果是一样的
\G字段显示效果相对美观

-- 显示表结构
mysql> show create table class\G;
*************************** 1. row ***************************
       Table: class
Create Table: CREATE TABLE `class` (
  `name` varchar(10) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `addr` varchar(40) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

修改数据表
mysql中通过alter table语句来修改表:

1、修改表选项
表选项包含engine、charset和collate
基本语法:alter table 表名 表选项 [=] 值;

-- 修改表属性
mysql> alter table class charset gbk;
Query OK, 0 rows affected (1.89 sec)

注意:如果数据库已经确定了,里面有很多数据了,不要轻易修改表选项(字符集影响不大)

2、修改表结构

修改表名:rename table 旧表名 to 新表名;或 alter table 旧表名 rename [to] 新表名;
修改表选项:alter table 表名 表选项 [=] 新值;
新增字段:alter table 表名 add [column] 新字段名 字段类型 [字段属性] [新位置:first/after 字段名];
修改字段名:alter table 表名 change 旧字段名 新字段名 字段类型 [字段属性] [新位置]
修改字段类型(属性):alter table 表名 modify 字段名 新类型 [新属性] [新位置]
删除字段:alter table 表名 drop 字段名;

-- 修改数据表
mysql> desc class;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(10) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| sex   | varchar(10) | YES  |     | NULL    |       |
| addr  | varchar(40) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.03 sec)

mysql> alter table class drop sex;
Query OK, 0 rows affected (1.77 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> dexc class;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dexc class' at line 1
mysql> desc class;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(10) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| addr  | varchar(40) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)

mysql> rename table class to class1;
Query OK, 0 rows affected (0.00 sec)

mysql> alter table class1 rename class;
Query OK, 0 rows affected (0.00 sec)

mysql> alter table class add column sex varchar(10) after age;
Query OK, 0 rows affected (0.22 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table class change addr address varchar(50) after sex;
Query OK, 0 rows affected (1.80 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table class modify sex int;
Query OK, 0 rows affected (1.80 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc class;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(10) | YES  |     | NULL    |       |
| age     | int(11)     | YES  |     | NULL    |       |
| sex     | int(11)     | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

mysql> alter table class drop sex;
Query OK, 0 rows affected (1.80 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc class;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(10) | YES  |     | NULL    |       |
| age     | int(11)     | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

备注:字段类型主要有三类:数值、日期/时间和字符串(字符)类型。
          字段属性主要有:null/not null,default,primary key, unique key, auto_increment, comment等
          新位置:字段想要存放的位置
                       first:在某某之前(最前面),第一个字段
                       after 字段名:在某某字段名之后

删除数据表
基础语法:drop table 表名[,表名2...];


 


版权声明:本文为no15870678621原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。