CMD操作数据库
登录
mysql -u root -p
查看所有数据库
show databases;
进入数据库
use test
退出数据库
exit;
CRUD
- 增加 INSERT
- 删除 DELETE
- 修改 UPDATA
- 查找 SELECT
创建数据库
database不需要增加s。
create database newdb;
表内插入数据
INSERT INTO pet (id,name) VALUES(1,'小明')mysql> INSERT INTO pet VALUES('小白',11,null);Query OK, 1 row affected (0.09 sec)
删除数据
更新删除一定要加
where.否则就得跑路了~
mysql> delete from pet where age = 121;Query OK, 1 row affected (0.08 sec)
修改数据
更新删除一定要加
where.否则就得跑路了~
mysql> update pet set name="updata花白" where name="话白";Query OK, 1 row affected (0.10 sec)Rows matched: 1 Changed: 1 Warnings: 0
查找数据
SELECT * FROM log;
按条件查找数据
select * from log where id = 1;select id,name from log where;select id,name from log where id = 1;
查看有多少表
show tables;
创建表
CREATE TABLE pet(name VARCHAR(10),age INT(10),sex CHAR(2));
查看数据表结构
mysql> describe pet;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| name | varchar(10) | YES | | NULL | || age | int(10) | YES | | NULL | || sex | char(2) | YES | | NULL | |+-------+-------------+------+-----+---------+-------+
建表约束
- 主键约束
- 自增约束
- 外键约束
- 唯一约束
- 非空约束
- 默认约束
主键约束
。我们通过
primary key来设置主键约束。能够唯一确定一张标总的一天=条约束。通过给某个字段增加约束,使该字段不重复且不能空。
create table user (id int primary key,name varchar(20));-- 插入成功mysql> insert into user value(1,'小明');Query OK, 1 row affected (0.01 sec)-- 插入重复id 报错mysql> insert into user value(1,'小明1');ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'。-- 插入null 报错mysql> insert into user value(null,'小明1');ERROR 1048 (23000): Column 'id' cannot be null
联合主键
primary key(id,name)的方式,插入联合主键。id和name中只要有一个不同就能插入。当然也不能为空!
create table user2 (id int,name varchar(20),primary key(id,name));-- 插入成功mysql> insert into user2 values(1,'小明');Query OK, 1 row affected (0.09 sec)-- 修改一项插入成功mysql> insert into user2 values(2,'小明');Query OK, 1 row affected (0.10 sec)-- 插入重复项会报错。mysql> insert into user2 values(1,'小明');ERROR 1062 (23000): Duplicate entry '1-小明' for key 'PRIMARY'mysql>
修改表结构
mysql> create table user4(id int,name char);Query OK, 0 rows affected (0.10 sec)-- add 添加主键约束mysql> alter table user4 add primary key(id);Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0-- drop 删除主键约束mysql> alter table user4 drop primary key;Query OK, 0 rows affected (0.21 sec)Records: 0 Duplicates: 0 Warnings: 0-- modify 修改字段 添加约束mysql> alter table user4 modify id int primary key;Query OK, 0 rows affected (0.17 sec)Records: 0 Duplicates: 0 Warnings: 0
自增约束
自增约束,需要使用关键字
auto_increment。顾名思义,就是不需要自己设置,自动增加!
create table user3 (id int primary key auto_increment,name char(10));-- 单独插入name字段mysql> insert into user3 (name) values('想');Query OK, 1 row affected (0.10 sec)-- 自动增加 自增约束值(id)mysql> select * from user3;+----+------+| id | name |+----+------+| 1 | 想 |+----+------+1 row in set (0.00 sec)
唯一约束
add unique约束,该字段值不能重复。
-- 创建1create table user5 (id int ,name char(20));add unique 添加唯一约束-- add 添加alter table user5 add unique(name);-- 创建2create table user6 (id int ,name char(20),unique(id,name));-- 插入成功mysql> insert into user5 values(1,'1');Query OK, 1 row affected (0.06 sec)-- 提示name 重复mysql> insert into user5 values(2,'1');ERROR 1062 (23000): Duplicate entry '1' for key 'name'-- drop index 删除约束mysql> alter table user5 drop index name;Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0-- modify 添加mysql> alter table user5 modify name char(10) unique;Query OK, 1 row affected (0.15 sec)Records: 1 Duplicates: 0 Warnings: 0
非空约束
使用关键字
not null来定义非空约束!
create table user7 (id int ,name char(20) not null);-- Null 下面name字段,显示不能为null(不能为空)!+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || name | char(20) | NO | | NULL | |+-------+----------+------+-----+---------+-------+2 rows in set (0.01 sec)
默认约束
当我们插入字段的时候,如果没有传值,就会使用默认值!
create table user8 (id int ,name char(20) default '我是默认值');-- default 如果不输入,会存在默认值~mysql> desc user8;+-------+----------+------+-----+-----------------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+-----------------+-------+| id | int(11) | YES | | NULL | || name | char(20) | YES | | 我是默认值 | |+-------+----------+------+-----+-----------------+-------+2 rows in set (0.00 sec)
外键约束
外键约束涉及到两个表: 父表,子表(主表,副标);副标需要修改的时候需要参照主表,主表有可以修改,如果主表没有,则不可修改!
-- 班级表create table class(id int primary key,name varchar(10));-- 学生表create table students(id int primary key,name varchar(10),class_id int,-- 自己的class_id 引用 class表的idforeign key(class_id) references class(id));-- 创建班级insert into class values(1,'一班');insert into class values(2,'二班');insert into class values(3,'三班');-- 创建学生insert into students value(1,'小明',1);insert into students value(2,'小红',2);insert into students value(3,'小花',3);-- 插入错误的表,因为class里面id值,没有4这一项;-- 改案例把id设置成class的name会更明显。列如:查找的时候找不到这个4班的小红;所以报错;mysql> insert into students value(4,'小红',4);ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`newdb`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))
import axios from 'axios'export default function get(url, params) {return new Promise((resolve, reject) => {axios.get('/webapi'+url,{params:params}).then(response => {resolve(response.data);}).catch(err => {reject(err)})});}
// The Vue build version to load with the `import` command// (runtime-only or standalone) has been set in webpack.base.conf with an alias.import Vue from 'vue'import App from './App'import $ from 'jquery'import router from './router'import html2canvas from 'html2canvas'import axios from 'axios'import elementui from 'element-ui'import VueLazyload from 'vue-lazyload'import store from './store/store'import 'element-ui/lib/theme-chalk/index.css'import 'normalize.css'import '@/assets/scss/index.scss'import NProgress from 'nprogress'import 'nprogress/nprogress.css'import api from '@/utils/api'import apiGet from '@/utils/api-get'import apiDelete from '@/utils/api-delete'import brief from '@/utils/brief'import esslogan from '@/utils/esslogan'import user from '@/utils/user'import apiJson from '@/utils/api-json'import apiGet2 from '@/utils/api-get2'import userGet from '@/utils/user-get'import briefget from '@/utils/briefget'import analysisget from '@/utils/analysisget'import briefdelete from '@/utils/briefdelete'import holiday from '@/utils/holiday'import moment from 'moment'import echarts from 'echarts'import 'echarts-gl'import 'echarts-wordcloud'axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';Vue.use(elementui)Vue.prototype.$http = axiosVue.prototype.$html2canvas = html2canvasVue.prototype.api = apiVue.prototype.userGet = userGetVue.prototype.apiDelete = apiDeleteVue.prototype.apiGet = apiGetVue.prototype.analysisget = analysisgetVue.prototype.apiJson = apiJsonVue.prototype.apiGet2 = apiGet2Vue.prototype.user = userVue.prototype.brief = briefVue.prototype.briefdelete = briefdeleteVue.prototype.briefGet = briefgetVue.prototype.esslogan = essloganVue.prototype.$echarts = echartsVue.prototype.holiday = holidayVue.use(VueLazyload, {preLoad: 10})Vue.config.productionTip = falserouter.beforeEach((to, from, next) => {var mychildren = document.getElementsByTagName('head')[0].childrenfor (const item of mychildren) {if (item.src == 'http://statistics.bluefocus.com/js/statistics.js') {document.getElementsByTagName('head')[0].removeChild(item)}}let script = document.createElement('script')script.type = 'text/javascript'script.src = 'http://statistics.bluefocus.com/js/statistics.js'document.getElementsByTagName('head')[0].appendChild(script)NProgress.start()next()})router.afterEach(() => {NProgress.done()})Vue.directive('tgi', {bind: function (el, binding) {if(binding.value.zhi==Math.max.apply(Math, binding.value.shuzu.map(function(o) {return o.tgi}))){el.style.backgroundColor = '#4f622a'}else{el.style.backgroundColor = '#76923b'}},update: function (el, binding) {if(binding.value.zhi==Math.max.apply(Math, binding.value.shuzu.map(function(o) {return o.tgi}))){el.style.backgroundColor = '#4f622a'}else{el.style.backgroundColor = '#76923b'}}})Vue.directive('lzs', {bind: function (el, binding) {if(binding.value>20||binding.value<-20){el.style.backgroundColor = '#215968'}else{el.style.backgroundColor = '#7ec2d5'}},update: function (el, binding) {if(binding.value>20||binding.value<-20){el.style.backgroundColor = '#215968'}else{el.style.backgroundColor = '#7ec2d5'}}})Vue.filter('dateformat', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {if (dataStr == null) {return ''}return moment(dataStr).format(pattern)}) //时间格式过滤器// axios.interceptors.request.use(// config => {// return config;// },// error => {// return Promise.error(error);// })// 响应拦截器axios.interceptors.response.use(response => {// 如果返回的状态码为200,说明接口请求成功,可以正常拿到数据// 否则的话抛出错误if (response.status === 200 && response.data.code != undefined) {if (response.data.code != 0 && response.data.code != -10) {elementui.Message.error(response.data.msg)}if (response.data.code == -10 && !(router.currentRoute.name == 'login' || router.currentRoute.name == null || router.currentRoute.name == 'null')) {if (router.currentRoute.query.id) {router.replace({path: '/login',query: {redirect: router.currentRoute.name,id: router.currentRoute.query.id,}});} else {if (router.currentRoute.query.businessId) {router.replace({path: '/login',query: {redirect: router.currentRoute.name,businessId:router.currentRoute.query.businessId,name: router.currentRoute.query.name,type:router.currentRoute.query.type,formId:router.currentRoute.query.formId||''}});} else {router.replace({path: '/login',query: {redirect: router.currentRoute.name}});}}}return Promise.resolve(response);} else {return Promise.reject(response);}},// 服务器状态码不是2开头的的情况// 然后根据返回的状态码进行一些操作,例如登录过期提示,错误提示等error => {console.log(error)if (error.response.status) {switch (error.response.status) {// 401: 未登录// 未登录则跳转登录页面,并携带当前页面的路径// 在登录成功后返回当前页面,这一步需要在登录页操作。case 401:router.replace({path: '/login',query: {redirect: router.currentRoute.fullPath}});break;case 302:router.replace({path: '/login',query: {redirect: router.currentRoute.fullPath}});break;// 403 token过期// 登录过期对用户进行提示// 清除本地token和清空vuex中token对象// 跳转登录页面case 403:elementui.Message({message: '登录过期,请重新登录',duration: 1000,});// 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面setTimeout(() => {router.replace({path: '/login',query: {redirect: router.currentRoute.fullPath}});}, 1000);break;// 404请求不存在case 502:elementui.Message.error(error.response.data.msg)break;case 404:elementui.Message({message: '网络请求不存在',duration: 1500,});break;// 其他错误,直接抛出错误提示default:elementui.Message({message: error.response.data.msg,duration: 1500,});}return Promise.reject(error.response);}});/* eslint-disable no-new */new Vue({el: '#app',store,router,components: {App},template: '<App/>'})
