mysql 笔记3

mysql  笔记3

--建库
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;