Mysql常用命令

来源:互联网 发布:网络用语飙车什么意思 编辑:程序博客网 时间:2024/06/05 18:10

基本操作

数据库登陆/退出:mysql -u user -p password -h host -P port    \q修改MySql提示符:①连接时指定:mysql -u user -p password --prompt 提示符;②连接后指定:prompt 提示符;(可选:\u用户\h服务器\d数据库\D时间)查看数据库/数据表:show databases; /show tables [from db_Name];显示如何创建数据库/ 数据表:show create database databaseName;show create table [database.]tableName;更改数据库编码为utf-8:alter database databasesName character set utf8;查看所在数据库/查看时间 /查看版本 /查看警告信息:select database(); /select now(); /select version();  / show warnings;切换使用指定数据库/查询表:use databaseName; /select * from tableName [databaseName.tableName];

创建数据库:create database [if not exists] db_Name  character set [=] charSetName;修改数据库:alter database db_Name character set = charName;删除数据库:drop database [if exists] db_Name;

数据类型:

①整型tinyint      -128127     一个字节smallint    -3276832767  两个字节mediumint -83886088388607 三个字节int      -21474836482147483647 四个字节bigint -92233720368547758089223372036854775807 八个字节②浮点型float(M,D) 单精度浮点大约精确到7位小数位double(M,D) M为数字总位数,D是小数点后的位数③日期时间型DATE 3字节 1000.1.1~9999.12.31DATETIME 8字节 1000.1.1.0:0:0~9999.12.31.23:59:59TIMESTAMP 4字节 1970.1.1~2037TIME 3字节  -8385959~8385959YEAR 1字节 70~69④字符型char(M) 定长类型不够补齐varchar(M) 变长类型tinytext 255字节textmediumtextlongtextENUM('value1','value2')枚举值SET('value1','value2')集合

数据表

打开数据库:use db_Name;   行:记录 列:字段创建数据表:create table [if not exists] table_Name(column_name data_type,………);查看数据表结构:show columns from tb_Name;
插入记录://①普通插入语句insert into tb_Name[(col_Name,…)] values(value,…);insert into tb_Name values(null,value1,value2,…)[,第二条记录];为自动编号的字段赋值时:null 或 default//②下面的方法可用于子查询insert into tb_Name set col_Name = {expr|DEFAULT},…//③可将查询结果插入指定的数据库insert into tb_Name [(col_Name,…)] select
记录的更新:update tb_Name set col_Name ='sth' [,col_Name2 = 'sth' where col_Name2 = 'sth' ] ;记录的查找:select ecpr,… from tb_Name;记录的删除:delete from tb_Name where col_Name = values;//不指定where条件时删除全部表!

非空/ 自动编号/主键:not null禁止为空,默认可为空auto_increment自动编号,必须与主键组合使用primary key 一张变只存在一个主键,自动为not null 保证记录的唯一性唯一约束UNIQUE KEY / 默认约束DEFAULT:字段可以为空值,可存在多个唯一约束当插入记录时,没有明确赋值时自动赋予默认值外键约束的要求:父表子表相同存储引擎 : InnoDB ( 具有外键列的表为子表 )外键列和参照列必须具有相同的数据类型,数字长度、有无符号位必须相同,而字符的长度可不同必须创建索引,如果参照列不存在索引将自动创建索引foreign key(col_Name) references  tb_Name(col_Name);外键约束的参照操作(ON DELETE):CASCADE:从父表删除或更新同时自动删除或更新子表中匹配的行SET NULL:从父表删除或更新行,并设置子表中的外键列为NULL。如果使用,必须保证子表列没有指定NOT NULLRESTRICT:拒绝对父表的删除或更新操作NO ACTION:标准SQL关键字,在MySql中同RESTRICT

添加数据表一列: alter table tb_Name add col_Name col_definition [first|after col_Name];col_definition是列定义,first为最前,默认为最后添加,after为在某列之后添加数据表多列:alter table tb_Name add (col_Name col_definition,…);删除数据列:alter table tb_Name drop col_Name1,drop col_Name2;删除一列的同时添加一列:alter table tb_Name drop col_Name1,add col_Name2 col_definition;添加主键约束  /添加唯一约束 /添加外键约束  /添加删除默认约束:alter table tb_Name add primary key(col_Name);alter table tb_Name add unique key(col_Name);alter table tb_Name add foreign key(col_Name) references table_Name(col_Name);alter table tb_Name alter col_Name {set default value|drop default};删除主键约束  /删除唯一约束 /删除外键约束:alter table tb_Name drop primary key;alter table tb_Name drop index col_Name;alter table tb_Name drop foreign key col_系统名称;修改列定义 /列名称定义:alter table tb_Name modify col_Name col_definition {first | after col_Name};alter table tb_Name change old_col_Name new_col_Name definition;数据表更名:alter table tb_Name rename new_tb_Name;rename table tb_Name to new_tb_Name [,tb_Name1 to new_tb_Name];  

如有错误,望斧正~

0 0