美文网首页
mysql常见指令1

mysql常见指令1

作者: 楚糖的糖 | 来源:发表于2019-01-02 23:09 被阅读0次

显示数据库: show databases:

显示数据表:  show tables;

创建数据库:create database python charset=utf8;

删除数据库:drop database python;

选择数据库:use python;

查看当前使用的数据库:select database();

查看默认搜索引擎:show variables like '%storage_engine%';

查看MySQL支持的存储引擎类型:show engines;或者show engines \s


完整性约束条件如下表:

主键primary key:物理上存储的顺序

非空not null:此字段不允许填写空值

惟一unique:此字段的值不允许重复

默认default:当不填写此值时会使用默认值,如果填写时以填写为准

外键foreign key:对关系字段进行约束

auto_increment 标识该属性,值自动增加

常用数据类型如下:

整数:int,bit

小数:decimal

字符串:varchar,char

日期时间: date,time, datetime

枚举类型(enum)

特别说明的类型如下:

decimal表示浮点数,如decimal(5,2)表示共存5位数,小数占2位

char表示固定长度的字符串,如char(3),如果填充'ab'时会补一个空格为'ab '

varchar表示可变长度的字符串,如varchar(3),填充'ab'时就会存储'ab'

字符串text表示存储大文本,当字符大于4000时推荐使用

对于图片、音频、视频等文件,不存储在数据库中,而是上传到某个服务器上,然后在表中存储这个文件的保存路径

使用数据类型的原则是:够用就行,尽量使用取值范围小的,而不用大的,这样可以更多的节省存储空间。


创建一个表

create table students(

id int unsigned not null auo_increment primary key,

name varchar(30),

age tinyint unsigned default 0,

high decimal(5,2),

gender enum("男","女","人妖","保密")default"保密",

cls_id int unsigned);

查看表的基本结构:desc students;

插入数据:insert into students values(0,"李莉",19,188.55,"男",0);

查询表:select * from students;

查看表详细结构:show create table students;(最后加上\G,美观)

修改表:

alter table students rename xuesheng;

alter table xuesheng rename to students;


修改字段和数据

增加字段:alter table students add birthday datatime;

修改字段的类型和约束条件: alter table students modify birthday date;

修改字段类型和字段名:alter table students change birthday date default"1995-12-01"

注意:Modify和change都可以改变字段的数据类型。不同的是,change可以在改变字段数据类型的同时,改变字段名。如果要使用change修改字段数据类型,那么change后面必需跟两个同样的字段名。

末尾添加字段:alter table students add life int(4)not null first;

首位添加字段:alter table students add friend int(5) ;

一次性添加多个字段:alter table students add(wuli int(3),huaxue int(2));

删除字段:alter table students drop life;

字段修改到第一个位置:alter table students modify friend(5) first;

字段修改到指定位置: alter table students modify friend(5)after age;


更改表的存储引擎的类型:alter table students engine=myisam;

删除外键约束:

alter table students foreign key class;

删除自增长的主键:

先删除自增长再删除主键

  Alter table 表名 change id id int(10);      //删除自增长

        Alter table 表名 drop primary key;           //删除主建

练习:

create table classes(

id int unsigned not null auto_increment  primary key,

name varchar(30) default '',

age tinyint unsigned default 0,

heigh decimal(5,2),

gender enum("男","女","人妖","保密"),

cls_id int,

birthday datetime);

查询表的结构:desc classes;

相关文章

网友评论

      本文标题:mysql常见指令1

      本文链接:https://www.haomeiwen.com/subject/vlltiftx.html