MySQL 必知必会学习笔记(常用命令二)
CREATE TABLE students(student_id INT UNSIGNED, name VARCHAR(30), sex CHAR(1), birth DATE, PRIMARY KEY(student_id));
INSERT INTO students(student_id, name, sex, birth) VALUES(41048101, 'Lucy Green', '1', '1990-02-14');
SELECT name FROM students WHERE student_id = 41048101;
ALTER TABLE students MODIFY student_id INT(10) AUTO_INCREAMENT;
-- 创建数据库
CREATE DATABASE test_db;
SHOW CREATE DATABASE test_db\G;
DROP DATABASE test_db;
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE tb_empl(id INT(1), name VARCHAR(25), deptID INT(11), salary FLOAT);
CREATE TABLE tb_empl2(id INT(1) PRIMARY KEY, name VARCHAR(25), deptID INT(11), salary FLOAT);
CREATE TABLE tb_empl3(id INT(1), name VARCHAR(25), deptID INT(11), salary FLOAT, PRIMARY KEY(id));
CREATE TABLE tb_empl4(name VARCHAR(25), deptID INT(11), salary FLOAT, PRIMARY KEY(name, deptID));
CREATE TABLE tb_dept1(id INT(11) PRIMARY KEY, name VARCHAR(22) NOT NULL, location VARCHAR(50));
CREATE TABLE tb_emp5(id INT(11) PRIMARY KEY, name VARCHAR(25), deptID INT(11), salary FLOAT,
CONSTRAINT fk_emp_dept1 FOREIGN KEY(deptID) REFERENCES tb_dept1(id));
CREATE TABLE tb_emp6(id INT(11) PRIMARY KEY, name VARCHAR(25) NOT NULL, deptID INT(11), salary FLOAT,
CONSTRAINT fk_emp_dept2 FOREIGN KEY(deptID) REFERENCES tb_dept1(id));
CREATE TABLE tb_dept2(id INT(11) PRIMARY KEY,name VARCHAR(22) UNIQUE,location VARCHAR(50));
CREATE TABLE tb_dept3(id INT(11) PRIMARY KEY, name VARCHAR(22), location VARCHAR(50), CONSTRAINT STH UNIQUE(name));
CREATE TABLE tb_emp7(id INT(11) PRIMARY KEY, name VARCHAR(25) NOT NULL, deptID INT(11) DEFAULT 1111, salary FLOAT,
CONSTRAINT fk_emp_dept3 FOREIGN KEY(deptID) REFERENCES tb_dept1(id));
CREATE TABLE tb_emp8(id INT(11) PRIMARY KEY AUTO_INCREMENT, name VARCHAR(25) NOT NULL, deptID INT(11), salary FLOAT,
CONSTRAINT fk_emp_dept5 FOREIGN KEY(deptID) REFERENCES tb_dept1(id));
INSERT INTO tb_emp8(name,salary) VALUES('Lucy', 20000),('Lilei', 25000),('Hanmeimei', 23000);
DESC tb_emp8;
SHOW CREATE TABLE tb_emp8;
ALTER TABLE tb_dept3 RENAME TO tb_deptment3;
ALTER TABLE tb_dept1 MODIFY name VARCHAR(30);
ALTER TABLE tb_dept1 MODIFY location VARCHAR(24);
ALTER TABLE tb_dept1 CHANGE location loc INT(11);
ALTER TABLE tb_dept1 MODIFY loc VARCHAR(11);
ALTER TABLE tb_dept1 ADD managerId INT(10);
ALTER TABLE tb_dept1 ADD column1 VARCHAR(12) NOT NULL;
ALTER TABLE tb_dept1 ADD colunm2 INT(11) FIRST;
ALTER TABLE tb_dept1 ADD column3 INT(11) AFTER name;
ALTER TABLE tb_dept1 DROP colunm2;
ALTER TABLE tb_dept1 MODIFY column1 VARCHAR(12) FIRST;
ALTER TABLE tb_dept1 MODIFY column1 VARCHAR(12) AFTER loc;
SHOW CREATE TABLE tb_deptment3;
ALTER TABLE tb_deptment3 ENGINE=MyISAM;
CREATE TABLE tb_emp9(id Int(11) PRIMARY KEY, name VARCHAR(25), deptId INT(11), salary FLOAT,
CONSTRAINT fk_emp_dept FOREIGN KEY(deptId) REFERENCES tb_dept1(id));
ALTER TABLE tb_emp9 DROP FOREIGN KEY fk_emp_dept;
DROP TABLE IF EXISTS tb_dept2;
CREATE TABLE tb_dept2(id INT(11) PRIMARY KEY, name VARCHAR(22), location VARCHAR(50));
CREATE TABLE tb_emp(id INT(11) PRIMARY KEY, name VARCHAR(25), deptId INT(11), salary FLOAT,
CONSTRAINT fk_emp_dept FOREIGN KEY(deptId) REFERENCES tb_dept2(id));
ALTER TABLE tb_emp DROP FOREIGN KEY fk_emp_dept;
DROP TABLE tb_dept2;
CREATE DATABASE company;
SHOW CREATE DATABASE company;
USE company;
CREATE TABLE offices(officeCode INT(10) NOT NULL UNIQUE,
city INT(11) NOT NULL, address VARCHAR(50), country VARCHAR(50), postalCode VARCHAR(25) UNIQUE, PRIMARY KEY(officeCode));
CREATE TABLE offices_new(officeCode INT(10) NOT NULL UNIQUE,
city INT(11) NOT NULL, address VARCHAR(50), country VARCHAR(50) NOT NULL, postalCode VARCHAR(25) UNIQUE, PRIMARY KEY(officeCode));
#ALTER TABLE offices MODIFY officeCode INT(10) NOT NULL UNIQUE PRIMARY KEY(officeCode));
#ALTER TABLE offices MODIFY officeCode PRIMARY KEY(officeCode));
ALTER TABLE offices ADD PRIMARY KEY(officeCode);
ALTER TABLE offices MODIFY country VARCHAR(50) NOT NULL;
CREATE TABLE employees(employeeNumber INT(11) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT,
lastName VARCHAR(50) NOT NULL,firstName VARCHAR(50) NOT NULL,mobile VARCHAR(25) UNIQUE,
officeCode INT(10) NOT NULL, jobTitle VARCHAR(50) NOT NULL, birth DATETIME NOT NULL,
note VARCHAR(255), sex VARCHAR(5), CONSTRAINT fk_emp_ofc FOREIGN KEY(officeCode) REFERENCES offices(officeCode) );
ALTER TABLE employees MODIFY mobile VARCHAR(25) AFTER officeCode;
ALTER TABLE employees CHANGE birth employee_birth DATETIME;
ALTER TABLE employees MODIFY sex CHAR(1) NOT NULL;
ALTER TABLE employees DROP note;
ALTER TABLE employees ADD favoriate_activity VARCHAR(100);
DROP TABLE offices;
ALTER TABLE employees DROP FOREIGN KEY fk_emp_ofc;
ALTER TABLE employees ENGINE=MyISAM;
ALTER TABLE employees RENAME TO employees_info;
SHOW DATABASES;
CREATE DATABASE IF NOT EXISTS Market;
USE Market;
CREATE TABLE customers(c_num INT(11) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT,
c_name VARCHAR(50), c_contact VARCHAR(50), c_city VARCHAR(50), c_birth DATETIME NOT NULL );
ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth;
ALTER TABLE customers MODIFY c_name VARCHAR(70);
ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50);
ALTER TABLE customers ADD c_gender CHAR(1);
ALTER TABLE customers RENAME TO customers_info;
ALTER TABLE customers_info DROP c_city;
ALTER TABLE customers_info ENGINE=MyISAM;
CREATE TABLE orders(o_num INT(11) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT, o_date DATE, c_id VARCHAR(50),CONSTRAINT fk_or_cu FOREIGN KEY(c_id) REFERENCES customers_info(c_num));
CONSTRAINT fk_emp_ofc FOREIGN KEY(officeCode) REFERENCES offices(officeCode)
CREATE TABLE tmp1(x TINYINT, y SMALLINT, z MEDIUMINT, m INT, n BIGINT);
CREATE TABLE tmp1(x TINYINT, y SMALLINT, z MEDIUMINT, m INT, n BIGINT);
CREATE TABLE worker(ID INT AUTO_INCREAMENT NOT NULL PRIMARY KEY,Name VARCHAR(30));
INSERT INTO worker VALUES(NULL, 'jimy');
INSERT INTO worker VALUES(NULL, 'Tom');
INSERT INTO worker VALUES(NULL, 'Kevin'),(NULL, 'Michal'), (NULL,'Nick');
CREATE TABLE fruits(f_id CHAR(10) NOT NULL, s_id INT NOT NULL, f_name CHAR(255) NOT NULL, f_price DECIMAL(8,2) NOT NULL, PRIMARY KEY(f_id));
INSERT INTO fruits(f_id, s_id, f_name, f_price) VALUES('a1',101,'apple', 5.2),
('b1',101,'blackberry', 10.2),
('bs1',102,'orange', 11.2),
('bs2',105,'melon', 8.2),
('t1',102,'banana', 10.3),
('t2',102,'grape', 5.3),
('o2',103,'coconut', 9.2),
('c0',101,'cherry', 3.2),
('a2',103,'apricot', 2.2),
('l2',104,'lemon', 6.4),
('b2',104,'berry', 7.6),
('m1',106,'mango', 15.6),
('m2',105,'xbabay', 2.6),
('t4',107,'xbababa', 3.6),
('m3',105,'xxtt', 11.6),
('b5',107,'xxxx', 3.6);
CREATE TABLE test.t(s1 INT, PRIMARY KEY(s1));
DEMIMITER //
CREATE PROCEDURE handledemo()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1;
SET @x = 1;
INSERT INTO test.t VALUES(1);
SET @x = 2;
INSERT INTO test.t VALUES(1);
SET @x = 3;
END;
//
DELIMITER ;
IF val IS NULL
THEN SELECT 'val' IS NULL;
ELSE SELECT 'val' IS NOT NULL;
END IF;
CASE val
WHEN 1 THEN SELECT 'val is 1';
WHEN 2 THEN SELECT 'val is 2';
ELSE SELECT 'val is not 1 or 2';
END CASE;
CASE
WHEN val IS NULL THEN SELECT 'val is null';
WHEN val < 0 THEN SELECT 'val is less than 0';
WHEN val > 0 THEN SELECT 'val is greater than 0';
ELSE SELECT 'val is 0';
END CASE
DELCARE id INT DEFAULT 0;
add_loop:LOOP
SET id = id + 1;
IF id >= 10 THEN LEAVE add_loop;
END IF;
END LOOP add_loop;
add_num: LOOP
SET @count = @count + 1;
IF @count = 50 THEN LEAVE add_num;
END LOOP add_num;
CREATE PROCEDURE doiterate()
BEGIN
DECLARE p1 INT DEFAULT 0;
my_loop:LOOP
SET p1 = p1 + 1;
IF p1 < 10 THEN ITERATE my_loop;
ELSEIF p1 > 20 THEN LEAVE my_loop;
END IF;
SELECT 'p1 is BETWEEN 10 AND 20';
END LOOP my_loop;
END;
DECLARE id INT DEFAULT 0;
REPEAT
SET id = id + 1;
UNTIL id >= 10;
END REPEAT;
DECLARE i INT DEFAULT 0;
WHILE i < 10 DO
SET i = i + 1;
END WHILE;
DELIMITER //
CREATE PROCEDURE CountProc(IN sid INT, OUT num INT)
BEGIN
SELECT COUNT(*) INTO num FROM fruits WHERE s_id = sid;
END //
DELIMITER ;
CALL CountProc(101, @num);
SELECT @num;
DELIMITER //
CREATE FUNCTION CountProc(sid INT)
RETURNS INT
BEGIN
RETURN (SELECT COUNT(*) FROM fruits WHERE s_id = sid);
END //
DELEMITER ;
SELECT CountProc(101);
SHOW STATUS;
SHOW PROCEDURE STATUS LIKE 'C%'\G;
CREATE TABLE sch(id INT, name VARCHAR(50), glass VARCHAR(50));
INSERT INTO sch VALUES(1,'xiaoming','glass 1'),(2, 'xiaojun','glass 2');
DELIMITER //
CREATE FUNCTION count_sch()
RETURNS INT
RETURN (SELECT COUNT(*) FROM sch);
//
DELIMITER //
CREATE PROCEDURE add_id(OUT count INT)
BEGIN
DECLARE itmp INT;
DECLARE cur_id CURSOR FOR SELECT id FROM sch;
DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_id;
SELECT count_sch() INTO count;
SET @sum = 0;
OPEN cur_id;
REPEAT
FETCH cur_id INTO itmp;
IF itmp <10
THEN SET @sum = @sum + itmp;
END IF;
UNTIL 0 END REPEAT;
CLOZE cur_id;
END;
SELECT @a, @sum //
CREATE TRIGGER testref
/* BEFORE INSERT ON test1 FOR EACH ROW */
BEGIN INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END //
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME='ins_num'\G;
GRANT SELECT,UPDATE ON *.* TO 'william'@'localhost' IDENTIFIED BY '111111';
SELECT Host,User, Select_priv,Update_priv FROM mysql.user WHERE user='william';
UPDATE `QB_SysWriSenImitation` SET `QuestNum` = 2 WHERE `Id` = '01-Imitation-Primary-061';
MySQL 必知必会学习笔记(常用命令二)的更多相关文章
- Redis学习笔记--常用命令
以下为本人学习Redis的备忘录,记录了大部分常用命令 1.客户端连接redis服务端: ===启动Redis服务端 redis-server /yourpath/redis.conf ===启动Re ...
- Hexo学习笔记--常用命令及部署步骤
参考资料: Hexo搭建Github静态博客:http://www.cnblogs.com/zhcncn/p/4097881.htmlHexo:独立博客新玩法:http://www.aips.me/h ...
- SQL学习笔记---常用命令
常用命令 变量 1.声明 declare @变量名 类型,… 2.赋值 1.同时赋值多个变量(可以结合查询) select @变量名=表达式1,表达式2 2.单个赋值(推荐) set @变量名=表达式 ...
- ionic学习笔记—常用命令
Ionic CLI介绍 Ionic CLI是开发Ionic应用程序过程中使用的主要工具.它就像一个瑞士军刀:它在一个界面下汇集了大量工具. CLI包含许多对Ionic开发至关重要的命令,例如start ...
- MySQL 必知必会学习笔记(常用命令一)
SHOW DATABASES;USE LangLibCEE;SHOW TABLES;SHOW COLUMNS FROM customers;DESC customers; SHOW STATUS WH ...
- Redis学习笔记~常用命令总结
回到目录 客户端redis-cli常用的命令总结 连接到服务器 redis-cli -h 127.0.0.1 -p 6379 --连接指定的redis服务器 发布/订阅, pub/sub模式运行在re ...
- MongoDB学习笔记—常用命令
这里记录一下MongoDB常用的命令 数据库相关 创建数据库 use DATABASE_NAME 如果数据库不存在,则创建数据库,否则切换到指定数据库. 删除数据库:切换到要删除的数据库下,执行命令即 ...
- linux/linux学习笔记-常用命令(mooc)
一.命令基本格式及文件处理命令 1.linux命令格式 简化选项用-表示,完整选项用--表示 注:linux通过权限位第1位(-:文件 / d:目录 / l:链接文件 来区分文件类型) 2.linux ...
- git 学习笔记(常用命令)
1.新建一个文件,如果没有使用git add 命令将它提交到暂存区,那么这个文件就还没有被跟踪. 2.通过配置.gitignore文件可以指定要忽略的文件,被忽略的文件夹是不会被提交到暂存区的.所以这 ...
随机推荐
- IDEA调用其它模块module的类方法
IDEA支持调用本project中其他模块的包里面的方法(需要配置该模块和src同级的.iml文件,配置完需要等一会才生效,尝试切换到桌面以...) 这样会使IDEA的project的模块间有依赖,该 ...
- ArcGIS Pro开发Web3D应用(4)——Portal发布三维没有“发布”选项解决
如题,将三维场景包.slpk上传并发布,未看到发布选项. 问题核心就在于环境没配置好,主要有2点: 1.验证联合托管状态 需要将托管服务器与Portal进行联合,并进行验证,此情况需要在WebAdap ...
- vim实现实时自动保存
进https://www.vim.org/scripts/script.php?script_id=4521网站下载vim -auto-save wget https://www.vim.org/s ...
- 无法添加注解@Resource
Spring项目中缺少javax.annotation包的依赖
- vsphere client/web client 开启ESXi SSH服务
●vsphere client 开启ESXi SSH服务 1,从主机和群集列表选择主机esxi01.2,选择[配置]-->[软件]列表-->[安全配置文件]--> [服务],点击右上 ...
- VGG
2019-04-08 13:30:58 VGG模型是2014年ILSVRC竞赛的第二名,第一名是GoogLeNet.但是VGG模型在多个迁移学习任务中的表现要优于googLeNet.而且,从图像中提取 ...
- ubuntu终端
https://blog.csdn.net/hello_new_life/article/details/75099249 ctrl+altl+F1进入命令行 ctrl+altl+F7退出命令行 在终 ...
- package.json bin的作用
许多包有一个或多个可执行文件(executable),他们希望直接导入到全局路径里面,这样可以直接使用,npm很容易达到这点, A lot of packages have one or more e ...
- spring boot2 kafka
一.软件版本 1.linux:centos6 2.zookeeper:zookeeper-3.4.1 3.kafka:kafka_2.12-2.2.0 4.jdk:1.8 5.instelliJ Id ...
- expect使用
expect时用与提供自动交互的工具.比如如果想要用ssh登陆服务器,每次都输入密码你觉得麻烦,那你就可以使用expect来做自动交互,这样的话就不用每次都输入密码了. 先看例子: #!/usr/bi ...