mysql 数据库使用
1. 常用命令
2. 创建表
create table test
(
test_id int, test_price decimal
);
或者
create table test2 as select * from test
修改表
alter table test2 //添加
add
(
test_a varchar(255) defaule 'aaa',
test_b varchar(255),
);
alter table test2 //修改
modify test_a varchar(255) default 'bbb';
alter table test2 drop test_a; //删除
alter table test2 rename to test3; //重命名
alter table test2 change test_b bb int; //修改列名test_b --> bb 类型修改为int
drop table test2; //删除表test2
truncate test2; //删除表里全部数据,但是保留结构。
3. 约束
非空约束:
create table test
(
test_id int not null, //不能为空
test_age int null //默认为空
);
unique唯一性约束,唯一性是指这列的值在这个表中要唯一。
create table test
(
test_id int unique, //直接加后面修饰
test_name int,
test_age int,
unique (test_name), //unique()
constraint test_uk unique(test_age) //[constraint 约束名] 约束定义
);
alter table test add unique(test_id); //修改约束性
alter table test modify test_name int unique;
alter table test drop index test_uk; //删除约束性 drop index + 约束名
Primary Key 主键约束 == 非空约束 + 唯一约束, 每个表中只有一个主键,但这个主键可以由多个数列组成。
主键:指的是,唯一标识这张表的字段,例如,书号是图书表的主键
外间:指的是 可以跟其他表建立关系的字段,例如 图书表的书名 可以在 借阅信息表中出现,这样图书表和借阅信息表之间建立了关系。
create table test //这仅是例子,正常一个table里只会有一个主键
(
test_id int primary key, //可以在primary key 前加 auto_increment 增加自增长功能。
test_age int,
test_name varchar(255),
primary key(test_age),
constraint test_pk primary key(test_name) // 可建组合约束
);
修改主键约束和唯一性约束一样,只需要改为 primary key 即可。
Foreign Key外键约束,构建一个表的两个字段 或 两个表的两个字段之间的参照关系。
MySQL支持使用列级约束但是不会生效,只是为了保持兼容性,需要用表级约束语法。
4. 索引
//create index index_name on table_name (column ....);
create index student_index on student_table2 (student_id); //建引索
drop index student_index on student_table2; //删引索
5. 视图
create or replace view view_test
as select teacher_name, teacher_id from teacher_table
with check option; //不允许修改视图数据
drop view view_test; //删除视图
6. DML 操作数据表的值
insert 插入
insert into teacher_table(teacher_name) values('kevin'); //插入一个
insert into teacher_table values(null, 'xiang'); //插入多个值
insert into test2(age) select test_id from test3; //用从句一次插入多个值
update 更新
update test2 set name='kevin';
update test2 set name='xiang' where age>=18; //接where从句,相当于if,满足条件才update
delete from 删除表里的数据
delete from test2; //删除所有记录
delete from test2 where age>18; //按条件删除
7. select语句和SQL函数
select concat(teacher_name, 'XX') from teacher_table; //concat 字符连接
select teacher_id as My_ID from teacher_table; //teacher_id as My_ID 别名
select distinct * from test3; //不显示重复行
使用where语句时支持的比较符。
select * from test3 where 21 between test_id and test_age; //选出test_id小于21和test_age大于21的列
select * from test3 where test_id in(12,18);
select * from test3 where 20 in(test_id,test_age); //选出test_id test_age == 20的列
select * from test3 where name like 'k%'; //like字符匹配, 通配符(_) 一个字符, (%)任意字符
//转义符为'\',反斜杠的转义 必须用关键字 escape 显示转义
SQL还提供 and or not 三个逻辑运算符。
select * from test3 where (test_id=12) and name like 'kevin' ;
order by 排序
select * from test3 order by test_id; //按test_id列的升序排列
select * from test3 order by test_id desc; //desc 降序排列
8. 常用函数
Case 控制流语句
select case name
when 'kevin' then 1
when 'xiang' then 2
else 'no'
end
from test3;
常用组函数
group by 分组 详解:http://blog.csdn.net/qianquanyiyan/article/details/8014406
select test_id,max(test_age) from test3 group by test_id;
mysql 数据库使用的更多相关文章
- nodejs进阶(6)—连接MySQL数据库
1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...
- 当忘记mysql数据库密码时如何进行修改
因为长时间没有使用数据库了,或者把密码改完之后就忘了数据库密码,不能正常进入数据库,也无法修改密码,有一个简单的常用修改密码方式: 1.首先找到和打开mysql.exe和mysqld.exe所在的文件 ...
- MySQL数据库和InnoDB存储引擎文件
参数文件 当MySQL示例启动时,数据库会先去读一个配置参数文件,用来寻找数据库的各种文件所在位置以及指定某些初始化参数,这些参数通常定义了某种内存结构有多大等.在默认情况下,MySQL实例会按照一定 ...
- 一起学微软Power BI系列-使用技巧(1)连接Oracle与Mysql数据库
说起Oracle数据库,以前没用过Oracle不知道,但是这1年用Oracle后,发现真的是想狂吐槽,特别是那个.NET驱动和链接字符串,特别奇葩.总归是和其他数据库不一样,标新立异,不知道为何.另外 ...
- CentOS下mysql数据库常用命令总结
mysql数据库使用总结 本文主要记录一些mysql日常使用的命令,供以后查询. 1.更改root密码 mysqladmin -uroot password 'yourpassword' 2.远程登陆 ...
- [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...
- mysql数据库主从同步
环境: Mater: CentOS7.1 5.5.52-MariaDB 192.168.108.133 Slave: CentOS7.1 5.5.52-MariaDB 192.168. ...
- PDO连接mysql数据库
1.PDO简介 PDO(PHP Data Object) 是PHP 5 中加入的东西,是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接 ...
- mysql数据库开发常见问题及优化
mysql 数据库是被广泛应用的关系型数据库,其体积小.支持多处理器.开源并免费的特性使其在 Internet 中小型网站中的使用率尤其高.在使用 mysql 的过程中不规范的 SQL 编写.非最优的 ...
- 如何在删除ibdata1和ib_logfile的情况下恢复MySQL数据库
昨天,有个朋友对公司内部使用的一个MySQL实例开启binlog,但是在启动的过程中失败了(他也没提,为何会失败),在启动失败后,他删除了ibdata1和ib_logfile,后来,能正常启动了,但所 ...
随机推荐
- EXTjs+SpringMVC+Mybatis实现照片的上传,下载,查看关键技术整理
第一个问题:如何通过Extjs4实现照片上传的布局展示以及本地照片选择后的在一个区域内进行图片预览 实现照片上传的布局展示: items : [ { xtype : 'box', itemId : ' ...
- SetTimer时间间隔的问题
1.用WM_TIMER来设置定时器 SetTimer函数的原型 UINT_PTR SetTimer( HWND hWnd, // 窗体句柄 UINT_PT ...
- 自定义cginc文件
首先定义一个cginc文件如下所示: #ifndef MY_CG_INCLUDE #define MY_CG_INCLUDE struct appdata_x { float4 vertex : PO ...
- 查看文档的后几行命令:tail
假如有一个文件test.txt,内容如下: [root@lee ~]# cat test.txt 这是第1行 这是第2行 这是第3行 这是第4行 这是第5行 这是第6行 这是第7行 这是第8行 这是第 ...
- CSS3 - 鼠标移入移出时改变样式
1,使用伪类实现样式切换伪类是CSS2.1时出现的新特性,让许多原本需要JavaScript才能做出来的效果使用CSS就能实现.比如实现下面的鼠标悬停效果,只要为:hover伪类应用一组新样式即可.当 ...
- 下周要搞大事情(ASP.NET Core & WebForms)!
下周要搞大事情(ASP.NET Core & WebForms)!
- 【BZOJ1007】[HNOI2008]水平可见直线 半平面交
[BZOJ1007][HNOI2008]水平可见直线 Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见 ...
- 【BZOJ3689】异或之 堆+可持久化Trie树
[BZOJ3689]异或之 Description 给定n个非负整数A[1], A[2], ……, A[n].对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A ...
- The space of such functions is known as a reproducing kernel Hilbert space.
Reproducing kernel Hilbert space Mapping the points to a higher dimensional feature space http://www ...
- 洛谷P2402 奶牛隐藏
洛谷P2402 奶牛隐藏 题目背景 这本是一个非常简单的问题,然而奶牛们由于下雨已经非常混乱,无法完成这一计算,于是这个任务就交给了你.(奶牛混乱的原因看题目描述) 题目描述 在一个农场里有n块田地. ...