MySQL基本用法

常用sql语句
 
查看数据库: show  databases;
 
创建一个HA的数据库: create database HA;
 
查看自己所处的位置: select database();
 
删除数据库: drop database 'wg';
 
创建表:
 
语法:**create table** 表名 (**字段名** 类型**,** 字段名 类型**,** 字段名 类型**);**
 
mysql> create table student(id int(20),name char(40),age int);
 
查看表的相关信息:
 
 use mysql ;
 
show tables;
 
查看表结构: desc student;
 
可以指定默认存储引擎和字符集:
 
mysql> create table student2(id int(20),name char(40),age int)ENGINE=MyISAM DEFAULT CHARSET=utf8;
 
 
 
删除表: drop table student2;
 
修改表名称:
 
语法  alter table 表名  rename 新表名
 
alter table student rename students;
 
修改表中的字段类型
 
语法:
 
alter table 表名 modify 要修改的字段名  要修改的类型
 
desc student;
 
alter table students modify column id int(10);
 
修改表中的字段类型和字段名称:
 
语法:**alter table** 表名 change 原字段名 新字段名 新字段类型**;**
 
alter table students change name stname char(20);
 
 
 
在表中添加字段:
 
语法: alter table students add sex enum('M','W');#enum是枚举类型,表示这个字段中的数据只能为F,M,S三个值中的一个
 
在制定位置添加字段
 
如在第一列添加一个字段
 
alter table students add uid int(10) frist;
 
在age后面添加一个字段:
 
alter table students add address char(40) after age;
 
删除表中的字段:
 
 alter table students drop address;
 
插入字段
 
语法:
 
insert   into 表名 values ( 字段值1,字段值2,字段值3);
 
insert into student values(1,'zhangs',21);
 
查询表中的记录
 
select * from  student ;
 
select id,name from student;
 
删除id 为3的行:
 
delete from students where id=3;
 
删除age为空的行;
 
delete from students  where age is null;
 
更新记录:
 
update   students set sex='M' where id=2;
 
所有的都变为2
 
update students set id=2;
 
SQL 基础条件查询语句
 
select  name,age from stuendts;
 
去重复查询语句:
 
select distinct name,age from students;
 
select distinct id,name,age from students where id=3;
 
select id,name from students where id >3 and age >25;
 
select id,name from students where id >3 or age >25;
 
mysql 区分大小写查询
 
select name from studnets where name='jk';
 
select * from students where binary name ='jk';
 
mysql 查询排序:
 
select distinct id from students order by id;
 
select distinct id from students order by id desc;
 
关于MySQL命令帮助
 
help show;
 
help select;

  1

原文地址:https://www.cnblogs.com/security-guard/p/12009631.html