MySQL (a) operating a database table, and

First, download and install

https://blog.csdn.net/h18733517027/article/details/81111894

Second, the connection database (remember play; carriage return linefeed otherwise, may also be used, such as graphical interface software: Navicat, SQLyog, phpmyadmin, Workbench )

#连接mysql
mysql -h 地址 -P 端口 -u 用户名 -p 密码
#例如: 
mysql -h 127.0.0.1 -P 3306 -u root -p ****

#退出mysql
exit;

Third, database operations

1. Create a database

#创建数据库
create database 数据库名 [数据库选项];
#例如
create database runoob default charset utf8 collate utf8_bin;

# Database options: character set and collation

# Character Set: general acquiescence utf8;

# Collation common: the end of the ⑴ci: case-insensitive ⑵cs the end: the end ⑶bin case-sensitive: the binary coding Compare

#修改数据库选项信息
alter database 数据库名 [新的数据库选项];
#例如: 
alter database test default charset gbk;

2, select the database

#选择数据库
use 数据库名;
#例如
use runoob;

3, delete the database

#删除数据库
drop database 数据库名;
#例如
drop database runoob;

4, check the database status

#show 查看当前有哪些数据库(查)
show databases;
#查看数据库的创建语句
show create database 数据库名;

Fourth, the operating table

1. Create

#关键字:create 创建数据表(增)

create table 表名(
字段1  字段1类型 [字段选项],
字段2  字段2类型 [字段选项],
字段n  字段n类型 [字段选项]
)表选项信息;


#例如:

create table test(
  id int(10) unsigned not null auto_increment comment 'id',
  content varchar(100) not null default '' comment '内容',
  time int(10) not null default 0 comment '时间',
  primary key (id)
)engine=InnoDB default charset=utf8 comment='测试表';

# Syntax analysis (parsing hereinafter MySQL column properties alone):

If you do not want field is NULL attribute field may be set to NOT NUL, when the database operation, if the input data field is NULL, the error is reported.

It is defined as the increment AUTO_INCREMENT attribute, generally used for the primary key, the value added by 1 automatically.

PRIMARY KEY key is used to define a primary key column may be used to define a multi-column primary keys, between columns separated by commas.

ENGINE setting storage engine, CHARSET set the encoding, comment remarks information.

#查询当前数据库下有哪些数据表(查)
show tables;

#模糊查询
#通配符:_可以代表任意的单个字符,%可以代表任意的字符
show tables like '模糊查询表名%';

#查看表的创建语句
show create table 表名;

#查看表的结构
desc 表名;

#关键字:drop  删除数据表(删)
drop table [if exists] 表名
#例如: 
drop table if exists test;

#关键字:alter 修改表名(改)
alter table 旧表名 rename to 新表名;

#修改列定义
#关键字:add 增加一列
alter table 表名 add 新列名 字段类型 [字段选项];
#例如: 
alter table test add name char(10) not null default '' comment '名字';

#关键字:drop 删除一列
alter table 表名 drop 字段名;
#例如: 
alter table test drop content;

#关键字:modify 修改字段类型
alter table 表名 modify 字段名 新的字段类型 [新的字段选项];
#例如: 
alter table test modify name varchar(100) not null default 'admin' comment '修改后名字';

#关键字:first 修改字段排序,把某个字段放在最前面
alter table 表名 modify 字段名 字段类型 [字段选项] first;
#例如: 
alter table test modify name varchar(100) not null default 'admin' comment '最前面' first;

#关键字:after 修改字段排序,字段名1放在字段名2的后面
alter table 表名 modify 字段名1 字段类型 [字段选项] after 字段名2;
#例如: 
alter table test modify name varchar(100) not null default 'admin' comment 'time字段后面' after time;

#关键字:change 重命名字段
alter table 表名 change 原字段名 新字段名 新的字段类型 [新的字段选项];
#例如: 
alter table test change name username varchar(50) not null default '' comment '用户名字';

#修改表选项
alter table 表名 表选项信息;
#例如: 
alter table test engine Myisam default charset gbk; --修改存储引擎和修改表的字符集


 

#Field Type:

MySQL supports multiple types can be roughly divided into three categories: the value, date / time and a character string (character) type.

MySQL supports all standard SQL numeric data types.

These types include strictly numeric data types (INTEGER, SMALLINT, DECIMAL and NUMERIC), and an approximate numeric data types (FLOAT, REAL and DOUBLE PRECISION).

INT is a synonym for INTEGER keyword, keyword DEC is a synonym for DECIMAL.

Stored bit BIT data type field value, and supports MyISAM, MEMORY, InnoDB and BDB tables.

As an extension to the SQL standard, MySQL also supports the integer types TINYINT, MEDIUMINT and BIGINT. The following table shows the range of each storage and integer type required.

Types of size Range (signed) Range (unsigned) use
TINYINT 1 byte (-128,127) (0,255) Small integer values
SMALLINT 2 bytes (-32 768,32 767) (0,65 535) Large integer values
MEDIUMINT 3 bytes (-8 388 608,8 388 607) (0,16 777 215) Large integer values
INT or INTEGER 4 bytes (-2 147 483 648,2 147 483 647) (0,4 294 967 295) Large integer values
BIGINT 8 bytes (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) Maximum integer value
FLOAT 4 bytes (E + -3402 823 466 38 494 351 -1175 E-38) 0, (E-1175 494 351 823 466 351 E 38,3.402 + 38) 0 (E-1175 494 351 823 466 38,3.402 E + 38) Single-precision
floating-point value
DOUBLE 8 bytes (-1,797 693 134 862 315 7 E + 308, -2 225 073 858 507 201 308 E-4) 0 (2,225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E + 308) 0 (2,225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E + 308) Double-precision
floating-point values
DECIMAL Of DECIMAL (M, D), if M> D, M + 2 is otherwise D + 2 Depending on the value of M and D Depending on the value of M and D Small value

Date and Time Types

It indicates the type of date and time is a time value DATETIME, DATE, TIMESTAMP, TIME, and YEAR.

Each type has a value of time effective range and a "zero" value of "zero" when the value of the specified unlawful MySQL can not be represented.

TIMESTAMP specific types of automatic update feature, which will be described later.

Types of Size
(bytes)
range format use
DATE 3 1000-01-01/9999-12-31 YYYY-MM-DD Date values
TIME 3 '-838:59:59'/'838:59:59' HH:MM:SS Time value or duration
YEAR 1 1901/2155 YYYY Year Value
DATETIME 8 1000-01-01 00:00:00/9999-12-31 23:59:59 YYYY-MM-DD HH:MM:SS Date and time value mixing
TIMESTAMP 4

1970-01-01 00:00:00/2038

End times are  2147483647  seconds, the Beijing  2038-1-19 11:14:07 GMT, January 19, 2038 early morning 03:14:07

YYYYMMDD HHMMSS Mixing date and time value, the time stamp

String type

It refers to a string type CHAR, VARCHAR, BINARY, VARBINARY, BLOB, TEXT, ENUM and SET. This section describes how these types work and how to use these types in a query.

Types of size use
CHAR 0-255 bytes Fixed-length string
VARCHAR 0-65535 bytes Variable-length strings
TINYBLOB 0-255 bytes No more than 255 characters in binary string
TINYTEXT 0-255 bytes Short text strings
BLOB 0-65535 bytes Long text data in binary form
TEXT 0-65535 bytes Long text data
MEDIUMBLOB Bytes 0-16777215 Binary text data in the form of medium length
MEDIUMTEXT Bytes 0-16777215 Medium length text data
LONGBLOB 0-4294967295 bytes Great text data in binary form
LONGTEXT 0-4294967295 bytes Great text data

Similar CHAR and VARCHAR type, but they save and retrieve different way. Their maximum length and whether trailing spaces are retained, it is also different. In the case conversion is not performed during the storage or retrieval.

BINARY 和 VARBINARY 类似于 CHAR 和 VARCHAR,不同的是它们包含二进制字符串而不要非二进制字符串。也就是说,它们包含字节字符串而不是字符字符串。这说明它们没有字符集,并且排序和比较基于列值字节的数值值。

BLOB 是一个二进制大对象,可以容纳可变数量的数据。有 4 种 BLOB 类型:TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB。它们区别在于可容纳存储范围不同。

有 4 种 TEXT 类型:TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT。对应的这 4 种 BLOB 类型,可存储的最大长度不同,可根据实际情况选择。

#列属性

MySQL真正约束字段的是数据类型,但是数据类型的约束太单一,需要有一些额外的约束,来更加保证数据的合法性. MySQL常用列属性有:null、not null、default、primary key、auto_increment、comment.

*空属性: null和not null

空属性: null(空,默认) 和 not null(不为空). mysql数据库默认字段都是为null的,实际开发过程中尽可能保证所有的数据都不应该为null,空数据没有意义.

*默认值default

一般配合no null使用,为空时设置默认值

*索引(此处简介描述)

mysql有多种索引:主键索引:primary key、唯一索引:unique key、全文索引:fulltext index、普通索引:key 或 index

主键索引:primary key(一张表中只能有一个字段可以使用对应的主键,用来唯一的约束该字段里面的数据,不能重复和不能为null)

#创建方式
create table test2(
  id int(10) unsigned not null primary key,
  sex char(20) not null default ''
);
#创建方式2
create table test3(
  id int(10) unsigned not null,
  name char(20) not null default '',
  primary key (id)
);

唯一键:unique key 解决表中多个字段需要唯一性约束的问题.

#唯一键(name,goods两者为唯一的)
create table test4(
  id int(10) unsigned not null,
  name char(20) not null default '',
  goods varchar(100) not null default '',
  primary key (id),
  unique key (name,goods)
);

*自动增长

自增长属性:每次插入记录的时候,自动的为某个字段的值加1(基于上一个记录). 通常跟主键搭配.

自增长规则:(1)任何一个字段要做自增长前提必须是一个索引 (2)自增长字段必须是整型数字

create table test5(
  id int(10) unsigned not null auto_increment,
  primary key (id)
);

*列描述(注释)comment 与其他的注释符不同之处在于,这里的注释内容属于列定义的一部分.

create table user(
  id int(10) unsigned not null auto_increment comment 'id',
  name char(20) not null default '' comment '名字',
  primary key (id)
)engine=InnoDB default charset=utf8 comment='用户表';

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/h18733517027/article/details/94721139