码迷,mamicode.com
首页 > 数据库 > 详细

mysql 数据库基本命令语句

时间:2018-01-14 21:17:52      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:创建   插入数据   nod   指定   int   创建数据库   客户端连接   基本   划线   

mysql mariadb

 

客户端连接
mysql -uroot -p;


客户端退出
exit 或 \q


显示所有数据库
show databases;
show schemas;

创建数据库
create database db1
charset utf8;


删除数据库
drop database db1;
drop database if exists db1;


查看表
show tables;
desc tb1;-------查看tb1的表结构
show create table tb1\G; -- 查看建表语句

 


表的增删改查

创建
create table tb1(

id int primary key auto_increment,
name varchar(20) not null,
num int not null unique,
xid int,
foreign key(xid) references tb2(id),
)engine=innodb charset=utf8;

 

修改
alter table tb1 add gender char(1) after name;

alter table tb1 modify num int null;

alter table tb1 modify id int;

alter table tb1 modify id int auto_increment;

alter table tb1 drop primary key;

alter table tb1 drop foreign key(外键约束名);

alter table tb1 drop index 约束名;
alter table tb1 modify id int;

 

删除表
drop table if exists tb1;

 

约束

主健 非空 唯一 外健 检查


默认值

num int default 1;


mysql 的一个sql_mod变量

linux 中安装mysql,sql_mod变量默认是空值,表示运行在‘不严格’模式,非空字段会插入,
字符段超长会被截断

。。。。。。。。。。。。。。。。。。

可以修改这个变量使mysql运行在严格模式


-- 查看 sql_mode 变量的值
show variables like ‘sql_mode‘;


-- 修改
set global sql_mode=‘STRICT_TRANS_TABLES‘;

------退出,重新进入mysql,再查看变量
show variables like ‘sql_mode‘;

 

* * structured query language
结构化查询语言

* sql标准语法
*各数据库厂商都有自己的扩展语法
*)mysql 扩展
*)oracle plsql
*)sql server t-sql


*sql分类
*)DDL -----定义语言,建库建表修改表
*)DML -----数据操作语言,增删改
*)DQL ----- 数据查询语言,select
插入数据insert

*insert into tb1 values(5,‘abc‘);
全部字段按字段顺序插入值


* * insert into tb1(gender, name) values(‘M‘, ‘张三‘);
向指定的字段插入值

** * insert into tb1(gender, name)
values(‘M‘, ‘张三‘),
(‘F‘, ‘李四‘),
(‘M‘, ‘王五‘);
向表中一次插入多条数据(非标准sql)


* insert into tb1 select * from tb2
insert into tb1(name, gender) select name, gender from tb2
向tb1插入tb2表中的数据


修改数据update
===========================================================
*)update tb1 set name=‘abc‘,age=23,gender=null
where id=43;


删除数据delete
===========================================================
*)delete from tb1 where .........


查询数据
=========================================================
* where 子句
=等于
<>不等于
>大于
<小于
>=大于等于
<=小于等于

between 小值 and 大值范围
in 指定几个固定取值

like 模糊查询 通常只查字符串
% -匹配0 到多个任意字符
_ -匹配单个任意字符

escape ‘\‘ : 指定转移运算符

\_普通下划线
\%普通%字符

is null


not
------------------------------
not between and
not in
is not null


and
or

mysql 数据库基本命令语句

标签:创建   插入数据   nod   指定   int   创建数据库   客户端连接   基本   划线   

原文地址:https://www.cnblogs.com/erma0-007/p/8284217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!