Command operations in cmd under windows

Command in cmd under windows:
 
cls clear
Up and down arrows for command history command switching
----------------------------------------------------------------------------------------------------------------
Start the MySQL service using the command line
 
(1) Start the MySQL service ---- net start mysql
(2) Close the MySQL service ---- net stop mysql
All windows system services are available with net start and net stop;
----------------------------------------------------------------------------------------------------------------
MySQL login:
mysql -v; (version information);
mysql -u[username] -p[password] -P[port][default:3306] -h[ip][default:127.0.0.1] (default port and local login do not need to write -P and -h)
 
Parameter directory:
-v ----- version
-u ----- username
-p ----- password
-P ----- port number
-h ----- server
 
MySQL exits:
exit;
quit;
\q
----------------------------------------------------------------------------------------------------------------
Modify the MySQL prompt:
 
Specified by parameters when connecting to the client
shell>mysql -uroot -proot -prompt \h>
After connecting to the client, pass ormpt
mysql>prompt \h>
 
After carriage return it becomes:
localhost>
 
Parameter directory:
\D----- full date
\d------ current database
\h------ server name
\u------ current user
 
Example: mysql>PROMPT \u@\h \d>
Output result: root@localhost(none)>
(Output result: root (current user)@localhost (hostname) (none) (doesn't open the database)>)
----------------------------------------------------------------------------------------------------------------
mysql common commands:
 
select version() displays the current server version
select now() displays the current date and time
select user() displays the current user
 
Specification of mysql statement:
 
1) All keywords and function names are capitalized
2) Database name, table name, field name are all lowercase
3) Statements must end with a semicolon;
----------------------------------------------------------------------------------------------------------------
View the list of databases under the current server
show databases
 
{} required | or [] optional
create database
CREATE {DATABASE | SCHEMA}[IF NOT EXISTS] db_name
[DEFAULT]   SET [=] charset_name
Modify the database:
ALTER {DATABASE | SCHEMA}[db_name]
[DEFAULT] CHARACTER SET [=] charset_name
delete database
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
show alarm
show warnings
Check out the CREATE DATABASE statement command:
SHOW CREATE DATABASE jj;
----------------------------------------------------------------------------------------------------------------
Data type refers to the data characteristics of columns, stored procedure parameters, expressions and local variables, which determine the storage format of data and represent different information types;
 
Shaping:
TINYINT: Signed -128~127: Unsigned 0-255; Occupies 1 byte;
SMALLINT: Signed -32768~32767: Unsigned 0~65535; Occupies 2 bytes;
MEDIUMINT: Signed -8388608~8388607: Unsigned 0~16777215; Occupies 3 bytes;
INT: Signed -2147483648~2147483647: Unsigned 0-4294967259; Occupies 4 bytes;
BIGINT: Signed -9223372036854775808~9223372036854775807: Unsigned 0~18446744073709551615; occupies 8 bytes.
 
floating point
FLOAT[(M,D)]
M is the total number of digits and D is the number of digits behind the sales classical. If MD is omitted, the value is saved according to the limits allowed by the hardware,
Single-precision numbers are accurate to about 7 decimal places.
 
DOUBLE[(M,D)]
The fractional value range is about 10 times larger than single precision,
 
Date time type Size in bytes
YEAR                                  1
TIME                                  3    
DATA                                 3
DATATIME 8
TIMESTAMP (timestamp) 4
 
Character type:
1. CHAR(M) M bytes, 0<=M<=255, fixed-length type; what is a fixed-length type? We wrote a CHAR(5) and only wrote an ABC. When it is stored in the computer, in addition to storing ABC, it will also add two spaces.
2. VARCHAR(M) is a variable-length type. If ABC is stored, then ABC is stored, 0~65535.
3. TINYTEXT, 2 to the 8th power;
4. TEXT, 2 to the 16th power byte.
5. MEDIUMTEXT, 2 to the 24th power.
6. LONGTEXY, 2 to the 32nd power.
7. ENUM, 1 or 2 bytes, depending on the number of enumeration values ​​(up to 65535 values).
8. SET, 1, 2, 3, 4, or 8 bytes, depending on the number of set members (up to 64 members).
----------------------------------------------------------------------------------------------------------------
Create data table
Step 1 Open the database: use t1; (the database to be opened)
 
Step 2: CREATE TABLE mytable (the name of the data table) (
      column_name data_type,
      //column name type,
       ....
);
UNSIGNED unsigned (no negative values);
List:
CREATE TABLE tb3 (
id SMALLINT UNSIGNED AUTO_INCREMENT,
username VARCHAR(30) NOT NULL ,
PRIMARY KEY(id)
);
----------------------------------------------------------------------------------------------------------------
 
View the current database: SELECT DATABASE();
View the data tables in the current library: SHOW TABLES;
View data tables in any library: SHOW TABLES FROM db_name;
View the data table structure: SHOW COLUMNS FROM table_name;
View the storage engines supported by the system: Show engines;
View the storage engine used by the table: show table status from db_name where name='table_name';
Insert record: INSERT [INTO] tbl_name [(col_name,...)] VALUES(val,...)
View the create table statement: SHOW CREATE TABLE table_name;
Record lookup: SELECT * FROM tbl_name;
Query index: SHOW INDEXES FROM provinces\G;
 
null allows null values;
not null does not allow non-null values;
AUTO_INCREMENT self-increment attribute, must be used in combination with the primary key
 
PRIMARY KEY primary key constraint:
                There can only be one primary key per table
                The primary key ensures the uniqueness of the record
                The primary key is automatically not null;
UNIOUE KEY unique constraint:
                Guaranteed record uniqueness
                Field can be empty
                Each table can have multiple unique constraints
DEFAULT default value
                When inserting a record, if the field is not explicitly assigned a value, the default value is automatically assigned;
FOREIGN KEYp :
                Maintain data consistency and integrity.
                Implement one-to-one or one-to-many relationships
 
Requirements for foreign key constraints:
1. The parent table and the child table must use the same storage engine, and the use of temporary tables is prohibited. (Parent table: table referenced by child table Child table: table with foreign key columns)
 
2. The storage engine of the data table can only be InnoDB.
 
3. Foreign key columns and reference columns must have similar data types. The length of the digits or the signed bit must be the same; the length of the characters can be different. (Foreign key column: The column that FOREIGN KEYp has been added to, the reference column: the column referenced by the foreign key column is called the reference column)
 
4. Foreign key columns and exhibitor columns must be indexed. If there is no index on the foreign key column, mysql will automatically create the index. (If there is no index on the reference column, mysql will not automatically create an index. But if the reference column is the primary key, it will automatically create an index. When the primary key is created, it will automatically create an index, so the reference column actually already has an index. And If there is no index created on the foreign key column, mysql will automatically create the index.)
 
Edit the default storage engine for data tables
mysql configuration file
        default-storage-engine=INNODB;
        
Create foreign key: FOREIGN KEY (pid) REFERENCES provinces (id);
Refer to the operation
when creating the table;
In foreign key (pid) references provinces(id) on delete cascade, (written like this)
 
1. Add a single column: If FIRST is specified, it is at the front of the entire table, and it is not written as the last of the entire table by default; if AFTER col_name is specified, it is after col_name.
ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST  AFTER col_name]
2. Add multiple columns: FIRST/AFTER cannot be specified, it can only default to the last.
ALTER TABLE tbl_name ADD [COLUMN] (col_name column_definition,...)
3. Delete a single column
ALTER TABLE tbl_name DROP [COLUMN] col_name
4. Delete multiple columns (Ps: while deleting a column, add a new column. Separate them with commas)
ALTER TABLE tbl_name DROP [COLUMN] col_name, DROP [COLUMN] col_name,DROP [COLUMN] col_name
 
 
 
1. Add a primary key constraint
CONSTRAINT_PK primary key name is PK
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
e.g:ALTER TABLE users ADD CONSTRAINT PK_users_id PRIMARY KEY (id);
2. Add a unique constraint
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...)
e.g:ALTER TABLE users ADD UNIQUE (username);
3. Add foreign key constraints
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name](index_col_name,...) reference_definition
e.g:ALTER TABLE users ADD FOREIGN KEY (pid) REFERENCES provinces (id)
4. Add/remove default constraint DEFAULT
ALTER TABLE tbl_name ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
eg:
ALTER TABLE users ADD age TINYINT UNSIGNED NULL;
ALTER TABLE users ALTER age SET DEFAULT 15;
ALTER TABLE users ALTER age DROP DEFAULT;
 
The index_name of a constraint can be queried by SHOW INDEX FROM table_name\G;
Remove the primary key constraint:
ALTER TABLE table_name DROP PRIMARY KEY;
Remove the unique constraint:
ALTER TABLE table_name DROP INDEX index_name;
Remove foreign key constraints:
ALTER TABLE table_name DROP FOREIGN KEY (fk_symbol) The name given by the query system by default;
The name fk_symbol of the foreign key constraint can be queried by SHOW CREATE TABLE table_name;
Delete the foreign key constraint, look for CREATE TABLENAME to find the name added by the system for the foreign key constraint
 
1. Modify the column definition
ALTER TABLE tbl_name MODIFY [COLUMN] col_name column_definition [FIRST |AFTER col_name];
ALTER TABLE users2 MODIFY id SMALLINT UNSIGNED NOT NULL FIRST; // Mention the position of the id field to the first column
SHOW COLUMNS FROM users2;
ALTER TABLE users2 MODIFY id TINYINT UNSIGNED NOT NULL; //Modify the data type, pay attention to the problem of data loss
2. Modify the column name
ALTER TABLE tbl_name CHANGE [COLUMN] col_name new_col_name column_definition [FIRST|AFTER col_name];
ALTER TABLE users2 CHANGE pid p_id TINYINT UNSIGNED; //Modify column name
3. Data sheet renamed
method 1
ALTER TABLE tbl_name RENAME [TO/AS] new_tbl_name
ALTER TABLE users2 RENAME (to/as) users3;
SHOW TABLES;
Method 2
RENAME TABLE tbl_name TO new_tbl_name [, tbl_name2 TO new_tbl_name2] ...
RENAME TABLE users5 TO users2;
Minimize the use of data table column names and table name renaming.
2. Constraints
By function: NOT NULL, PRIMARY KEY, UNIQUE KEY, DEFAULT, FOREIGN KEY
Divided by the number of data columns: table-level constraints, column-level constraints
3. Modify the data table
Operations on fields: add/delete fields, modify column definitions, modify column names, etc.
Operations on Constraints: Add/Remove Various Constraints
Operation for the data table: rename the data table (two ways)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324932945&siteId=291194637