MySQL库表操作

(1)创建数据库Market.
mysql> create database Market;
Query OK, 1 row affected (0.00 sec)
(2)创建数据表customers,在c_num字段上添加主键钓束和自增的束,在c_birth字段
mysql> use Market;
Database changed
mysql> CREATE TABLE customers (
    ->   c_num INT(11) PRIMARY KEY AUTO_INCREMENT,
    ->   c_name VARCHAR(50),
    ->   c_contact VARCHAR(50),
    ->   c_city VARCHAR(50),
    ->   c_birth DATETIME NOT NULL
    -> );
Query OK, 0 rows affected (0.01 sec)
​
(3)将c_contact字段插入到e birth字段后面.
mysql> ALTER TABLE customers MODIFY  c_contact VARCHAR(50) AFTER c_birth;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> DESC customers;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| c_num     | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name    | varchar(50) | YES  |     | NULL    |                |
| c_city    | varchar(50) | YES  |     | NULL    |                |
| c_birth   | datetime    | NO   |     | NULL    |                |
| c_contact | varchar(50) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
​
mysql> UPDATE customers set c_birth = CONCAT(c_birth,c_contact);
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0
(4)将c_name字段数据类型政为VARCHAR(70).
mysql> ALTER TABLE customers MODIFY c_name VARCHAR(70);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> DESC customers;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| c_num     | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name    | varchar(70) | YES  |     | NULL    |                |
| c_contact | varchar(50) | YES  |     | NULL    |                |
| c_city    | varchar(50) | YES  |     | NULL    |                |
| c_birth   | datetime    | NO   |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
(5)将c_contact字段改名为c_phone.
mysql> ALTER TABLE customers CHANGE  c_contact c_phone VARCHAR(50);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> DESC customers;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| c_num   | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name  | varchar(70) | YES  |     | NULL    |                |
| c_city  | varchar(50) | YES  |     | NULL    |                |
| c_birth | datetime    | NO   |     | NULL    |                |
| c_phone | varchar(50) | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
(6)增加e_gender字段,数据类型为CHAR(1)
mysql> ALTER TABLE customers ADD c_gender CHAR(1);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> DESC customers;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| c_num    | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name   | varchar(70) | YES  |     | NULL    |                |
| c_city   | varchar(50) | YES  |     | NULL    |                |
| c_birth  | datetime    | NO   |     | NULL    |                |
| c_phone  | varchar(50) | YES  |     | NULL    |                |
| c_gender | char(1)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
(7)将表名修政为customers_info.
mysql> ALTER TABLE customers RENAME customers_info;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> ALTER TABLE customers RENAME customers_info;
Query OK, 0 rows affected (0.00 sec)
​
mysql> DESC customers_info;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| c_num    | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name   | varchar(70) | YES  |     | NULL    |                |
| c_city   | varchar(50) | YES  |     | NULL    |                |
| c_birth  | datetime    | NO   |     | NULL    |                |
| c_phone  | varchar(50) | YES  |     | NULL    |                |
| c_gender | char(1)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
(8)删除字段c_ciy.
mysql> ALTER TABLE customers_info DROP c_city;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> DESC customers_info;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| c_num    | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name   | varchar(70) | YES  |     | NULL    |                |
| c_birth  | datetime    | NO   |     | NULL    |                |
| c_phone  | varchar(50) | YES  |     | NULL    |                |
| c_gender | char(1)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
(9)修改数据表的存储引擎为MyISAM.
mysql> ALTER TABLE customers_info ENGINE MyISAM;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> DESC customers_info;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| c_num    | int(11)     | NO   | PRI | NULL    | auto_increment |
| c_name   | varchar(70) | YES  |     | NULL    |                |
| c_birth  | datetime    | NO   |     | NULL    |                |
| c_phone  | varchar(50) | YES  |     | NULL    |                |
| c_gender | char(1)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
​
(1) 创建数据表orders,在o_num字段上添加主键约束和自增约束,在c id字段上添加外键约束,关联customers表中的主键c_num
mysql> CREATE TABLE orders (   o_num INT(11) PRIMARY KEY AUTO_INCREMENT,   o_date DATE,   c_id INT(11), FOREIGN KEY (c_id) REFERENCES customers_info(c_num) );
Query OK, 0 rows affected (0.00 sec)
​
mysql> DESC orders;
+--------+---------+------+-----+---------+----------------+
| Field  | Type    | Null | Key | Default | Extra          |
+--------+---------+------+-----+---------+----------------+
| o_num  | int(11) | NO   | PRI | NULL    | auto_increment |
| o_date | date    | YES  |     | NULL    |                |
| c_id   | int(11) | YES  | MUL | NULL    |                |
+--------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
(2) 删除orders表的外键约束,然后删除表customer_info
show create table orders 查看外键名
alter table orders dorp foreign key 外键名
dorp table customerso_info
1、​
mysql> create DATABASE Team;
Query OK, 1 row affected (0.00 sec)
​
mysql> use Team;
Database changed
mysql> CREATE TABLE player(
    -> playid INT PRIMARY KEY,
    -> playname VARCHAR(30) NOT NULL,
    -> teamnum INT NOT NULL UNIQUE,
    -> info VARCHAR(50)
    -> );
Query OK, 0 rows affected (0.01 sec)
1)创建一个新账户,用户名为account1,该用户通过本地主机连接数据库,密码为 oldpwdl。授权该用户对Team数据库中player表的SELECT和INSERT权限,并且授权该用户对player表的info字段的UPDATE权限。
更改密码策略
mysql> SET GLOBAL validate_password_special_char_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL validate_password_number_count=0;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL validate_password_length=0;
Query OK, 0 rows affected (0.00 sec)
mysql> create user account1@'localhost' identified by 'oldpwdl';
Query OK, 0 rows affected (0.01 sec)
mysql> grant select,insert,updat(info)on Team.player to accountl@'localhost' identified by '123456';
mysql> grant select,insert,update(info) on Team.player to account1@'localhost' identified by 'oldpwd1';
Query OK, 0 rows affected, 1 warning (0.01 sec)
(2) 创建SQL语句,更改accountl用户的密码为newpwd2.
mysql> set password for 'account1'@'localhost'=password('newpwd2');
Query OK, 0 rows affected, 1 warning (0.00 sec)
(3) 创建SQL语句,使用FLUSH PRIVILEGES重新加载权限表。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
(4) 创建SQL语句,查看授权给accounti用户的权限。
show grants for account1@'localhost';
mysql> show grants for account1@'localhost';
+----------------------------------------------------------------------------------+
| Grants for account1@localhost                                                    |
+----------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'account1'@'localhost'                                     |
| GRANT SELECT, INSERT, UPDATE (info) ON `Team`.`player` TO 'account1'@'localhost' |
+----------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
(5) 创建SQL语句,收回account1用户的权限。
mysql> REVOKE ALL PRIVILEGES ON Team.player FROM 'account1'@'localhost';
Query OK, 0 rows affected (0.00 sec)
(6) 创建SQL语句,将accounti用户的账号信息从系统中删除。
mysql> drop user 'account1'@'localhost';
Query OK, 0 rows affected (0.00 sec)
​

你可能感兴趣的:(mysql,adb,android)