DB_sql语句

来源:互联网 发布:收据软件 编辑:程序博客网 时间:2024/06/11 00:25


注:(mysql5.7版本开始创建用户需要create user/5.7版本之后,直接使用:grant 权限 on mysqlDB.table to username@localhost; 是不行的)


添加用户:
CREATE USER “test”@”localhost” IDENTIFIED BY “1234”; #本地登录 
CREATE USER “test”@”%” IDENTIFIED BY “1234”; #远程登录 

授权格式:
grant 权限 on 数据库.* to 用户名@登录主机 identified by “密码”;
#指定部分权限给用户
grant select,update on testDB.* to “test”@”localhost” identified by “1234”;
#所有数据库的某些权限
grant select,delete,update,create,drop on . to test@”%” identified by “1234”; #”%” 表示对所有非本地主机授权,不包括localhost

创建数据库:
create database testDB; 
或create database testDB default charset utf8 collate utf8_general_ci;

删除用户:
mysql -u root -p 
Delete FROM mysql.user Where User=”test” and Host=”localhost”; 
flush privileges; 
drop database testDB;

删除账户及权限:
drop user 用户名@’%’; 
drop user 用户名@ localhost;

修改指定用户密码:
update mysql.user set authentication_string=password(“新密码”) where User=”test” and Host=”localhost”; 
flush privileges;#刷新系统权限表

列出所有数据库--show database;
切换数据库--use '数据库名';
列出所有表--show tables;
查看表结构--describe 表名;
查看表创建结构--show create table 表名;
删除数据库和数据表--drop database 数据库名;|drop table 数据表名;

修改表名:
rename table 表名 to  新表名
修改表的字符集:
alter table 表名 character set 字符集 (例:alter table user character set gbk)
添加列:
alter table 表名 add 列名 数据类型(例:alter table users add image blob;)
修改列:
alter table 表名 modify 列名 新的数据类型(数据长度大小)(例:alter table users modify sex char(2);)
删除列:
alter table 表名 drop 列名
修改列名字:
alter table 表名 change column 原列名 新列名 类型 (例:alter table users change column sex sexy varchar(2);)

建表:
例子:
create table shujubiao( 
id int primary key auto_increment, 指定为i整形 #自增长代码代表:auto_increment #主建的代码代表:primary key
name varchar(32) default '1' not null, 指定为不固定长度,最大为32为字符,不能为空  default设置默认值
age tinyint unsigned not null 指定为小整型 
foreign key (class)  references class(code) #外键的代码代表公式:foreign key (列名)  references  主表名 (列名)

#unsigned    为“无符号”的意思
例如如果    tinyint最大是127,那tinyint    unsigned    最大就可以到   127 * 2 |不加即为(有符号值:-128 到127)
#唯一  unique
复合主键
CREATE TABLE t1(
id int not null,
name char(20),
primary key (id,name)
);


union和union all的区别就是一个会去重一个不会
turncate tablename; #删除整表数据,自增长id从头再来,快速,从磁盘直接删除,不可恢复
delete from student; #删除整个表的数据,自增长继续
原创粉丝点击