Home > Database > Mysql Tutorial > Common operations of mysql operations - basic operations of databases and data tables (1) 2 lessons in total

Common operations of mysql operations - basic operations of databases and data tables (1) 2 lessons in total

WBOY
Release: 2018-04-19 06:13:15
Original
1397 people have browsed it

 ##
 ##
 ##
 ##             文件包含:   数据库和数据表的CRUD操作 
 ##
 ##             其中包含:   数据库和数据表的:
 ##                                         数据库的字符集修改
 ##                                         数据表的名字 字段信息 等操作
 ##
 ##
 ##   // +----------------------------------------------------------------------
 ##   // | 数据库操作
 ##   // | 1.增------------创建数据库
 ##   // | 2.删------------删除数据库
 ##   // | 3.改------------修改数据库
 ##   // | 3.查------------查询数据库创建
 ##   // +----------------------------------------------------------------------#
 ##        
 ##
 ##
 #1.数据库创建语句
create database  `liu1` character set utf8  collate utf8_general_ci
 #2.删除数据库创建语句  
drop database if exists `liu1`;
 #3.修改数据库创建语句 
alter database `liu1` charset = utf8
alter database `liu1` collate utf8_general_ci;utf8_slovenian_ci
 #4.查看数据库创建语句  
show create database `liu1`;
 ##
 ##
 ##
 ##   // +----------------------------------------------------------------------
 ##   // | 数据表操作
 ##   // | 1.增------------创建表
 ##   // | 2.删------------删除表
 ##   // | 3.改------------修改表
 ##   // | 4.查------------查看表创建语句
 ##   // +----------------------------------------------------------------------#
 ##        
 ##
 ##
##################################字段类型查询#####################################################
#整型
#           1)tinyint   1字节  -128     127      UNSIGNED   255
#           2)SMALLINT  2字节  -32768   32767    UNSIGNED   65535
#           3)MEDIUMINT 3字节  -8388608 8388607  UNSIGNED   16777215
#           4)INT       4字节  -2147483648 2147483647   UNSIGNED    4294967295
#           5)BIGINT    8字节  -9223372036854775808    9223372036854775807  UNSIGNED 18446744073709551615
#小数型
#           1)float(4,2)   范围  -99.99-99.99  UNSIGNED 0-99.99 (丢失精度)
#           2)decimal(4,2) 范围  -99.99-99.99  UNSIGNED 0-99.99
#时间
#           1)date          格式:2017-11-12(没有时分秒)
#           2)datetime      格式:2017-11-12 12:12:12
#           3)timestamp     格式:2017-11-12 12:12:12(当插入数据时,默认插入系统当前时间)
#多选       set(1,2,3,4)     格式:字段名(属性1,属性2,属性3) insert into `liu1` values('1,2,4');
#                                 select * from liu1 where find_in_set('1查询的选项','hobby查询的字段名')
#单选       enum(1,2,3,4)    格式:字段名(属性1,属性2,属性3) insert into `liu1` values('1');
#
#字符串
#           1)char          范围  1-255字节
#           2)varchar       范围  1-65535字节(21844汉字)
#           3)text          
##################################字段类型查询#####################################################

##################################1.表创建语句#####################################################
CREATE TABLE IF NOT EXISTS `admin`(  
    `adminid` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID', 
    `adminuser` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '管理员账号',  
    `adminpass` CHAR(32) NOT NULL DEFAULT '' COMMENT '管理员密码',  
    `adminemail` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '管理员电子邮箱',  
    `logintime` INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '登陆时间',  
    `loginip` BIGINT NOT NULL DEFAULT '0' COMMENT '登陆IP',  
    `createtime` INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '创建时间',  
    `repasssendmailtime` INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '密码修改时间',
    `repasssedradint` INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '密码修改随机数字',
    PRIMARY KEY(`adminid`),  
    UNIQUE shop_admin_adminuser_adminpass(`adminuser`,`adminpass`),  
    UNIQUE shop_admin_adminuser_adminemail(`adminuser`,`adminemail`)  
  
)ENGINE=InnoDB DEFAULT CHARSET=utf8;  

INSERT INTO `admin`(adminuser,adminpass,adminemail,createtime) 
VALUES
('admin',md5('admin'),'857523518@qq.com',UNIX_TIMESTAMP()),
('admin',md5('admin'),'857523518@qq.com',UNIX_TIMESTAMP());
#UNSIGNED    表示无符号
#PRIMARY KEY 主键 一张表中只能有一个主键,表示该字段的数据不能重复 自带不能为空属性
#PRIMARY KEY(`adminid name`) 复合主键 表示2个字段的数据都不可能重复
#UNIQUE KEY 唯一键   一个表中可以多个唯一键  可以为空,而且空字段不参与唯一性比较
##################################2.删除表语句#####################################################
DROP TABLE IF EXISTS `admin`;
##################################3.修改表语句#####################################################
#1.修改表名
alter table admin1 RENAME TO admin2;
#2.修改表字符集
alter table `admin2` character set utf8
#3.修改表字段长度
alter table `admin2` modify column adminuser  varchar(32) not null default '';
#3.插入新字段
alter table `admin2` add name varchar(6) not null default ' ' after adminid; (在adminid后) 
#3.删除新字段
alter table liu1 drop name;
#4.修改表字段名
alter table liu1 change id id1 varchar(16) not null default ' '(注: 后面的属性要全部带上)
##################################4.查看表创建语句###################################################
show create table admin2
Copy after login

The above is the detailed content of Common operations of mysql operations - basic operations of databases and data tables (1) 2 lessons in total. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template