MySql数据库操作_操作表DDL_mysql入门_002
发布时间:2022-10-10 13:04:39 所属栏目:MySql教程 来源:
导读: 1 创建表
创建表的格式
CREATE TABLE 表名 (
字段名 1 字段类型 1,
字段名 2 字段类型 2
);
最后一列不可以加逗号
创建表的关键字 CREATE 创建 TABLE
创建表的格式
CREATE TABLE 表名 (
字段名 1 字段类型 1,
字段名 2 字段类型 2
);
最后一列不可以加逗号
创建表的关键字 CREATE 创建 TABLE
|
1 创建表 创建表的格式 CREATE TABLE 表名 ( 字段名 1 字段类型 1, 字段名 2 字段类型 2 ); 最后一列不可以加逗号 创建表的关键字 CREATE 创建 TABLE 表 sql中常用的数据类型:常见的 int 整数--- age int double 浮点数 --- score double(5,2) ---一共有五位数,小数点后面保留两位 date 日期类型, 只包含年月日,yyyy-MM-dd datetime 日期--包含年月日时分秒 -- yyyy-MM-dd HH:mm:ss timesTAMp :时间戳类型,包含年月日时分秒--不给就使用当前系统时间. varchar 字符串 --- name varchar(20) --- 20为最大的长度,20个字符(最大20个字符) 创建学生信息表,包含下面字段:编号;姓名;年龄;分数;出生日期;添加时间. create table student( id int, name varchar(20), age int, score double(4,1), birthday date, insert_time timesTAMp ); mysql> use db3 Database changed mysql> create table student( -> id int, -> name varchar(20)mysql数据表, -> age int, -> score double(4,1), -> birthday date, -> insert_time timesTAMp -> ); Query OK, 0 rows affected (0.20 sec) 查看表 查看某个数据库中所有的表 show tables; mysql> show tables; +---------------+ | Tables_in_db3 | +---------------+ | student | +---------------+ 1 row in set (0.00 sec) mysql数据权限 表设计_mysql copy 表数据_mysql数据表 mysql> -- 查看表的结构 mysql> desc student; +-------------+-------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+-------------------+-----------------------------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | | score | double(4,1) | YES | | NULL | | | birthday | date | YES | | NULL | | | insert_time | timesTAMp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------------+-------------+------+-----+-------------------+-----------------------------+ 6 rows in set (0.01 sec) mysql> -- 查看创建表的sql语句 mysql> show create table student; +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | student | CREATE TABLE `student` ( `id` int(11) DEFAULT NULL, `name` varchar(20) DEFAULT NULL, `age` int(11) DEFAULT NULL, `score` double(4,1) DEFAULT NULL, `birthday` date DEFAULT NULL, `insert_time` timesTAMp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> -- 上面'存在的目的是为了避免c关键字冲突 mysql> -- 快速创建一个结构相同的表 mysql> -- 创建stu表,stu表结构和student表结构相同 mysql> create table stu like student; Query OK, 0 rows affected (0.40 sec) mysql> desc stu; +-------------+-------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+-------------------+-----------------------------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | | score | double(4,1) | YES | | NULL | | | birthday | date | YES | | NULL | | | insert_time | timesTAMp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------------+-------------+------+-----+-------------------+-----------------------------+ 6 rows in set (0.01 sec) mysql> show tables; +---------------+ | Tables_in_db3 | +---------------+ | stu | | student | +---------------+ 2 rows in set (0.00 sec) mysql> -- 删除表; mysql> drop table stu; Query OK, 0 rows affected (0.12 sec) mysql> create table stu like student; Query OK, 0 rows affected (0.16 sec) mysql> drop table if exists stu; Query OK, 0 rows affected (0.10 sec) mysql> create table stu like student; Query OK, 0 rows affected (0.16 sec) mysql> -- 修改表的g结构 mysql> -- 添加表列 add mysql> alter table student add remark varchar(20); Query OK, 0 rows affected (0.54 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> -- 修改列类型modify 将学生表student 的remark字段x的类型改成varchar(100) mysql> alter table student modify remark varchar(100); Query OK, 0 rows affected (0.67 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> -- 修改列名change mysql> -- 将student表中的remark字段名改成intro ,类型varchar(30) mysql> alter table student change remark intro varchar(30); Query OK, 0 rows affected (0.48 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> -- 删除列 drop - alter table 表名 drop 列名 mysql> -- 删除student表中的字段 intro mysql> alter table student drop intro; Query OK, 0 rows affected (0.47 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> -- 修改表名--rename table 表名 to 新表名; mysql> -- 将学生表student改名为student2; mysql> rename table student to student2; Query OK, 0 rows affected (0.37 sec) mysql> -- 修改字符集 character set mysql> -- alter table 表名 character set 字符集; mysql> -- 将student2的表的编码改成gbk; mysql> alter table student2 character set gbk; Query OK, 0 rows affected (0.23 sec) Records: 0 Duplicates: 0 Warnings: 0 (编辑:天瑞地安资讯网_保定站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐

