--建库
create database dsdb DEFAULT CHARACTER set utf8 collate utf8_general_ci;
/*
删除数据库
drop DATABASE 数据库名称
*/
drop DATABASE dsdb;

--显示数据库
show DATABASES;

--切换数据库
use 数据库名称;
use dsdb;

--建表
create table 表名(字段名 数据类型[长度] 属性[非空 默认值 主键 注释...]) charset=utf8,ENGINE=INNODB;

create table user_info(
user_id int(11) not null primary key auto_increment,
user_name VARCHAR(50) DEFAULT null,
user_sex char(2),
user_age int(3)
) charset=utf8,ENGINE=INNODB;

--删除表
drop TABLE 表名;
drop table user_info;

--复制表
create table 新表 select * from 旧表; //不能复制键
create table user_info_back select * from user_info;

create table 新表 like 旧表;
create table user_info_back2 like user_info;
--修改
alter table 表名 add 字段名 数据类型 属性; --添加字段
alter table user_info add user_address varchar(500) not null;

alter table 表名 change 旧的字段名称 新名称 数据类型 属性;--修改字段
alter table user_info change user_address my_address char(100) null;

--删除
alter table 表名 drop 字段名
alter table user_info drop my_address;

--添加主键
alter table 表名 add primary key (字段名);
alter table user_info_back add primary key (user_id);

--修改名称
alter table 表名 rename to 新名称;
alter table user_info_back rename to user_new_table;

数据操作:
插入数据:
insert into 表名([字段名称]) value ([列表值])
insert into 表名 set 字段名 = 值, 字段名= 值......ALTER
--复制表
insert into 表名1 select * from 表名2;
use yygdb;

show variables like 'autocommit';
--值0和OFF都是一样的,当然,1也就表示ON。

SET AUTOCOMMIT = 0;
insert into userinfo (useName,useSex,useAge)
VALUES
('托马斯','男',6),
('托马斯1','男',6),
('托马斯2','男',6),
('托马斯2','男',6),
('托马斯2','男',6),
('托马斯2','男',6),
('托马斯3','男',6);
ROLLBACK;

select * from userinfo
insert into userinfo set useName="詹姆斯",useSex='男',useAge=20;
create table userinfo_new select * from userinfo;

--修改
update 表名 set 字段名称=值,字段名称=值,字段名称=值.... where 条件表达式;

update userinfo set useSex='车' , useAge='100' where id<24 and useAge<18;

--删除 几种方式区别?
delete from 表名 where 条件表达式;

delete from userinfo where useName='托马斯2';//删除数据不会释放表空间

truncate table 表名;//删除数据释放表空间

truncate table userinfo;

DROP TABLE IF EXISTS order_table;

--查询
select * [字段名] from 表名 where 条件表达式;

--查询时 取部分数据(分页查询) limit
select * [字段名] from 表名 limit 数据条数;

select * from userinfo order by id DESC limit 2,3;

select * from userinfo limit (当前页数-1) * 每页条数, 每页条数;

select * from userinfo limit 8,4;

create table employee(
id SMALLINT(4) not null auto_increment,
employee_id INT(4) not null,
first_name varchar(25) not null,
last_name varchar(35),
email VARCHAR(55) not null,
salary decimal(8,2) not null,
primary key (id)
);

insert into employee VALUES(1,1,'bob','connwer','245532fdf',453.90),
(2,2,'bob2','connwehr','245532fdf',453.90),
(3,3,'bob3','conndfwer','245532fdf',453.90),
(4,4,'bob4','connwbfder','245532fdf',453.90);

创建视图 虚表(保护数据信息,不让别人随便查看,如工资)
create view employee_contact_info_view as
select first_name ,last_name,email from employee order by last_name asc;

select * from employee_contact_info_view;
show tables;

show create view employee_contact_info_view;
视图可以和所有子句和函数联合使用

新建视图,并给字段重新取名字
create view employee_view(firstname,lastname,emailaddress)as
select first_name,last_name,email from employee order by last_name asc;
alter view employee_contact_info_view(First_name,Last_name,Employee_id) as
select first_name,last_name,employee_id from employee order by last_name asc;
update employee_contact_info_view set Last_name='hahah' where Employee_id=1;

select * from information_schema.views;

drop view employee_view;

mysql 笔记3的更多相关文章

  1. MySQL笔记汇总

    [目录] MySQL笔记汇总 一.mysql简介 数据简介 结构化查询语言 二.mysql命令行操作 三.数据库(表)更改 表相关 字段相关 索引相关 表引擎操作 四.数据库类型 数字型 字符串型 日 ...

  2. 涂抹mysql笔记-数据库中的权限体系

    涂抹mysql笔记-数据库中的权限体系<>能不能连接,主机名是否匹配.登陆使用的用户名和密码是否正确.mysql验证用户需要检查3项值:用户名.密码和主机来源(user.password. ...

  3. centos7.2下安装Mysql笔记

    centos7.2下安装Mysql笔记 安装 MySQL 适用于 CentOS 7.0 或以后版本: yum install mariadb mariadb-server 适用于 CentOS 6.8 ...

  4. MySQL笔记(六)游标练习

    23.3.1 Trigger Syntax and Examples 意义不明的几道练习,留着备用. 感觉不好写,而且难以调试..不知道以后会不会有实际的应用场景. 环境:MySQL 笔记(三)由 t ...

  5. mysql 笔记(一)

    mysql 笔记 预留 mysql> use mysql; mysql> grant all privileges  on *.* to root@'%' identified by &q ...

  6. 【MySQL笔记】SQL语言四大类语言

     SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL.   1. 数据查询语言DQL 数据查询语言DQL基本结构是由SELECT子句,FROM子句, ...

  7. Mysql 笔记二

    Mysql 笔记二 Mysql 笔记二 Table of Contents 1. 前言 2. Master Thread 工作方式 2.1. 主循环(loop) 2.2. 后台循(backgroup ...

  8. 深入浅出mysql笔记---1、mysql下载安装

    深入浅出mysql笔记---1.mysql下载安装 一.总结 一句话总结: linux下rpm安装即可 1.linux的wget命令作用? 下载文件的工具:比如wget http://cn.wordp ...

  9. 深入浅出mysql笔记---0、序

    深入浅出mysql笔记---0.序 一.总结 一句话总结: 心得:买书之前建议先找找电子书,纸质书太难带了 1.开源作用? 开源对mysql的发展至关重要 2.mysql在2002年就全面支持了事务, ...

  10. 最全mysql笔记整理

    mysql笔记整理 作者:python技术人 博客:https://www.cnblogs.com/lpdeboke Windows服务 -- 启动MySQL net start mysql -- 创 ...

随机推荐

  1. scrollto 到指定位置

    goTo = function(target){ var scrollT = document.body.scrollTop|| document.documentElement.scrollTop ...

  2. supervisor 使用

    cat /etc/supervisord.conf https://www.cnblogs.com/yuzhoushenqi/p/6825204.html http://127.0.0.1:1001/ ...

  3. orange

    选型:使用orange系统 orange与kong的比较1.kong整体代码上较凌乱, orange相对较有条理2.kong本身不支持后台管理页面,只能通过api方式增,删,改plugin, oran ...

  4. json "key" 注意

    json 的key只能是字符串 必须使用 双引号

  5. 991 AlvinZH的奇幻猜想----整数乘积plus(背包DP大作战P)

    914 AlvinZH的奇幻猜想----整数乘积puls 思路 难题.动态规划. 将数字串按字符串输入,处理起来更方便些. dp[i][j]:表示str[0~i]中插入j个乘号时的乘积最大值.状态转移 ...

  6. Saiku2.6 Saiku315 链接SQL的JDBC字符串

    Saiku26 type=OLAP name=CloudConn driver=mondrian.olap4j.MondrianOlap4jDriver location=jdbc:mondrian: ...

  7. Ubuntu14.04安装libusb

    https://www.cnblogs.com/ettie999/p/8142973.html libuvc是一个跨平台的USB视频设备库,建立在libusb之上. 它能够对导出标准USB视频类(UV ...

  8. codeforces1137B kmp(fail的妙用)

    题目传送门 题意:给出$s$和$t$两个串,让你构造出一个答案串,使得答案串中的01数量和s一样,并且使$t$在答案串中作为子串出现次数最多. 思路: 要想出现的次数尽可能多,那么就要重复的利用,哪一 ...

  9. 怎么搭建vue-cli脚手架

    我们在使用vue搭建项目的时候,经常要使用到vue-cli. 一.安装node.js 去node官网下载并安装node,一直next就行. 等待安装完毕,输入node-v,如果输出版本号,那说明已经成 ...

  10. python logging模块的使用

    logging 专门用于记录日志的模块,相对于print来说,logging 提供了日志信息的分级.格式化.过滤等功能.在程序中定义丰富有条理的log信息,可以方便分析程序的运行状态,在发生问题是可有 ...