以前我们也写过一点关于mysql 的操作,但都是几个简单的语句,今天开始,来系统的学习一下mysql。
因为以前也简单介绍过,所以关于细节之类的都不在详述,这里直接开始写代码。
库的基本操作
(1)创建新的数据库:create database test10(数据库的名字) character set utf8(设置编码格式);
(2)删除一个数据库:drop database test10;
(3)查看所有数据库:show databases;
(4)使用数据库:use table;
(5)查看当前数据库:select database();
(6)显示当前时间、用户名、数据库版本:select now(), user(), version();
(7)查看当前库信息:show create database test10
(8)修改库选项信息: (这个暂时没有运行出来)
表的基本操作
(1)创建表
create table[ if not exists] 表名 ( 表的结构定义 )[ 表选项]
每个字段必须有数据类型
最后一个字段后不能有逗号
对于字段的定义:
字段名 数据类型 [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] [COMMENT 'string']
比如:
CREATE TABLE `score` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`change_type` VARCHAR(255) NULL DEFAULT NULL,
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`score` INT(11) NULL DEFAULT NULL,
`user_id` BIGINT(20) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci';
(2)表的选项:
- 删除列:alter table test10(表的名字) drop core(列的名字);
- 增加列:alter table test11 add core2 int not null comment '注释’;
- 给列重命名:alter table socre change user_id1(要修改的列) user_id2(新的名字) bigint not null comment ‘注释’;
- 修改列的类型信息:这个没有实现
- 修改表的名字:alter table gag rename to gag1; rename table socre2 to score;
(3)查看所有表:show tables;
(4)查看表结构:show create table score2 (更详细)
(5)删除表:drop table socre