mysql servermysql -h localhost -u root -p //Connection Database enterpasswd: //Type the password >show databases; //output database name table >create database test_db; //Create a name that is Test_ DB database >use test_db; //into test_db database >create table tb_emp1> (>id int (one),> Name varchar (,>deptid int ),>salary float>);//Create a database table labeled TB_EMP1, with 4 fields in the table. The field name ID represents the list label, the field name name represents the employee name, the field name DeptID represents the department number, the field name salary represents the employee salary >show tables; //the table in the output database >desc tb_emp1; //the name of the field in the output datasheet//method//1 that defines the primary key. Specify the primary key while defining the column >create table tb_emp2> (>id int (11) primary key,>name varchar (+), >deptid int (one),>salary float>);//Create a database, Marked as TB_EMP2, the table has 4 fields, the field name ID indicates the list number and the definition of the primary key//key can uniquely represent a record in the table, can be combined with foreign keys to define the relationship between different data tables, and can speed up the database query speed >DESC TB_EMP2 ; //use desc to display the names of the columns in the datasheet, and the ID field is defined as PRI//2. Define the primary key >create table tb_emp3> (>id int (11) After all columns have been defined. >name vaRchar (+), >deptid int (one), >salary float,>primary key (ID) >);//Effect ditto//Set multi-field federated primary key One cannot be determined alone, and multiple combinations are required to determine the uniqueness. >create table tb_emp4> (>id int, >name varchar), >deptid int (11), >salary float,>primary key (Id,deptid) >) >desc tb_emp4; // The View Discovery ID and DeptID fields are labeled as primary keys The Federated primary key//foreign key called Table Tb_emp4 is used to establish a connection between two tables, one or more, the foreign key of a table can be a null value, if it is not a null value, it must be the primary key key value of the other table// A foreign key is a field of a table that can not be a primary key for the table, but must correspond to the primary key//definition foreign key of another table, and does not allow the deletion of rows that have an association relationship in another table, and the role of foreign keys is to maintain consistent integrity of the data. The primary key of the department table tb_dept is the ID, and in the employee table TB_EMP5 there is a key deptid associated with this ID >create table tb_dept> (>id int (one) Primary key,>name varchar ( not null, //) uses a non-null constraint field flag not null>location varchar (>);//Create a new department table with ID for department number, name for department name, location for department position >create table tb_emp5> (> Id int ( primary key,>name varchar), >deptid int (one), >salary float, >constraint fk_emp_dept1 foreign key (DeptID) references tb_dept (ID) >);//Create a new Employee Information table TB_EMP5, add a FOREIGN key constraint with the name fk_emp_dept1 in the table, The outer check name is DeptID, which relies on the primary key of table tb_dept1 id//template constraint foreign key name foreign key (field name 1) references Primary table name (primary key 1)//The connection between the Word table and the parent table//related table, the foreign key of the child table (from the table) must be associated with the primary key of the parent table (primary table), and the data type of the associated field must match >create table tb_emp6> (>id int ( primary key,>name varchar) not null >deptid int (11), >salary float,>constraint fk_emp_dept2 foreign key (DeptID) references tb_ EMP6 (ID) >);//define Non-empty field name insert value cannot be null/uniqueness constraint (unique) requires the column to be unique, allowed to be empty, but only one null value/Field Uniqueness Constraint Definition method 1>create table Tb_dept2> (>id int (one) primary key,>name varchar () unique,>location varchar (>) >desc tb_dept2 //display field key value is uni//field uniqueness Constraint Definition method 2>create table Tb_dept3> (>in int (one) primary key,>name varchar (a), >location varchar (50) , >CONSTRAINT STH&NBSp;unique (name) >);>desc tb_dept3show create table table name //view table FOREIGN KEY constraint name// The default constraint is used to specify the default value for the column. If you insert a new record that is not copied for this field, the system automatically assigns a default value to the field. Field names data types default defaults >create table tb_emp7> (>id int (one) primary key,>name varchar ( not null,>deptid int) default 11111, //set the employee's department number default 11111>salary float,>constraint fk_emp7_dept foreign key (DeptID) references tb_dept (ID) >) >desc tb_emp7 //found the default key value for DeptID column in TB_EMP7 table is 11111// Adding the Auto_increment keyword to the primary key allows the system to automatically generate primary key values, and with each additional record, the primary key value plus 1 the keyword can be applied to any integer type >create table tb_emp8> ( >id int ( primary key auto_increment,>name varchar) not null, >deptid int (one) default 11111,>salary float,>constraint fk_emp8_dept Foreign key (DeptID) references tb_dept (ID) >); >DESC TB_EMP8 //TBThe DeptID column in the _EMP8 table has the default key value of 11111,id column extra The key value is auto_increment//below let's test it//First we now add a record in the Tb_dept table with a department ID of 11111, otherwise in tb_ A record is inserted in EMP8 because of a missing default 11111 foreign key value for the Department ID and error >insert into tb_dept (id,name,location) >values (11111, ' Server ', ' Shaanxi,china '); >insert into tb_emp8 (name,salary) >values (' Frank ', 6000), (' Tom ', 5600), ( ' Grace ', 5900;//Below we use Select to display all records results in the table to see the ID value automatically increased >select * from tb_emp8 // We can view the values of variables in MySQL >show variables like '%xxx% '; //view the values of Session variables can view auto_ Increment variable value >show global variables like '%xxx% '; //view the value of a global variable//view table BASIC Structure Statement >describe tb_emp8;>desc tb_emp8;//table Basic Structure Extreme explanation//null : whether the column can store null values, yes, or No//key: Indicates whether the column can store null values, The PRI indicates that the column is part of a table's primary key, and uni indicates that the class is part of a unique index, and Mul indicates that a given value in the column allows multiple//default: Table Sohu Dead The column has a default value, if any, how many//extra: Represents the additional information that can be obtained about a given column, such as auto_increment//view table Detail Structure Statement >show create table tb_emp8\g;//add \g parameter is the display effect neat/ /difference The  DESC statement shows the basic structure of the table in the form of a table show creThe ate table statement displays the detailed structure of the subscript in the form of a statement
mysql5.6 first day of study on primary use method