--建库
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. PHP开发微信公众号(一)二维码的获取

    要开发微信公众号,首先进行需要注册一个,然后认证.这就不用多说了. 当然如果没有,也可以去申请一个测试号来使用,地址:https://mp.weixin.qq.com/debug/cgi-bin/sa ...

  2. 编程开发之--Oracle数据库--存储过程使用动态参数绑定(3)

    1.动态参数绑定,可以实现动态的执行不同的sql --创建包 create or replace PACKAGE MYPACKAGE AS type empcursor is ref cursor; ...

  3. (转)Go语言核心36讲之Go语言入门基础知识

  4. mysql出现 Unknown column 'bname' in 'where clause'和Unknown column 'bid' in 'field list'

    在用mysql数据库建表和修改数据库数据时,出现  Unknown column 'bname' in 'where clause'和Unknown column 'bid' in 'field li ...

  5. 4:Median of Two Sorted Arrays

    here are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  6. DES加密之强制更新下载分离器

    数据加密算法(Data Encryption Algorithm,DEA)是一种对称加密算法,很可能是使用最广泛的密钥系统,特别是在保护金融数据的安全中,最初开发的DEA是嵌入硬件中的.通常,自动取款 ...

  7. (转)Linux企业运维人员常用的150个命令分享

    Linux企业运维人员常用的150个命令分享 原文:http://www.jb51.net/article/127014.htm 本文将向大家介绍Linux企业运维人员常用的150个命令,如有不足之处 ...

  8. CoreJava笔记之JavaBean、静态方法static和final

    记住两句话: 1.属性是静态绑定到变量类型: 2.方法是动态绑定,由最终对象的方法决定 =============================== 关于JavaBean: 1.不是语法规则,是习惯 ...

  9. Node.js静态文件服务器

    首先还是先感谢github,感谢github上提供此段源码的作者.跟昨晚看的静态文件服务器来比今天的静态文件服务器稍微复杂些,可以学到很多新的东西.仔细会发现这次的代码多了一个fs.stat函数和Re ...

  10. 关于80286——《x86汇编语言:从实模式到保护模式》读书笔记15

    一.80286的工作模式 80286首次提出了实模式和保护模式的概念. 实模式:和8086的工作方式相同: 保护模式:提供了存储器管理机制和保护机制,支持多任务. 二.80286的寄存器 (一)通用寄 ...