初学SQL常用到的一些指令

Posted on 2016-07-26 00:42  醉zzz  阅读(199)  评论(0)    收藏  举报

一、库

查看有哪些库:show databases;  

进入某个库:use 库名; 

新增库:create database atm; (atm为库名)

删除库:drop database if exists atm;/ drop database atm;(atm为库名)

 

二、表

注意:进行表操作前一定要先进入该表所在的库

show tables; —— 查看当前库中有哪些表

desc 表名; —— 查看某个表的表结构

1、新增表:

  create table XXX(

  字段1 数据类型 属性(是否能为nuldefaultauto_increment,

  字段2 数据类型 属性(是否能为nuldefaultauto_increment,

  primary key(主键字段)

  );

  eg

  create table account1(

    code varchar(6) not null auto_increment,

    password int(6) not null,

    money double(5,2) not null default 0.0,

    primary key(code)

  );

  (account1为表名,codepasswordmoney3个字段,其中code为主键)

 

2、删除表:drop table if exists account1; /drop table account1;account1为表名)

 

三、插入数据

1、插入全部字段数据:insert into 表名 value(字段1, 字段2, 字段3);

  insert into account1 value('100001',111111,100.00);

  insert into account1 value('100002',222222,200.00);

  insert into account1 value('100003',333333,300.00);

注意:字符串用英文单引号引住

2、插入部分字段数据:insert into 表名(字段1, 字段2value(字段1, 字段2);

  insert into account1(code,password) value('100004',444444);

 

四、查询数据

1、整表查询:select * from 表名;*表示所有字段)

2、个别字段查询:select 字段1,字段2 from 表名;

3、条件查询:where后面带出条件语句select * from account1 where code='100002';

  select password from account1 where code='100002';

  select * from account1 where code='100002' and password=111111; and表示“且”)

  select * from account1 where code='100002' or password=111111; or表示“或”)

  select * from account1 where money>200.00;

 

五、修改数据

update 表名 set 字段1=1,字段2=2;(一般要加条件)

  update account1 set password=123;(不加条件,整个表的数据被修改)

  update account1 set password=123 where code='100002'; (只修改该条件对应的数据)

  update account1 set password=123,money=0 where code='100002'; (同时修改多个字段)

 

六、删除数据

delete from 表名;(一般要加条件)

  delete from account1;(不加条件,整个表的数据被删除)

  delete from account1 where code='100006'; (只删除该条件对应的数据)