doc命令操纵数据库

创建一个数据库:create database firstdb(数据库名字);
删除数据库:drop database daname
查看所有的数据库:show databases;
选中要操作的数据库:use dbname;
创建一个表:
create table t_user(
  id integer,
  userName varchar(255),
  password varchar(255),
  displayName varchar(255),
  sex varchar(255),
  address text, //大文本用text
  age integer,
  score float, //单精度 7-8位有效数字
  score2 double, //双精度 15-16位有效数字
  birthday date, //日期格式 只有年月日
  times time, //只有时分秒
  arrive datetime //年月日时分秒
)
删除表:drop table tbname;
增加一个列:alter table tabname add column col type;
显示表格属性:desc tabname;
显示表格具体内容:select * from tabname;
向数据库中添加一个表格:create table if not exists tabname;
向表格中添加数据:insert into tabname (key1,key2) values(value1,value2);
删除表格中的全部数据:delete from tabname;
删除表格中的某项数据:delete from tabname where 表达式;
自动增长:auto_increment;后面不写值默认从0开始
添加自动增长:alter table tabname change key key type auto_increment;
主键:就是在声明的时候在后面加上 primary key
显示表的详细信息:desc tbname(表名);
创建完表后添加主键:alter table class add constraint primary key(id);  class:表名  id:要作为主键的建
删除主键:alter table class drop id;  class:表名  id:要作为主键的建
增加外键:alter table tabname add constraint fk_key1 foreign key(cid) references class(cid);
注意: 主表和附表都有同一个属性,并且这个属性在附表中作为主键,在主表中不是主键
where字句:
      关系运算符>,<,=,!=,>=,<=
      逻辑运算not,and,or
      is null(是否为空) is not null(是否不为空)
      between (在某两个值之间)
      in (一系列值中)
      like (相似值得比较)
      exits (是否存在符合条件的数据)
      all/any (一组数据的所有/其中的任何一个)
修改表中数据:update tabname set key1=value1,key2=value2 where 表达式;
修改编码格式:
alter TABLE  `tabname` CHANGE  `key`  `key` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL
 

更新:
between:update tabname set key1=value1 where key2 between value2 and value3;
in:update tabname set key1=value1 where key2 in(value2,value3,...);
is null:update tabname set key=value where key2 is null; 
is not null:update tabname set key=value where key2 is not null; 
子查询:selecet 要查询信息 from tabname1 where key in (select key from tabname2 where 表达式); 
关联查询:select tab1.key1,tab2.key2 from tabname1 tab1,tabname2 tab2 where 表达式;
降序排列:order by key desc; 默认是升序排列
原文地址:https://www.cnblogs.com/zttDream/p/6652713.html