create table test2 select * from test where 1=2
-- 只复制表结构
create table city1 like city;
INSERT INTO test2 SELECT * FROM test;
-- 上面的表必须存在
-- 复制整张表的数据
create table test2 select * from test
-- create database xxx charset
-- create table xxx (id int,xxxxxx)
-- drop table
-- drop database
-- alter table add/drop/modify/change
-- create user
-- drop user
select id,name from test limit 1,3; --- 显示第一行之后的三行
用户管理
----
定义 : 用户名+主机域
功能:连接数据库、管理数据库对象
连接数据库:
1、定义用户:用户名+主机域, 密码
2、定义权限:对不同的对象进行权限(角色)定义
grant 权限 on 权限范围(对象) to 用户 identified by '';
show grants for oldboy@'10.0.0.%';
权限(角色):
select
update
delete
insert
drop
create
ALL
replication slave
权限范围:
*.* 所有数据库对象
oldboy.* oldboy单库下所有对象
oldboy.test 单表级别
用户:
repl@localhost
repl@'10.0.0.53'
repl@'10.0.0.%'
repl@'10.0.0.5%'
-----
要求:
1、用户只能通过10网段访问,用户名为oldboy,密码为123
2、只能对oldboy数据库下的对象进行增insert create、改update、查select
grant select,insert,update,create on oldboy.* to oldboy@'10.0.0.%' identified by '123';
drop user oldboy@'10.0.0.%'
----------------
skip-grant-tables 启动过程中跳过授权表。
/application/mysql/bin/mysqld_safe --skip-grant-tables --skip-networking &
在这中模式启动情况下:
无密码登录
网络用户无法登陆
只能本地登录
和授权有关的命令都不能执行了
grant
revoke
drop user
create user
use mysql
update user set password=PASSWORD('123456') where user='sys' and host='localhost'
flush privileges;
5.7 无password 需要修改:authentication_string
use mysql
update user set authentication_string=PASSWORD('123456') where user='sys' and host='localhost'
flush privileges;
-----------------
SQL 是用户用来管理、控制数据库的专用语言
SQL入门
mysql
1、mysql客户端接口自带功能
1、h 或 help 或 ? 获取帮助
2、G 格式化输出(行转列)
3、T 或 tee 记录操作日志 tee /tmp/mysql.log
4、c 或 CTRL+c 退出mysql
5、s 或 status 查看数据库状态信息
6、. 或 source mysql> source /tmp/world.sql
7、u 或use use world
show databases 看当前所有数据库的名字
show tables 查看当前use到的数据库所有的表
show tables from world 查看目标数据库下的表
8、快捷键
上下翻页
TAB
ctrl +C
ctrl +L
2、SQL
DDL:
数据定义语言
定义范围:
库 :名字、特性
表:表名字、列
DDL语句:
create database oldboy
create table test (id int)
创建库:
CREATE DATABASE db_name CHARACTER SET charset_name COLLATE collation_name
例子
mysql> create database oldboy charset utf8 ;
mysql> show create database oldboy; 查询数据库定义信息。
修改库:
ALTER DATABASE [db_name] CHARACTER SET charset_name COLLATE collation_name
例子:
mysql> alter database oldboy charset gbk;
删除库:
drop database oldboy;
show character set;#找字符集和校对规则.
表定义(列):
表名
列名
列属性(数据类型、列约束)
创建表:
create table test(id int);
create table t1(idcard int ,name char(30),sex char(4));
修改表定义:
修改表名:
rename table t1 to test1;
alter table test1 rename to people;
修改表结构:
alter table people add addr char(40) NOT NULL;
指定添加年龄列到name列后面的位置,示例如下:
alter table people add age int(4) after name;
通过下面的命令在第一列添加qq字段。
alter table test add telnum int first;
同时添加多个列定义:
alter table people add id int first ,add sex char(4) after name ;
删除表结构:
alter table people drop sex;
修改表定义
alter table people modify name char(20);
修改列名:
alter table people change name people_name char(30) ;
---------------------------
总结DDL:
create database xxx charset
create table xxx (id int,xxxxxx)
drop table
drop database
alter table add/drop/modify/change
create user
drop user
----------------------------
DCL:数据库控制语言
grant
revoke
-----------------------
DML:
数据操纵语言:针对数据行的操作
insert语句:
create table oldboy (id int,name varchar(20));
insert into oldboy values(1,'oldboy');
insert into oldboy values(2,'yougboy'),(3,'youggilr');
select * from oldboy;
insert into oldboy(name) values('xiaoming');
INSERT INTO `test` VALUES (1,'oldboy'),(2,'oldgirl'),(3,'inca'),(4,'zuma'),(5,'kaka');
------------------------------------
create table test like oldboy;
insert into oldboy select * from oldboy;
==========================
create table test2 select * from test where 1=2
------------------------------------
update(一定要有where条件)
update test set name='oldboy1' WHERE id = 1;
delete(一定要有where条件)
delete from oldboy where id=1;
INSERT INTO `test` VALUES (1,'oldboy'),(2,'oldgirl'),(3,'inca'),(4,'zuma'),(5,'kaka');
生产中的伪删除
alter table test add state tinyint(2) not null default 1;
update test set state=1;
正常显示:
select * from test where id=1;
update test set state=0 where name='oldboy';
mysql> select * from test where state=1;
+----+---------+-------+
| id | name | state |
+----+---------+-------+
| 2 | oldgirl | 1 |
| 3 | inca | 1 |
| 4 | zuma | 1 |
| 5 | kaka | 1 |
mysql> select * from test;
+----+---------+-------+
| id | name | state |
+----+---------+-------+
| 1 | oldboy | 0 |
| 2 | oldgirl | 1 |
| 3 | inca | 1 |
| 4 | zuma | 1 |
--------------------------------
DQL:
select from
select user,password ,host from mysql.user where user='sys';
select user,password ,host from mysql.user where user like 'sy%';
select * from oldboy.test;
select id,name from oldboy.test;
select id,name from test where id=2;
select id,name from test where name='oldgirl';
select id,name from test where id>2;
select id,name from test where id>2 and id<4;
select id,name from test where id>2 or id<4;
select id,name from test;
select id,name from test order by id asc;
select id,name from test order by id desc;
select id,name from test limit 1,3;
#第一行之后取三行
select database();
select user();