标签:har 开始 函数 值类型 val null tran rda _for
创建数据库:mysql> create database test default character set utf8;
查看数据库:mysql> show databases;
查看数据库编码:mysql> select schema_name,default_character_set_name from information_schema.schemata where schema_name =‘test‘;
删除数据库:mysql>drop database test;
选择数据库:mysql> use bjsxt;
数值类型
浮点型:
字符串类型:
日期类型:
创建表:mysql> create table employees(employees_id int,last_name varchar(30),salary float(8,2));
修改表名:mysql> alter table employees rename emp;
修改列名:mysql>alter table emp change column last_name name varchar(30);
修改类型:mysql> alter table emp modify name varchar(40);
添加新列:mysql> alter table emp add column commission_pct float(4,2);
删除列:mysql> alter table emp drop column commission_pct;
创建表时指定约束:
实例:创建 departments 表包含 department_id 该列为主键且自动增长,department_name 列不允许重复,location_id 列不允含有空值。
mysql> create table departments(department_id int primary key auto_increment,department_name varchar(30) unique,location_id int not null);
实例2:创建 employees 表包含 employees_id 该列为主键且自动增长,last_name 列不允许含有空值,email 列不允许有重复不允许含有空值,dept_id 为外键参照 departments 表的主键。
mysql> create table employees(employee_id int primary key auto_increment,last_name varchar(30) not null,email varchar(40) not null unique,dept_id int,constraint emp_fk foreign key(dept_id)references departments(department_id));
添加主键约束:ALTER TABLE 表名 ADD PRIMARY KEY(列名)
修改为主键自动增长:mysql> alter table emp modify employees_id int auto_increment;
删除主键约束:mysql> alter table emp drop primary key; 先去除自动增长,再删除主键约束
修改为非空约束:mysql> alter table emp modify salary int not null;
删除非空约束:mysql> alter table emp modify salary float(8,2) null;
添加唯一性约束:mysql> alter table emp add constraint emp_uk unique(name);
删除唯一性约束:mysql> alter table emp drop key emp_uk;
添加外键约束:mysql> alter table emp add constraint e_fk foreign key(dept_id)references departements(department_id);
删除外键约束:mysql> alter table emp drop foreign key e_fk;
删除外键索引:mysql> alter table emp drop index e_fk;
添加数据:mysql> insert into departments(department_name,location_id)value("market",1);
完全插入: mysql> insert into departments values(default,"development",2); 自动增长可以用null,default,0占位
添加默认值:mysql> create table emp3(emp_id int primary key auto_increment,name varchar(30),adress varchar(50) default"unknow");
修改默认值:mysql> alter table emp3 add column job_id int default 0;
更新数据:mysql> update emp3 e set e.adress="BeiJing" where emp_id=1;
子查询更新:mysql> update emp3 e,(select adress from emp3 where emp_id =1)t set e.adress=t.adress where e.emp_id=2;
删除数据:mysql> delete from emp3 where emp_id=1;
清空数据:mysql> truncate table emp3;
手动提交事务:mysql> start transaction; commit:
mysql连接符:mysql> select concat(employee_id,‘#‘,last_name,‘#‘,email,‘#‘,salary,‘#‘,commission_pct,‘#‘) from employees;
mysql去除重复:mysql> select distinct dept_id from employees;
字符处理:
标签:har 开始 函数 值类型 val null tran rda _for
原文地址:https://www.cnblogs.com/mashiyi/p/14869074.html