一、MYSQL
1.DCL(Data Control Language):数据控制语句,用于定义用户的访问权限和安全级别。包括如下SQL语句:
GRANT:授予访问权限
REVOKE:撤销访问权限
COMMIT:提交事务处理
ROLLBACK:事务处理回退SAVEPOINT:设置保存点LOCK:对数据库的特定部分进行锁定
2 .DDL(Data Definition Language ) :数据定义语句,用于库和表的创建、修改、删除。包括如下SQL语句:
CREATE TABLE:创建数据库表
ALTER TABLE:更改表结构、添加、删除、修改列长度
DROP TABLE:删除表
CREATE INDEX:在表上建立索引
DROP INDEX:删除索引
3 .DML(Data Manipulation Language ) :数据操纵语句,用于添加、删除、修改、查询数据库记录。包括如下SQL语句:
INSERT:添加数据到数据库中
UPDATE:修改数据库中的数据
DELETE:删除数据库中的数据
SELECT:选择(查询)数据
1.1 DCL(Data Control Language):数据库控制语句
1.2 DDL(Data Definition Language):数据定义语句
show databases ;
CREATE DATABASE IF NOT EXISTS we_share default charset utf8 COLLATE utf8_general_ci ;
use we_share;
drop database we_share;
show tables;
desc `order`;
create table if not exists template (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL COMMENT '名称',
`del_flag` int(11) NOT NULL DEFAULT '0' COMMENT '删除标记(0-未删除 1-已删除)',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='模板表';
CREATE TABLE dept AS
SELECT employee_id, last_name, salary*12 ANNSAL, hire_date FROM employees WHERE department_id = 80;
ALTER TABLE `app_area`
ADD COLUMN `company_id` INT(11) NOT NULL COMMENT '公司id(app_company中的id)',
MODIFY COLUMN `type` tinyint(4) NOT NULL COMMENT '用户类型(1-内部账号 2-客户账号)' AFTER `gender`,
CHANGE COLUMN `dept_id` `hold_dept_id` int(11) NOT NULL COMMENT '持有方机构ID' ,
DROP COLUMN `company_id`;
ALTER TABLE `sys_dic` RENAME TO `sys_dict`
drop table `order`;
drop table if exists `order`;
CREATE INDEX index_name ON table_name (column_name)
CREATE UNIQUE INDEX index_name ON table_name (column_name)
ALTER TABLE `app_mach_day`
ADD UNIQUE INDEX `uk_machid_date` (`mach_id`, `date`),
ADD INDEX `idx_time_machId_deviceId` (`time`, `mach_id`, `device_id`) USING BTREE ,
DROP INDEX `FK_app_project_sys_dept`,
ADD CONSTRAINT `FK_app_project_sys_dept` FOREIGN KEY (`dept_id`) REFERENCES `sys_dept` (`id`),
DROP FOREIGN KEY `FK_app_project_sys_dept`;
数据库操作
SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME='databaseName';
SELECT DISTINCT * FROM information_schema.TABLES t, information_schema.SCHEMATA n WHERE t.table_name = 'tableName' AND n.SCHEMA_NAME = 'databaseName';
1.3 DML(Data Manipulation Language):数据操纵语句
1 增
insert into t_user(id,username,password,realname) values ( 1,'jack1','000000','jack4');
insert into t_user values (2,'jack2','000000','jack4');
insert into t_user(id,username,password,realname) values
( 3,'jack4','000000','jack4'),( 4,'jack5','000000','jack5'),
( 5,'jack6','000000','jack6'),( 6,'jack7','000000','jack7');
INSERT INTO emp2 SELECT * FROM employees WHERE department_id = 90;
1表中插入 null
(1)隐式方式: 在列名表中省略该列的值
INSERT INTO t_user(id,username ) VALUES (7, 'Purchasing');
(2)显示方式: 在VALUES 子句中指定空值
insert into t_user(id,username,password,realname) values (8,'jack8',null,null);
insert into t_user values (8,'jack8',null,null);
insert into t_stu(id,username,password,update_time ) values (8,'jack8','000000', now());
INSERT INTO t_stu(id,username,password,update_time ) VALUE(8,'jack8','11111', now()) ON DUPLICATE KEY UPDATE name= '小李',password='123456'
load data infile 'e:\\d.txt' into table t_stu fields terminated by',' lines terminated by'\r\n'
2改
UPDATE t_stu SET username = 'jack7',password = 70 WHERE id = 7;
3删
DELETE FROM t_stu WHERE username = 'jack7';
DELETE FROM copy_emp;
TRUNCATE TABLE detail_dept;
4查
1.4 函数
用法 select function(列) from 表
1.字符函数
LOWER('SQL Course')
UPPER('SQL Course')
CONCAT('Hello', 'World')
SUBSTR('HelloWorld',1,5)
LENGTH('HelloWorld')
INSTR('HelloWorld', 'W')
LPAD(salary,10,'*')
RPAD(salary, 10, '*')
TRIM('H' FROM 'HelloWorld')
REPLACE('abcd','b','m')
2.数字函数
ROUND(45.926, 2)
TRUNC(45.926, 2)
MOD(1600, 300)
floor(11.84)
3.时间函数
select now();
STR_TO_DATE('9-13-1999','%m-%d-%Y')
DATE_FORMAT(‘2018/6/6’,‘%Y年%m月%d日’)
select database();
select user();
2表结构语句
SELECT * FROM INFORMATION_SCHEMA.Columns WHERE table_name = 'user' and column_name = 'username' and table_schema = database()
SELECT EXISTS (); 判断存在函数,存在返回1,不存在返回0;
|